Run shell script on multiple windows server from jenkins (linux)

Hi All, I am using jenkins on linux server, i have added windows as node to Jenkins, When now i need execute shell script on 4 windows server through jenkins pipeline, but it only execute the script on my windows agent, instead of other 4 servers, as we have option for linux serve to ssh and execute the command is there any way that we can rdp or connect windows server and execute script on those windows server?

1 Like

Same question from my side. Please provide a batter solution for this .

Hello and welcome to this community, @Shashank_shank. :wave:

Yes, you can execute scripts on remote Windows servers from Jenkins.
However, unlike Linux, Windows does not support SSH natively. You have a few options:

  1. Use SSH: Windows Server 2019 and later versions support OpenSSH. If you’re using one of these versions, you can enable OpenSSH and then use the ssh command in your Jenkins pipeline to connect to the Windows servers and execute your script.
  2. Use PowerShell Remoting: PowerShell Remoting allows you to run PowerShell commands or scripts on a remote Windows machine. You can use the Invoke-Command cmdlet to execute your script on the remote servers.

Here’s an untested example of how you could maybe use PowerShell Remoting in a Jenkins pipeline:

pipeline {
    agent any
    stages {
        stage('Run script on remote Windows servers') {
            steps {
                script {
                    def servers = ['server1', 'server2', 'server3', 'server4']
                    for (server in servers) {
                        powershell """
                            \$cred = New-Object System.Management.Automation.PSCredential ('username', (ConvertTo-SecureString 'password' -AsPlainText -Force))
                            Invoke-Command -ComputerName ${server} -Credential \$cred -FilePath C:\\path\\to\\your\\script.ps1
                        """
                    }
                }
            }
        }
    }
}

Please note that PowerShell Remoting may need to be enabled on the remote servers (depending on the Windows version) for this to work. You can enable it by running Enable-PSRemoting in a PowerShell session with administrator privileges on each of the remote servers.

There are Jenkins plugins available that can execute commands or scripts on remote Windows machines, such as the PowerShell plugin or the SSH plugin (if OpenSSH is enabled on the Windows servers).

Remember to secure your credentials.
You should definitely avoid hardcoding credentials in your Jenkins pipeline, so use Jenkins credentials or secret text variables to securely store your credentials.