gpt4 book ai didi

powershell - Exchange Remote Powershell 出现零星 'Broken' 状态

转载 作者:行者123 更新时间:2023-12-03 01:39:45 31 4
gpt4 key购买 nike

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

我偶尔会收到一个错误, session 状态已损坏。一旦它被破坏,我需要创建一个新的 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 已损坏

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>

问题:为什么会出现这个错误,如何解决?

最佳答案

您的 session 很可能超时。要进行补救,请创建一个具有增加的 session 超时值的 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 远程 session 状态的手册后,我发现 Broken 是一种异常状态,它不是由空闲或其他本地操作引起的,而是由相反,这表明所涉及的主机之间失去连接,或者在服务器端运行“wsmprovhost”出现问题。您应该调查您的操作系统和网络是否健康。脚本仍然可以处理此类错误,为此,您应该检查 session 状态,如果不是已打开,请重新打开 session 。

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

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

关于powershell - Exchange Remote Powershell 出现零星 'Broken' 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30617304/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com