Redirect output of process started locally with PSExec

I'm using PSExec in a test machine environment to start the setup package of the application to be tested as administrator, and then run tests as administrator where needed. PSExec is only used for the "run as" part here, no remote execution. The call looks like this:

c:\psexec -u Administrator -p adminpassword -w C:\SystemTests C:\SystemTests\run_system_tests.cmd ...

The problem is that Jenkins, our CI server, only captures PSExec's logo in the console output, but not the actual test printout:

PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com

C:\SystemTests\run_system_tests.cmd exited with error code 0.

The started script opens its own console window locally and prints everything in there. But it's not logged in Jenkins so we won't know what the script did and why things may have failed.

Is there an option to make PSExec print the started program's output to stdout, instead of the logo, and not open a console window?


Solution 1:

I've found that you can split the PsExec output from the remote output this way ...

psexec \\wxesbbtlkS02 cmd /c C:\test.bat >tmpfile.txt 2>other.txt

so you get the PsExec output in the other.txt file, and the remote in tmpfile.txt

Solution 2:

Researching nearly this exact issue I came across this technet forum thread.

The relevant post in the thread:

post on sysinternals by user bnyffele

I actually found that there is no difference between the various systems, but it depends on how you address the remote machine:

psexec \\hostA cmd        -> seperate window opens (if command is executed on \\hostA)
psexec \\127.0.0.1 cmd    -> cmd prompt in same window
psexec \\[IP address] cmd -> cmd prompt in same window
psexec \\localhost cmd    -> results in error message about localhost being a duplicate name.
psexec \\hostA.domain.com -> cmd prompt in same window

So, it seems that I only get a separate window, when I am executing psexec for the local machine and I am using the short hostname.

It appears that if you specify the local host IP address as the machine to run the command on you will get the output. I tested this myself, and while it didn't print the standard output in real time it did print it all to the same terminal when the script finished.

If you are trying to figure out how to do this using the system name instead of the IP address you can still make it work by adding a dot to the end of the host name as determined by another user several posts later in the same thread.

post on sysinternals by user bluechipps

Actually i just discovered a workaround that is almost painless. All you have to do is add a '.' after the shortname and it will still function the same but will then output properly to the same cmd window! Note the differences.


PS C:> psexec \hal ipconfig

PsExec v1.95 - Execute processes remotely
Copyright (C) 2001-2009 Mark Russinovich
Sysinternals - www.sysinternals.com

ipconfig exited with error code 0.
PS C:>


PS C:> psexec \hal. ipconfig

PsExec v1.95 - Execute processes remotely
Copyright (C) 2001-2009 Mark Russinovich
Sysinternals - www.sysinternals.com

Windows IP Configuration

Ethernet adapter Local Area Connection 2:

Connection-specific DNS Suffix . :#####
Link-local IPv6 Address . . . . . :#####
IPv4 Address. . . . . . . . . . . :#####
Subnet Mask . . . . . . . . . . . :#####
Default Gateway . . . . . . . . . :#####

ipconfig exited on hal. with error code 0.
PS C:>


Solution 3:

if you escape the > character with a caret ^ you can pipe the output to a file. See below.

c:\psexec -u Administrator -p adminpassword -w C:\SystemTests C:\SystemTests\run_system_tests.cmd ^> outputfile.txt