Problem scenario: When using Jenkins, PsExec never seems to work when called as a batch command in a .bat file or PowerShell script. Jenkins logs an error about the PsExec command not being found.
Solution / workaround: PsExec seems to never work if called by a PowerShell script or a batch script via Jenkins. The only exception seems to be if a Scheduled Task calls a .bat file or PowerShell script that uses PsExec. First, create a Scheduled Task that calls a .bat file or PowerShell script that invokes PsExec. Second, create a Jenkins job that has a Build Step of a Windows Batch command or Windows PowerShell command (as either will suffice). Design the PowerShell or batch command to invoke the Scheduled Task, use a command like this:
SCHTASKS /RUN /TN foobar
where "foobar" is the name of the Scheduled Task that you want to run. Now the Jenkins job will call the Scheduled Task which will in turn invoke PsExec.
Alternatively, you may want to go to Services and change the user for the Jenkins service. The service will need to be restarted after you enter the credentials. Finally, PowerShell scripts are known to potentially act differently if they are executed with a local user or from a remote server. Scheduled Tasks can be a workaround if the remote execution of a PowerShell script that fails but works when it is run locally. Don Jones has experienced this problem. This problem seems to happen mostly with very complex or involved PowerShell scripts (e.g., those that install big applications).