Exchange 远程 Powershell 出现零星的“损坏”状态

Ole*_*e K 3 powershell exchange-server powershell-remoting

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

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

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

# 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>
Run Code Online (Sandbox Code Playgroud)

当我重复该命令时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
Run Code Online (Sandbox Code Playgroud)

更新

在 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>
Run Code Online (Sandbox Code Playgroud)

问题:为什么会出现错误以及如何解决?

Ves*_*per 7

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

$so = New-PSSessionOption -IdleTimeout 600000
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<SERVER>/PowerShell/ -Authentication Default -SessionOption $so
Run Code Online (Sandbox Code Playgroud)

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

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

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

尽管如此,如果重新连接尝试会引发超时,那么请退出您的脚本,因为您无法在没有连接的情况下真正进行任何远程处理。