Powershell: Execute exe on remote server and capture output

Solution 1:

Remotely execute it locally... by which I mean, execute it on the remote server in a local context, remotely, from your local machine. Well, maybe it'd just be less confusing to read on...

Sounds like your best bet here is to use PSExec to open a remote PowerShell console or command line from your local machine, which would be executing on the remote server.

So, for example:

psexec \\remoteserver cmd.exe

or

psexec \\remoteserver powershell.exe

You end up with an instance of PowerShell or cmd running on the remote server, that you'd be manipulating on your screen.

Solution 2:

Okay...this works:

invoke-command $remoteServer { param($installer)  cmd /c "$installer" /install /serviceName:TheServiceName } -ArgumentList $installer

where $remoteServer is the name of the remote server, $installer is the UNC path to the installer on that remote server, and TheServiceName is the service name.

However, I also had to supply special configuration to enable the exe to be invoked over the network, as in this article: http://through-the-interface.typepad.com/through_the_interface/2011/07/loading-blocked-and-network-hosted-assemblies-with-net-4.html

So this trick is to configure the service to be remotifiable, and then you can invoke-command it.