Search code examples
powershellexchange-serverpowershell-remoting

Exchange Remote Powershell gets sporadic 'Broken' state


I am trying receive the status of all TransportAgents from an remote Exchange Server through Windows Powershell.

I sporadically receive an error, that the The session state is Broken. Once it is broken I need to create a new Session

Below a list of the commands I am using (in proper order)

# Build the PSSession for Exchange
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<SERVER>/PowerShell/ -Authentication Default
# Invoke the command 'Get-TransportAgent' on the remote PSSession
Invoke-Command $Session {Get-TransportAgent}
# Result when there is NO ERROR
Identity                                           Enabled         Priority        PSComputerName
--------                                           -------         --------        --------------
Transport Rule Agent                               True            1               <SERVER>
Malware Agent                                      True            2               <SERVER>
Text Messaging Routing Agent                       True            3               <SERVER>
Text Messaging Delivery Agent                      True            4               <SERVER>

When I repeat the command Invoke-Command $Session {Get-TransportAgent} the following error occured sporadically:

Invoke-Command : Because the session state for session Session9, <GUID-REMOVED>, <SERVER> is not equal to Open, you cannot run a command  in the session.  The session state is Broken. At line:1 char:1

UPDATE

After adding the IdleTimeout in SessionOption I'm receving the below error followed by Session is Broken

Invoke-Command $Session {Get-TransportAgent}
Starting a command on the remote server failed with the following error
message : The I/O operation has been aborted because of either a thread exit or an application request. For more information, see the about_Remote_Troubleshooting
Help topic.
+ CategoryInfo          : OperationStopped: (<SERVER>:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : JobFailure
+ PSComputerName        : <SERVER>

Question: Why does the error occurs and how to solve this?


Solution

  • Most likely your session times out. To remedy, create a PSSessionOption object with increased session timeout value, which you can then supply to New-PSSession cmdlet. The default timeout is 60000 milliseconds (1 minute), which might be pretty small if you are running an extensive script which contains asynchronous requests or just works really long.

    $so = New-PSSessionOption -IdleTimeout 600000
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<SERVER>/PowerShell/ -Authentication Default -SessionOption $so
    

    This SessionOption object will make your $session remain open for 10 minutes after last command execution.

    EDIT: After reading manuals on Powershell remote session states, I have discovered that Broken is an abnormal state which does not arise from idling or other local actions, but is rather an indication of loss of connectivity between hosts involved, or of problems running "wsmprovhost" on the server side. You should investigate if your OS and network is healthy. A script can handle such errors still, to do it you should check session state, and if it's not Opened, reopen the session.

    if ($session.state -ne "Opened") { 
        Write-Host "The session went into $($session.state) state! Reinitializing!"
        $session=New-PSSession <arguments skipped>
    }
    

    Still, if reconnection attempt would throw a timeout, then bail out of your script as you can't really do any more remoting without a connection.