0

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?

jonseymour
  • 1,006
  • 1
  • 12
  • 22
  • *inside a script block* Does this part actually matters? Did you get any different result if you just write `$bar "param"` without wrapping it in script block? – user4003407 Jul 16 '18 at 05:15
  • @PetSerAl - yes, you are correct. This is not an issue with script block's per se, but with the PowerShell command invocation syntax in general. You suggested answer above contained the solution. Specifically I need to declare the command as `& $bar "param"` or the script block as `{& $bar "param"}` Thanks for your help! – jonseymour Jul 16 '18 at 05:42

0 Answers0