1

我正在尝试使用 Powershell 如下通过命令行参数 ($args[0]) 传递属性,但没有考虑它。

if ( Test-path -path $args[0]) {
            &"$MsbuildBinPath\Msbuild.exe" $MSBuildFile  /t:BuildAll "/p:AllComponents=$args[0]"  $Logger $ErrorLogger
            if ($LastExitCode -ne 0) {
                    Write-Host "It failed, send a mail"
            }
    }

如果我像下面这样通过属性,则正在考虑它。

"/p:AllComponents=List.txt" 

为什么直接应用命令行参数时不考虑它?

我可以将命令行值存储在某个变量中并传递,但是还有其他机制可以直接传递它吗?

4

1 回答 1

3

$args[0] 不在字符串内展开,您需要将其括在子表达式符号中:

... "/p:AllComponents=$($args[0])"

要避免这种语法,请将参数分配给变量并将变量嵌入到字符串中:

$argsZero= $args[0]
... "/p:AllComponents=$argsZero"

有关更多信息,请在控制台中键入以下内容:

Get-Help about_Quoting_Rules
于 2011-12-26T15:02:21.177 回答