【问题标题】:PowerShell run exe file and pass args as string varPowerShell 运行 exe 文件并将 args 作为字符串 var 传递
【发布时间】:2022-11-11 20:47:33
【问题描述】:

我正在创建一个 powershell 脚本来运行带有参数的 exe 文件。 args 列表的构造方式是,如果 arg 的值为空或 null,则不应传递参数

下面是我的脚本


 $runnerCommand = " "
[string]$splitRun = "20:1"
[string]$maxTestWorkers = "777"
[string]$retryTimes = "9"
[string]$testFilterInXmlFormat = "<filter><cat>XX</cat></filter>"

#$runnerCommand += '--testDllPath ' + $testDllPath + " "

if ($splitRun){
    $runnerCommand+= "--splitRun '$splitRun' "
}

if ($maxTestWorkers){
    $runnerCommand+= "--maxTestWorkers '$maxTestWorkers' "
}

if ($retryTimes){
    $runnerCommand+= "--retryTimes '$retryTimes' "
}

if ($testFilterInXmlFormat){
    $runnerCommand+= "--testFilterInXmlFormat '$testFilterInXmlFormat' "
}


$cmdPath = "C:\AutoTests\TestAutomation.Runner\bin\Debug\TestAutomation.Runner.exe"


& $cmdPath --testDllPath C:/AutoTests/Build/TestAutomation.TestsGUI.dll $runnerCommand

看起来 PowerShell 在最后一行代码的 $runnerCommand 之前做了一个“新行”,导致没有从 $runnerCommand 传递参数

请建议如何解决问题。

我尝试了不同的方法

【问题讨论】:

    标签: powershell


    【解决方案1】:

    您当前将所有参数作为单个字符串传递。您应该使用一个数组,每个参数作为一个单独的元素。我实际上无法对此进行测试,但是这样的事情应该可以工作:

    [string]$splitRun = "20:1"
    [string]$maxTestWorkers = "777"
    [string]$retryTimes = "9"
    [string]$testFilterInXmlFormat = "<filter><cat>XX</cat></filter>"
    
    $runnerArgs = @(
        '--testDllPath', 'C:/AutoTests/Build/TestAutomation.TestsGUI.dll'
        if ($splitRun) {
            '--splitRun', $splitRun
        }
        if ($maxTestWorkers) {
            '--maxTestWorkers', $maxTestWorkers
        }
        if ($retryTimes) {
            '--retryTimes', $retryTimes
        }
        if ($testFilterInXmlFormat) {
            '--testFilterInXmlFormat', $testFilterInXmlFormat
        }
    )
    
    $cmdPath = "C:AutoTestsTestAutomation.RunnerinDebugTestAutomation.Runner.exe"
    
    & $cmdPath $runnerArgs
    

    请注意,PowerShell 允许在数组初始值设定项内使用表达式,包括 if-expressions。

    【讨论】:

      猜你喜欢
      • 2014-04-20
      • 2020-09-12
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      • 2014-01-01
      • 2023-03-30
      相关资源
      最近更新 更多