powershell - Powershell正在从程序参数中删除逗号

标签 powershell

我想将逗号分隔的值列表作为单个开关的一部分传递到脚本中。

这是程序。

param(
  [string]$foo
)

Write-Host "[$foo]"

这是用法示例
PS> .\inputTest.ps1 -foo one,two,three

我希望该程序的输出为[one,two,three],但它返回[one two three]。这是一个问题,因为我想使用逗号来定义值。

为什么Powershell删除逗号?为了保留它们,我需要更改什么?

最佳答案

逗号是powershell中的特殊符号,用于表示数组分隔符。

只需将参数放在引号中:

inputTest.ps1 -foo "one, two, three"

或者,您可以“引用”逗号:
inputTest.ps1 -foo one`,two`,three

关于powershell - Powershell正在从程序参数中删除逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11990688/

相关文章:

Powershell,如何为共享文件夹添加权限

arrays - 为什么当直接分配或从 get-content 检索数组时,数组的行为会有所不同

Windows PowerShell。通过 PowerShell 命令运行程序

powershell - 创建新对象 PSObject 时出错

powershell - 我们可以在PowerShell中编写星级程序吗?

git - PowerShell 捕获 Git 输出

c# - 无法使用 Azure Batch 帐户任务执行 powershell 脚本

git - 如何在 Powershell 提示符中右对齐某些内容?

powershell - 如何以编程方式获取 Azure 订阅的所有成员

powershell - Powershell复制项目,将新文件夹存储为变量