Search code examples
c#powershellpowershell-remoting

Powershell Remoting C# invoke-expression


As a result of this approach not working, I am trying a very convoluted way of getting complete route information on a remote machine. I am trying to connect to a remote powershell runspace and execute the route.exe command line tool that way and return the information.

Essentially I am applying the solution found here except I am trying to run the command: Invoke-Expression "c:\WINDOWS\system32\route.exe print" instead of "get-process"

When I use "get-process" it works. When I try Invoke-Expression I get an exception telling me that this command-let cannot be found. When I run the exact same command on the machine locally, it works and the routing information is displayed. So I know it's not a syntax problem or a typo in the path. Does anyone know why this is happening?

Thanks.


Solution

  • Okay, so as soon as I posted this I had a brilliant, but obvious, idea. Technically the "Invoke-Expression" is the 'command' and the "c:\WINDOWS\system32\route.exe print" is an argument of the command. So you need to add this to the code that I linked to above:

    powershell.AddCommand(scriptText); 
    powershell.AddArgument(args);
    

    Where scriptText is the "Invoke-Expression" and args is "c:\WINDOWS\system32\route.exe print".

    Hope this helps someone!