Wednesday, October 03, 2007

Using PsExec to Execute PowerShell on a Remote System and Return "PowerShelly" Results

Precision Computing - Using PowerShell and PsExec to invoke expressions on remote computers

"While eagerly awaiting PowerShell’s upcoming remoting functionality, many people turn to Sysinternals’ PsExec tool to build their own version. However, PowerShell seems to hang when called via PsExec on the remote machine. This has come up on the SysInternal forum (http://forum.sysinternals.com/forum_posts.asp?TID=10823) among other places, and is caused by the same issue outlined here: http://www.leeholmes.com/blog/UsingMshexeInteractivelyFromWithinOtherPrograms.aspx.

To work around this problem, you can give some input to the Powershell process. But to give it input, you need to use cmd.exe:

psexec \\server cmd /c "echo . | powershell dir 'c:\program files'"

...

And to make it even more PowerShelly, the –OutputFormat of XML lets you get back an XML representation of your command’s output. On your local system, PowerShell converts your output back to deserialized objects. From there, you can continue to manipulate the output with the object-oriented goodness you’ve come to expect of us... "

I thought this was pretty darn cool. Cool enough that I immediately had to try it out... :)

In short, this post shows you how to use PsExec to run PowerShell on a remote computer returning the results to a local instance of PowerShell. (PsExec and PowerShell needs to be installed on the local machine and PowerShell on the remote machine).

Why would you want to do this?

I'm thinking aggregating information and/or doing stuff across a number of machines and providing centralized/combined results.

Hum... Now I'm thinking about all the cool stuff I can do with this... :)

No comments: