将 PowerShell 中的变量参数传递给本机应用程序

将 PowerShell 中的变量参数传递给本机应用程序

以下是我的部署自动化脚本的一部分:

$SVNEXE = "$env:ProgramFiles\TortoiseSVN\bin\svn.exe"
foreach ($element in $commandstoken) {
#$exportfile = [System.Web.HttpUtility]::UrlPathDecode($StreamServe5RepoURI + $commandstoken[$a])
$exportfile = ($StreamServe5RepoURI + $commandstoken[$a])
$SVNRevision = $commandstoken[$a+1]
[string]$SVNCommand = "export -r $SVNRevision --force"
Write-Host -ForegroundColor Red $SVNCommand
Write-Host -ForegroundColor Red $SVNCommand $exportfile
&"$SVNEXE" $SVNCommand $exportfile "C:\Temp\__foo\export"
$a = $a+2
}

我想将变量传递给 svn.exe $SVNCommand,但 svn.exe 抛出一个错误:svn.exe : Unknown subcommand: 'export -r 2384 --force'
据我所知,变量扩展正在工作,所以我不明白为什么会svn.exe抛出这个错误。

答案1

该错误Unknown subcommand: 'export -r 2384 --force'表明调用运算符将 'export -r 2384 --force''export -r 2384 --force'​​其视为单个参数(来自 technet 上的 & 呼叫操作员):

当外部命令有很多参数或参数或路径中有空格时,事情就会变得棘手!有了空格,你就必须嵌套引号,结果并不总是很清楚!

对于您来说,您可以按照该页面上的建议进行操作:

 $SVNcommand = @('export', '-r', $SVNRevieion, "--force", $exportfile, "C:\Temp\__foo\export") 

并使用如下所有参数进行调用:

&"$SVNEXE" $SVNCommand 

答案2

我让它工作的唯一方法是将每个参数保存在单独的变量中:

$arg1 = 'export', '-r'
$arg2 = $SVNRevision
$arg3 = '--force'
$arg4 = $exportfile
$arg5 = 'C:\Temp\__foo\export\'
&"$SVNEXE" $arg1 $arg2 $arg3 $arg4 $arg5

这似乎实际上是一个错误执行需要引号和变量的命令实际上是不可能的

相关内容