I'm creating a power shall scrip to run exe file with arguments. The list of args are constructed in a way that if value of the arg is empty or null, the parameter shall not be passed
Below is my script
$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
It looks like that PowerShell do a 'new line' before $runnerCommand in the last line of code that results in not passing the args from $runnerCommand
Please suggest how to solve the problem.
I tried different approaches
CodePudding user response:
You're currently passing all of the arguments as a single string. You should instead use an array with each argument as a separate element. I can't actually test this, but something like this should work:
[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:\AutoTests\TestAutomation.Runner\bin\Debug\TestAutomation.Runner.exe"
& $cmdPath $runnerArgs
Note that PowerShell allows expressions, including if
-expressions, inside the array sub-expression operator (@()
).