Issue
i need to execute a bat/exe on remote Server which is not a Jenkins Slave. for which i am using windows powershell add-in as build in Jenkins.
below Invoke-Command is executing the bat file on server when executing the shell from local machine, but same is not working when running it through Jenkins Job
$user=$args[0]
$pass = $args[1]
$password = ConvertTo-SecureString $pass -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PsCredential -argumentlist $user, $password
Invoke-Command -ComputerName XXXX-Credential $cred -ScriptBlock {
start D:\XXXX\XXXX.bat
}
Solution
i solved this by passing the sessionName Parameter in Invoke-command, and using the same sessionname within script so that it will use the same session to run the script as well
Invoke-Command -ComputerName computername-Credential $cred -InDisconnectedSession -SessionName "RemoteSession" -ScriptBlock {
write-host "Start"
Get-PSSession "RemoteSession"
<<Script>>
write-host "End"
}
Answered By - Bhuvnesh Sainani