powershell - 创建一个可以减少结果的管道函数

标签 powershell

在我的脚本中,我经常使用逗号连接,并希望创建一个可以通过管道传输的辅助函数,以便我可以做到

$fileNames | %{ "../$_.js" } | Join-ByComma

而不是必须做
($fileNames | %{ "../$_.js" }) -join ', '

我无法弄清楚如何以适用于管道输入的方式执行此操作。我试过这样的事情
function Join-ByComma($arr) { 
    $arr -join ', '
}


function Join-ByComma($arr) { 
    Process { $_ }
    End { $arr -join ', ' }
}

两者都不起作用

最佳答案

您可以使用 $Input自动变量,代表管道输入:

function Join-ByComma {
    @($Input) -join ', '
}

关于powershell - 创建一个可以减少结果的管道函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29736505/

相关文章:

powershell - 尝试在CSV列表中管道导入CSV

powershell - 需要使用Powershell根据数组中的名称选择一个项目

powershell - 使用Powershell搜索包含特定编号的文件

powershell - 是否可以在一行 PowerShell 中启动具有多个选项卡的 Microsoft Edge?

powershell - 如何使用 Azure PowerShell 获取虚拟机的 MAC 地址

json - 使用 ConvertTo-Json 转换为 JSON 时保留数据类型名称

powershell - 如何列出所有通讯组的名称,组类型,管理者和广告描述

用于将 Hyper-V guest 状态保存到磁盘的 Powershell 命令

azure - PowerShell 任务未在 Azure Pipelines 中运行脚本?

windows - git-bash.exe : how to run single command?