headinabook
headinabook

Reputation: 383

Using psexec.exe in jenkins, handle is invalid

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?

Upvotes: 7

Views: 16280

Answers (4)

Akash Soori
Akash Soori

Reputation: 31

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
    """

Upvotes: 0

Jesper Mygind
Jesper Mygind

Reputation: 2486

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"

Upvotes: 0

headinabook
headinabook

Reputation: 383

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.

Upvotes: 6

Gurr3
Gurr3

Reputation: 11

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.

Upvotes: 0

Related Questions