Connect to Remote Windows Server using psexec from JenkinsPipeline

Please visit my website and subscribe to my YouTube channel for more articles

https://devops4solutions.com/

In this blog, we will setup how we can execute scripts which is present on the remote windows server using Jenkins job

Prerequisite

  1. Configure Jenkins agents on the windows system.
  2. Download psexec from here
  3. Set the environment variable in Path.

Requirement

I want to run a script which is present on server “abc” from server “xyz” which needs credentials also to login to to that remote server.

Jenkins Server can be hosted either on linux/windows but the agent on which job needs to run should be be hosted on windows

The format of command as follows:

  1. accepteula — is required so that it wont prompt at run time to accept the connection.
  2. -w — provide the path where your script is present
  3. Full Path of file
psexec.exe \\%HOSTNAME% -u %USERNAME% -p “%PASSWORD%” -accepteula -w “E:\test” “E:\test\test.bat”

How to run from Jenkins Job

  1. net stop and start is required so that if there is any open connection , they will get auto deleted.
Image for post
Image for post
@echo off
net stop workstation /y
net start workstation /y
net use “\\%HOSTNAME%\E$” %PASSWORD% /user:%USERNAME%

psexec.exe \\%HOSTNAME% -u %USERNAME% -p “%PASSWORD%” -accepteula -w “E:\test” “E:\test\test.bat”
net use “\\%HOSTNAME%\E$” \delete
Image for post
Image for post

Congratulations, we have successfully setup how we can execute scripts which is present on the remote server.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store