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

后端 未结 2 2032
名媛妹妹
名媛妹妹 2021-01-16 03:05

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-Comma         


        
2条回答
  •  囚心锁ツ
    2021-01-16 03:49

    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,?}
    

提交回复
热议问题