7

I am using Jenkins on a Windows7 system. I would like to use it to execute a batch script on a remote Windows system. The batch script will be used to flash a development board and run some tests. I came across psexec.exe. That works well through a command prompt window--I can connect and run the script without any issues, but when I try to have Jenkins do it, I get the following output:

PsExec v2.11 - Execute processes remotely
Copyright (C) 2001-2014 Mark Russinovich
Sysinternals - www.sysinternals.com

The handle is invalid.
Connecting to ABCDEFG...

Couldn't access ABCDEFG:
Connecting to ABCDEFG...

Build step 'Execute Windows batch command' marked build as failure

The command I am using in both cases is: psexec.exe \\ABCDEFG -u "DOMAIN\username" -p "password" "C:\test.bat"

The user associated with username has administrator privileges on the remote system (ABCDEFG is not the real name of the system).

Can anyone help me figure out why it is not working through Jenkins? Or, is there an easier/better way to execute a batch script on a remote Windows system through Jenkins?

headinabook
  • 373
  • 3
  • 6
  • 14
  • If you are logged onto the build server, are you able to successfully invoke the above Windows Batch command? Does the user running as the Jenkins instance have access to the `ABCDEFG` computer you are trying to access? – Anthony Forloney Aug 08 '14 at 23:21
  • The user that is logged onto the Windows system running Jenkins has access to the remote system (ABCDEFG). They can successfully run that batch command from the command line on the system running Jenkins, but it will not work when they have Jenkins execute the command. – headinabook Aug 09 '14 at 00:05
  • Does this behaviour occur all the time through Jenkins or does it succeed sometimes? – Anthony Forloney Aug 09 '14 at 00:12
  • It occurs all the time. I've never seen it succeed though Jenkins. – headinabook Aug 09 '14 at 00:26
  • I can't say that I have run into this issue with my experience in Jenkins, normally problems like these boil down to permissions issue involving the user running the Jenkins instance. There are suggested questions regarding the `Invalid handles` message using `psexec` which seem to suggest alternative method. I would suggest giving those a try to see whether you have better success. – Anthony Forloney Aug 09 '14 at 00:28
  • I've looked at some of the other suggested questions. I am an administrator on both systems. The remote system has not exceeded its maximum number of users logged in. I have tried using psexec.exe with the -s option and got the same response. It fails every time. – headinabook Aug 09 '14 at 00:48
  • From what i understand, the command runs successfully from command line with user _username_ but it fails when it's run through Jenkins with the same user _username_, correct? It sounds silly to ask but how have you ascertained that Jenkins is running with the same user, _username_? – Technext Aug 09 '14 at 05:02
  • I hope you've installed Jenkins as a Windows service and not running as a jar every time. If that's the case, you should first ensure that the `Log on as` is set to the same user as the one who is able to run command successfully from command prompt. – Technext Aug 10 '14 at 04:31
  • I did install Jenkins as a Windows service and do not run it as a jar every time. I attempted to set the username each time the project ran by running "set %USERNAME%=myusername" but that did not work either. I'll try modifying the "Log on as" and see if that solves my problem. – headinabook Aug 11 '14 at 14:47

4 Answers4

6

Thanks to all your help, especially Technext, I have a solution.

I needed run "services.msc", find "Jenkins", right click on it, and go to "Properties". Once the properties windows appeared, I had to click the "Stop" button to stop Jenkins, open the "Log On" tab, enter in my username and password (the username I used when running through command prompt), and start Jenkins again. That got rid of the "handle is invalid" message in Jenkins.

Update: A better solution was to go onto the system I was using psexec.exe to get onto, go to Control Panel > User Accounts > Give other users access to this computer. Click on "Add..." and type in the username and domain Jenkins uses to run its commands (to find this, open your Jenkins in a browser window, go to Manage Jenkins > System Information and look for USERNAME and USERDOMAIN under Environment Variables). Make sure you give it Administrator rights. Then click ok. Now psexec.exe shouldn't have the "handle is invalid" issue.

headinabook
  • 373
  • 3
  • 6
  • 14
  • Had a very similar issue, the service account that was running Jenkins was already an indirect member (via another security group) of the local Administrator group on the remote machine, however I had the exact same error. Adding the Jenkins Service account directly to the local Administrator group solved the issue for me. – Wouter van Rij Jul 04 '19 at 06:41
  • @headinabook Hi, I don't know how to find 'Give other users access to this computer' on windows server 2012 R2, could you tell me? – Po-Sen Huang Jan 14 '21 at 02:08
0

Sorry, I don't have enough reputation for comments, but is the single \ a typo? Since

The handle is invalid.

probably means that the computer address is invalid. Try

 psexec.exe \\ABCDEFG -u "DOMAIN\username" -p "password" "C:\test.bat"

Notice the two backslashes to access a locally mapped computer.

otherwise if that does not work i recommend the @ tag

psexec.exe @servername.txt -u "DOMAIN\username" -p "password" "C:\test.bat"

where @servername.txt is a textfile containing only the servernames, one per line. The file parameter handles the formatting of \

ex servername.txt

ABCDEFG
COMPUTER2

EDIT: also found after some quick googling that it can be related to windows security.

Community
  • 1
  • 1
Gurr3
  • 11
  • 1
  • 2
  • The @ tag with the txt file did not work. I got the same "handle is invalid" response. – headinabook Aug 10 '14 at 01:09
  • can the user running jenkins do the command, since I had some problems where psexec fails with user even though `-u domain\user` was correct. As i have now the user running jenkins does `-u domain\user` with itself as user. It was something about psexec first trying with the user it runs as, and then trying with the given credentials, but failing at first and giving up, don't remember the exact reason. – Gurr3 Aug 10 '14 at 11:23
0

Check out that a simple restart of the remote machine doesn't solve the problem. Also, adding parameters -h and -accepteula may help. Modified command:

psexec.exe \\ABCDEFG -u "DOMAIN\username" -p "password" -h -accepteula "C:\test.bat"

Jesper Mygind
  • 2,406
  • 1
  • 18
  • 20
0

I execute below code from Jenkins pipeline groovy script to connect dynamically created VM as a resource on Jenkins master. Below code connect dynamically created VM as resource on Jenkins master with 4 executors. You can change the number of executors based on your requirement.

    bat label: 'ConnectResource', script: """
    @echo OFF

    C:\\apps\\tools\\psexec \\\\${machine_ip} -u ${machine_ip}\\${machine_username} -p ${machine_password} -accepteula -d -h -i 1 cmd.exe /c "cd C:\\apps\\jenkins\\ & java -jar C:\\apps\\jenkins\\swarm.jar -master http://pnlv6s540:8080 -username ${jenkins_user_name} -password ${jenkins_user_password} -name ${machine_ip}_${BUILD_NUMBER} -labels ${machine_ip}_${BUILD_NUMBER} -deleteExistingClients -disableClientsUniqueId -executors 4" & ping 127.0.0.1 -n 60 > nul
    """