Exchange Remote Powershell gets sporadic 'Broken' state

11,091

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.

Share:
11,091
Ole K
Author by

Ole K

Web / Software - Developer by using PHP, ASP.NET C#. Also doing mobile apps for Android and iPhone Hobbies: 3D models, Security

Updated on June 26, 2022

Comments

  • Ole K
    Ole K almost 2 years

    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?

  • Ole K
    Ole K almost 9 years
    It still gives me error (see my updated question) in less than 10 minutes, I will check other SessionOptions as well - thank you
  • Vesper
    Vesper almost 9 years
    I have managed to find out that if there is any kind of network disconnects on either side of a PSSession, the session breaks. You can only debug the connectivity between your local and remote servers. A modification to the script is possible, so that if $session.state is not open, you might instruct the script to recreate session object.
  • Ole K
    Ole K almost 9 years
    I recovered that remote server has bad connection (round-trip, network latency issue) and this causes the 'Broken' state. - I will give it a last chance by using -NoEncryption and -NoCompression flag. Thanks again