1

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

1 Answer 1

4

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 (@()).

Sign up to request clarification or add additional context in comments.

2 Comments

Nice solution! To be precise @() is not the array initializer, which would be ,. It is the array sub-expression operator.
Thanks. This works like a charm. I got where the problem was!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.