I have a powershell script that is calling a batch script (running perl inside) which isn’t parsing parameters correctly when called from my powershell script. The situation is as follows.
When calling my batch/perl script manually
test.cmd -tasks "Task A","Task B","Task C"
everything works fine and the parameter “Task A”,”Task B”,”Task C” is treated as one unit that gets passed into another executable.
But when calling using invoke-expression
$fullCommand = "cmd.exe /C C:\test\test.cmd -tasks `"Task A`",`"Task B`",`"Task C`";
Invoke-Expression -Command $fullCommand;
The batch script thinks that each string is a separate parameter. In other words, “Task A” is the first parameter after -tasks, then “Task B”, then “Task C”, which the script doesn’t recognize how to process.
I could get around this by passing in each task name individually (e.g. -Task “Task A” -Task “Task B” -Task “Task C”), but that would require me to have extra logic to parse these, put them into an array, then reform them into a string and pass back into another executable.
Invoke-Expression takes a powershell expression. If you want to run an executable, just use the & operator. e.g.