2

I'm building a process to automate deploys using Jenkins and I have an issue getting the code on the server

I have a powershell script on the deploy server that all it does is do an hg update command.

$ciPwd = ConvertTo-SecureString $env:CI_SERVER_PWD -AsPlainText -Force
$ciCredential = New-Object System.Management.Automation.PSCredential ($env:CI_SERVER_USR, $ciPwd)
Invoke-Command -ComputerName web1-dev,web2-dev -Credential $ciCredential -ScriptBlock { C:\hg\Update.ps1 } -InDisconnectedSession -SessionName srv1,srv2

Write-Host "web1-dev output:"
Receive-PSSession -Name srv1
Remove-PSSession -Name srv1

Write-Host "web2-dev output:"
Receive-PSSession -Name srv2
Remove-PSSession -Name srv2

the script always hangs at the 'web1-dev output:' portion.

I'm kinda new to powershell scripting so its possible I'm doing something wrong, and if there's a better way to call a remote powershell script, I'm open to that

Eman
  • 123
  • 1
  • 6

2 Answers2

3

I'm guessing that your PowerShell script is hanging because of the Invoke-Command command. My best guess is PowerShell is either prompting you for something, you are passing in an argument incorrectly, or you are executing the script in your script block incorrectly. I'm not sure exactly how Script-Block works when referencing paths to PowerShell scripts, but it could be because C:\hg\Update.ps1 does not exist on the remote server. It could also be because of the commands in your "C:\hg\Update.ps1" script. Some debugging tips that I recommend:

  1. Remove your Invoke-Command line and see if the program executes.
  2. Replace the ScriptBlock command with a simpler command, like Get-Culture, command and see if it still hangs.
  3. Replace your C:\hg\Update.ps1 with another script that performs a simple command.
  4. If all of those options still hang, try using the simple script method, but with different args.

And of course as with all PowerShell remoting issues, you should check that your firewall and network settings on the remote server allow connections from Jenkins over ports 5985 and 5986.

Preston Martin
  • 3,288
  • 4
  • 18
  • 39
0

I have noticed that if your PowerShell script starts a session with some other tool (e.g., a cmd shell) without closing it before the PowerShell job/step is done, then the jenkins pipeline hangs indefinitely until that other session/process ends. In my case, I was opening a session to collect data from a third party component, failing to close the session. Once I added code to my PS script to close the session with that third party component the job/step completed successfully and the pipeline moved on to next step.