I'm tearing my hair out trying to invoke-command but pass the path to the exe as a parameter
eg: I want to take this command
powershell Invoke-Command -ComputerName localhost -ScriptBlock { param($command ) C:\windows\system32\getmac.exe /$command } -ArgumentList ?
and translate it into a form like this
powershell Invoke-Command -ComputerName localhost -ScriptBlock { param($path, $command ) $path\getmac.exe /$command } -ArgumentList C:\windows\system32,?
I've tried all manner of quoting, ampersands and other contortions but can't get it to work. The above attempt results in
Unexpected token '\getmac.exe' in expression or statement. At line:1 char:97
(I don't really want to invoke getmac on localhost, this is the runnable, SO distilled version)
Try this option. It shows me help for
cscript.exe
.I tried other options using
&
and then path and arguments and it was giving me missing}
exception. Then usingcmd /c
instead of&
inside scriptblock fixed the issue.Powershell won't parse a string as a command that way. For e.g. if you do this:
You would get the same error. The trick to work around this is to use the invoke operator
&
:or in your example, like this (also note that for a command that you pass to the powershell executable, you must wrap it in scriptblock braces):