2015-06-03 19 views
1

我试图通过Windows Powershell从远程Exchange Server接收所有TransportAgents的状态。Exchange远程Powershell获取零星'Broken'状态

我偶尔会收到一个错误,那就是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 

UPDATE

在SessionOption添加的IdleTimeout后我recev荷兰国际集团下面的错误,然后会话被打破

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> 

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

回答

2

很可能您的会话超时。要进行补救,请创建一个具有增加的会话超时值的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”的问题。你应该调查你的操作系统和网络是否健康。脚本仍然可以处理这样的错误,要做到这一点,你应该检查会话状态,如果它不是Opened,重新打开会话。

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

不过,如果重新连接尝试将抛出一个超时,然后拯救你的脚本,你真的不能做任何更多的远程无连接。

+0

它仍然给我错误(请参阅我更新的问题)在不到10分钟,我会检查其他SessionOptions以及 - 谢谢 –

+0

我设法发现,如果有任何形式的网络断开任何一方一个PSSession,会话中断。您只能调试本地和远程服务器之间的连接。可以对脚本进行修改,以便如果'$ session.state'不是'open',则可以指示脚本重新创建会话对象。 – Vesper

+1

我恢复了远程服务器连接不良(往返,网络延迟问题),这导致'Broken'状态。 - 我将通过使用-NoEncryption和-NComCompression标志给它最后的机会。再次感谢 –