【问题标题】:Run Executable from Powershell script with parameters使用参数从 Powershell 脚本运行可执行文件
【发布时间】:2014-09-30 23:05:53
【问题描述】:
所以我有一个 powershell 脚本,它应该运行一个带有参数的可执行文件,以设置我要运行的方法,我需要传递一个参数,它是一个配置文件的目录。所以这就是我所拥有的
Start-Process -FilePath "C:\Program Files\MSBuild\test.exe" -ArgumentList /genmsi/f $MySourceDirectory\src\Deployment\Installations.xml
/f 是短名称,file 是我的属性的长名称...我在 powershell 中收到一个错误,告诉我找不到 /f 或 /file 的位置参数。
有什么想法吗?
【问题讨论】:
标签:
powershell
command-line
【解决方案1】:
只需添加一个对我来说效果很好的示例:
$sqldb = [string]($sqldir) + '\bin\MySQLInstanceConfig.exe'
$myarg = '-i ConnectionUsage=DSS Port=3311 ServiceName=MySQL RootPassword= ' + $rootpw
Start-Process $sqldb -ArgumentList $myarg
【解决方案2】:
我可以通过使用 Invoke-Expression cmdlet 来实现它。
Invoke-Expression "& `"$scriptPath`" test -r $number -b $testNumber -f $FileVersion -a $ApplicationID"
【解决方案3】:
这是执行多个参数的另一种方法。当论点对于一个单行来说太长时,我会使用它。
$app = 'C:\Program Files\MSBuild\test.exe'
$arg1 = '/genmsi'
$arg2 = '/f'
$arg3 = '$MySourceDirectory\src\Deployment\Installations.xml'
& $app $arg1 $arg2 $arg3
【解决方案4】:
尝试引用参数列表:
Start-Process -FilePath "C:\Program Files\MSBuild\test.exe" -ArgumentList "/genmsi/f $MySourceDirectory\src\Deployment\Installations.xml"
您也可以将参数列表提供为数组(逗号分隔的参数),但使用字符串通常更容易。