WS-Management服务无法处理请求:Powershell

时间:2018-08-08 17:54:10

标签: powershell

我随机出现错误

  

WS-Management服务   无法处理该请求。允许该用户最多使用5个并发shell。关闭现有   壳或提高该用户的配额。有关更多信息,请参见about_Remote_Troubleshooting帮助主题。

我的示例代码如下

try
{
    $serverlist=server1...server100
    foreach($computer in $computers)
    {
    Enter-PSSession -ComputerName $computer

    $Ostype = (Get-WmiObject  -Class Win32_OperatingSystem  -ComputerName $computer -ErrorAction SilentlyContinue ).Caption 

    Exit-PSSession
}
    }
    Catch
    {
    [System.Windows.MessageBox]::Show($_.exception.message)
    }
    finally
    {
    Exit-pssession
    }

在执行脚本的过程中有时会发生错误,因此上述错误使我相信,我没有正确关闭我的PSsession。

有人可以告诉我是否做对了

2 个答案:

答案 0 :(得分:3)

请尝试以下代码。它不使用交互式会话,而是使用Invoke-Command cmdlet。

try
{
    $serverlist=server1...server100
    foreach($computer in $computers)
    {
    $s = New-PSSession -ComputerName $computer 
    Invoke-Command -Session $s -ScriptBlock {$os = (Get-WmiObject  -Class Win32_OperatingSystem  -ComputerName $computer -ErrorAction SilentlyContinue ).Caption}

     Remove-PSSession -Session $s
    }
}
Catch
{
    [System.Windows.MessageBox]::Show($_.exception.message)
}

答案 1 :(得分:1)

解决方案1:我们可以使用以下命令删除现有的PS会话

Get-PSSession |删除-PSSession

解决方案2:重新启动IIS Exchange Server

解决方案3:终止计算机上的所有 wsmprovhost.exe 过程

解决方案4::为用户更新限制策略,并将限制提高到更高的值,例如30,运行

Set-ThrottlingPolicy默认值*-PowerShellMaxConcurrency 30进行设置