【问题标题】:Passing Array to PowerShell from Windows CMD [duplicate]从 Windows CMD 将数组传递给 PowerShell [重复]
【发布时间】:2019-08-13 15:12:58
【问题描述】:

我正在尝试使用以下命令从 Windows CMD 执行 PowerShell 文件。

powershell -NoProfile -NonInteractive -ExecutionPolicy ByPass -File C:\Users\akarri\Desktop\Temp\test1.ps1 -ConfigPathsArray @(1,2,4)

PowerShell 代码:

Param(
   [Parameter(Mandatory=$True,Position=0)]
   [Object[]] $ConfigPathsArray       
)

function Start-Transform($configsArray) {
    [Object[]] $test = @(1,2,4)
    $test.GetType()

    foreach ($configArray in $test) {
        Write-Output $configArray
    }

    $configsArray.GetType()
    foreach ($configArray in $configsArray) {
        Write-Output $configArray
    }
}

Start-Transform -configsArray $ConfigPathsArray -Verbose

虽然我从 CMD 传递一个数组,但 $configPathsArray 的计数是 1

当我使用 -Command 选项而不是 -File 执行命令时它工作正常,但我需要您的帮助来使用 -File 命令执行相同的命令(使用 TeamCity Enterprise 2019.1.1(内部版本 66192))

【问题讨论】:

    标签: powershell teamcity command-prompt


    【解决方案1】:

    about_powershell.exe:

    相比之下,在 cmd.exe 中运行 powershell.exe -File .\test.ps1 -TestParam $env:windir 会导致脚本接收到文字字符串 $env:windir,因为它对当前 cmd 没有特殊含义。 exe外壳。

    这意味着@(1,2,4) 作为文字字符串传递给您的字符串,因为它不会扩展为数组。结果$ConfigPathsArray 拥有一个字符串条目@(1,2,4),因此Count 返回1

    您可以尝试使用 TeamCity PowerShell Runner,如 here 所述。

    【讨论】:

      猜你喜欢
      • 2022-01-05
      • 2021-11-27
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      相关资源
      最近更新 更多