<p>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.</p>

CC BY-SA 4.0
2024-07-17T18:14:31.840Z
2024-07-17T18:14:31.840Z
Liberto Santantón
<p>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:</p>

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

<p>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.</p>

CC BY-SA 3.0
2018-02-08T04:08:21.163Z
the only thing update.ps1 does is hg update, nothing else
Eman
CC BY-SA 3.0
2018-02-08T14:52:07.540Z
@Eman I have never used Mercurial, but it could be something to do with that command. Have you tried the debugging steps I provided?
Preston Martin
CC BY-SA 4.0
2020-12-15T21:48:06.427Z
that was the problem.
Eman
CC BY-SA 3.0
2018-02-08T02:50:31.253Z
2018-02-08T14:47:11.683Z
2018-02-08T14:47:11.683Z
Preston Martin
<p>I'm building a process to automate deploys using Jenkins and I have an issue getting the code on the server</p>

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

<pre><code>$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
</code></pre>

<p>the script always hangs at the 'web1-dev output:' portion.</p>

<p>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</p>

<jenkins><deployment><powershell>
Powershell script in jenkins is hanging
Eman
# Powershell script in jenkins is hanging

(Asked by: username_0 on 2018-02-07)

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

**Tags:** jenkins, deployment, powershell


---

## 2 answers

### Answer by username_2 on 2018-02-08. Score: 3 | Accepted answer

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.

> **Comments:**
> **username_0**: the only thing update.ps1 does is hg update, nothing else  
> **username_2**: @username_0 I have never used Mercurial, but it could be something to do with that command. Have you tried the debugging steps I provided?  
> **username_0**: that was the problem.  

---

### Answer by username_1 on 2024-07-17. Score: 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.


---

