PS session is in "Closed" or "Broken" state

I create a new PS session to localhost for which I’m using custom configuration, the session is called “RemoteSession” and I store it into variable for later use, ex:

Set-Variable -Name SessionInstance -Value (New-PSSession @PSSessionParams)

Session state after creation:

Id Name            Transport ComputerName    ComputerType    State         ConfigurationName     Availability
 -- ----            --------- ------------    ------------    -----         -----------------     ------------
  7 RemoteSession   WSMan     localhost       RemoteMachine   Opened        LocalFirewall.Core       Available
  3 wincompat-loca… WSMan     localhost       RemoteMachine   Opened        Microsoft.PowerShell     Available

Whenever I run Invoke-Command (and I run it a lot) I use -Session parameter to specify session to localhost, ex:

Invoke-Command -Session $SessionInstance -ScriptBlock { /* code here */ }

Calling Invoke-Command this way works up until at some point the session changes from Opened to Closed or from “Opened” to “Broken”, ex:

PS> Get-PSSession

 Id Name            Transport ComputerName    ComputerType    State         ConfigurationName     Availability
 -- ----            --------- ------------    ------------    -----         -----------------     ------------
  5 RemoteSession   WSMan     localhost       RemoteMachine   Closed        LocalFirewall.Core            None
  3 wincompat-loca… WSMan     localhost       RemoteMachine   Opened        Microsoft.PowerShell     Available

Other times it’s just “Broken” instead of “Closed”:

PS> Get-PSSession
Id Name            Transport ComputerName    ComputerType    State         ConfigurationName     Availability
 -- ----            --------- ------------    ------------    -----         -----------------     ------------
  7 RemoteSession   WSMan     localhost       RemoteMachine   Broken        LocalFirewall.Core            None
  3 wincompat-loca… WSMan     localhost       RemoteMachine   Opened        Microsoft.PowerShell     Available

My session to localhost somehow changed to “Closed” and sometimes to “Broken” state.
Because it’s closed or broken I can no longer use that session to run commands in Invoke-Command

My question is:

  1. What does “Closed” mean?
  2. What does “Broken” mean?
  3. Why does my session change from “Opened” to “Closed” or to “Broken”?
  4. How do I keep my session in “Opened” state until I manually remove it?

Note:
I do not do anythign with the session except using it for Invoke-Command

I did take a readon MS docs about what session states mean, but there is no mention of “Closed” or “Broken” state:
about Remote Disconnected Sessions - PowerShell | Microsoft Docs

I cannot explain why your sessions change the state to closed or broken. But did you try to set some session options like -MaxConnectionRetryCount or -IdleTimeout or -OperationTimeout or some other available options you can adjust with

And BTW: why are you actually using PSSessions to your localhost? That sounds unnecessary complex.

Yes but I’m using all default values, the session does not time out.

The reason why is because the function in question may work on either localhost or remote computer, to make it generic so that it works against any computer I wrapped entire function logic into Invoke-Command

With a bit of debuggin I figured out this happens when using 2 such functions in a pipeline, both functions make use of invoke-command with same session, and when I pipe them they both use the session and at some point the session get’s broken.

Also this happens on with “Core” PS edition.
It’s difficult and it will take me a lot of debugging to figure out why it happens.

If anyone can explain what “Broken” and “Closed” mean that would help a lot.

So the most proper and professional solution to me would be to create the session needed inside the function and close/remove it when the function exits.
This way the functions do not interfere with each other and their sessions.

Another very sophisticated and professional solution could be to make the functions use a session provided to them as a parameter. Just like some builtin PowerShell cmdlets do. :wink:

1 Like

Sounds reasonable!
I’m all in to do so, and will let you know how it turns out.

1 Like

I was able to debug this issue.

What was happeing is that I’m using PS compatibility session to run Windows PS commands withing PS core, problem is that newly created session needed it’s own isntance of compatiblity session which I was not able to set in session configuration

I’ve imported Windows PS modules into the session manually with help of compatibility module soon after creating of the session, and now things works as they should. ex.

Set-Variable -Name SessionInstance -Value (New-PSSession @PSSessionParams)

# This addition is what fixed the problem
Invoke-Command -Session $SessionInstance -ScriptBlock {
	if ($PSVersionTable.PSVersion -ge "7.1")
	{
		Import-WinModule -Name Appx -ErrorAction Stop
	}
}

Thanks olaf for helping!


But I still do not know what is the meaning of “Closed” and “Broken” session, there is no documentation for these states.

I’m glad you found a working solution :+1:t4: :slightly_smiling_face: but I actually think I didn’t help you at all with your actual issue. :face_with_hand_over_mouth:

I haven’t found a documentation either but I’d expect a “Broken” or “Closed” session to be useless. If the state would have been “Disconnected” you could at least try to re-connect to it with Connect-PSSession. But I actually don’t have experiences with that topic in dept … sorry.

But in a perfect world like it seems to be in the documentation there is never a status like “Closed” or “Broken:face_with_diagonal_mouth:

no you was helpful for giving me advice on how to write generic remoting function. :wink:

I think I’l ask bout broken and closed sessions on github because nowhere on the internet can be found any info.

1 Like