Exchange Remote Powershell 获取零星 'Broken' 状态

Exchange Remote Powershell gets sporadic 'Broken' state

我正在尝试通过 Windows Powershell 从远程 Exchange 服务器接收所有 TransportAgent 的状态。

我偶尔会收到一个错误,即 The session state is Broken。一旦它坏了,我需要创建一个新的 Session

下面是我正在使用的命令列表(按正确顺序排列)

# 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>

当我重复命令 Invoke-Command $Session {Get-TransportAgent} 时,偶尔会出现以下错误:

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

更新

在 SessionOption 中添加 IdleTimeout 后,我​​收到以下错误,然后是 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>

问:为什么会报错,如何解决?

您的会话很可能超时。要补救,请创建一个具有增加的会话超时值的 PSSessionOption 对象,然后您可以将其提供给 New-PSSession cmdlet。默认超时为 60000 毫秒(1 分钟),如果您是 运行 包含异步请求的扩展脚本或工作时间很长,则该超时可能非常小。

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

SessionOption 对象将使您的 $session 在上次命令执行后保持打开状态 10 分钟。

编辑: 阅读有关 Powershell 远程会话状态的手册后,我发现 Broken 是一种异常状态,它不是由空闲或其他本地操作引起的,而是表明所涉及主机之间的连接丢失,或服务器端出现问题 运行 "wsmprovhost"。您应该调查 OS 和网络是否健康。脚本仍然可以处理此类错误,为此您应该检查会话状态,如果不是 Opened,请重新打开会话。

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

不过,如果重新连接尝试会引发超时,那么请退出您的脚本,因为如果没有连接,您真的无法再进行任何远程处理。