How do you correctly exit a remote job in a PowerShell session
Asked Answered
O

2

6

I have a number of remote jobs started with a Powerhsell invoke-command -session command. In some cases, at least one of the remote child job exits, but the local PS session state reports the jobs as "Running" and the session hangs waiting for the job to finish.

Is this a known bug in PSv2? Is there a workaround or technique to allow PS to correctly detect when a remote child job exits?

Ordovician answered 17/6, 2015 at 0:37 Comment(2)
Do you have any code to show what objects, or properties you are looking at? Have you tried to look at the session's runspace? [System.Management.Automation.RemoteRunspace] AvailabilityChanged event?Robbyrobbyn
How do you determine that the child job exited?Adp
H
1

How about: Remove-PSSession

Get-PSSession | Remove-PSSession
Remove-PSSession -Session (Get-PSSession)
$s = Get-PSSession
Remove-PSSession -Session $s

OR

$r = Get-PSSession -ComputerName Serv*
$r | Remove-PSSession

Powershell Core: Remove-PSSession

Hyla answered 10/7, 2019 at 7:6 Comment(0)
M
0

When you are using Invoke-Command -Session I believe you will need to use the Get-PSSession command to get the updated status. From Technet:

-Session

Runs the command in the specified Windows PowerShell sessions (PSSessions). Enter a variable that contains the PSSessions or a command that creates or gets the PSSessions, such as a New-PSSession or Get-PSSession command.

Maddux answered 29/7, 2015 at 13:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.