How to Invoke-Command and pass path to command as parameter

人走茶凉 提交于 2019-12-01 12:33:14

Try this option. It shows me help for cscript.exe.

C:\>powershell.exe Invoke-Command -ComputerName localhost -ScriptBlock { param($path, $command ) cmd /c $path $command } -args '"C:\windows\system32\cscript.exe"','"/?"'

I tried other options using & and then path and arguments and it was giving me missing } exception. Then using cmd /c instead of & inside scriptblock fixed the issue.

zdan

Powershell won't parse a string as a command that way. For e.g. if you do this:

$path="C:\Windows\System32"
$path\getmac.exe

You would get the same error. The trick to work around this is to use the invoke operator &:

&$path\getmac.exe

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):

powershell -command {Invoke-Command -ComputerName localhost -ScriptBlock { param($path, $command ) &$path\getmac.exe /$command } -ArgumentList C:\windows\system32,?}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!