I am trying to write a PowerShell script with a Script Block that is to be passed to Invoke-Command. The script block needs to invoke an executable whose path is determined by a PowerShell variable ($bar in this example)
However, when I try to do this, I get the following exception:
At C:\Users\foo\test.ps1:4 char:13
+ $cmd3={$bar "param"}
+ ~~~~~~~
Unexpected token '"param"' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken
test.ps1 consists of these lines:
$bar="foo.exe"
$cmd1={foo.exe "param"}
$cmd2={$bar}
$cmd3={$bar "param"}
It seems a variable $bar can be used, but only if there are no arguments (cmd2). A literal foo.exe can be used even if there are arguments (cmd1)
I want to use a variable reference for the executable name and more than 0 arguments (cmd3), but this case is not allowed, it seems.
Why?