Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 超出Exchange联机预算的PowerShell运行空间错误_C#_Powershell_Exchange Server_Office365_Runspace - Fatal编程技术网

C# 超出Exchange联机预算的PowerShell运行空间错误

C# 超出Exchange联机预算的PowerShell运行空间错误,c#,powershell,exchange-server,office365,runspace,C#,Powershell,Exchange Server,Office365,Runspace,我有一个应用程序,可以通过编程方式调用Exchange Server上的PowerShell命令。将应用程序连接到Exchange online server outlook.office365.com是成功的,并且可以调用单个命令,但在短时间内按顺序发送多个命令,服务器会被阻塞,并以下面的消息进行回复 我尝试使用runspace.Close和runspace.Dispose,但没有效果 从下面的消息中,我看到在60秒内最多有5个运行空间,但是我的代码关闭并处理了运行空间,这个限制不应该被忽略。

我有一个应用程序,可以通过编程方式调用Exchange Server上的PowerShell命令。将应用程序连接到Exchange online server outlook.office365.com是成功的,并且可以调用单个命令,但在短时间内按顺序发送多个命令,服务器会被阻塞,并以下面的消息进行回复
我尝试使用runspace.Close和runspace.Dispose,但没有效果
从下面的消息中,我看到在60秒内最多有5个运行空间,但是我的代码关闭并处理了运行空间,这个限制不应该被忽略。当然,我总是有可能做错事

消息(某些文本被点遮住):

下面是简化的代码(实际代码更复杂,但基本上就是这样):

private Collection GetUser(字符串用户名、字符串密码)
{
尝试
{
服务器名=”https://outlook.office365.com/powershell-LiveID?PSVersion=2.0"
SecureString pass=新SecureString();
foreach(password.ToCharArray()中的字符x)
{
pass.AppendChar(x);
}
WSManConnectionInfo connectionInfo=新的WSManConnectionInfo(
新Uri(服务器名)、PSNamespace、新PSCredential(用户名、密码));
connectionInfo.AuthenticationMechanism=AuthenticationMechanism.Basic;
connectionInfo.skipncheck=true;
connectionInfo.SkipCACheck=true;
SetData(“WSManConnectionInfo”,connectionInfo);
使用(Runspace Runspace=RunspaceFactory.CreateRunspace(connectionInfo))
{
使用(PowerShell PowerShell=PowerShell.Create())
{
尝试
{
powershell.AddCommand(“获取用户”);
powershell.AddArgument(用户名);
Open();
powershell.Runspace=运行空间;
返回powershell.Invoke();
}
捕获(例外e)
{
返回null;
}
最后
{
runspace.Close();
Dispose();
}
}
}
}
捕获(例外e)
{
返回null;
}
}
谢谢你的意见


Boris

据我所知,无论运行空间是否被再次拆除,在上述时间段内可以创建的运行空间数量都是有限的。
然而,我还没有找到任何支持这种解释的文件。

Hi@Boris你找到解决方案了吗,我遇到了完全相同的问题
Connecting to remote server failed with the following error message : Fail to create runspace because you have exceeded your budget to create runspace. Please wait for 45 seconds.
Policy: CN=GlobalThrottlingPolicy_....,CN=Global Settings,CN=ExchangeLabs,CN=Microsoft Exchange,CN=Services,CN=Configuration,DC=namprd05,DC=prod,DC=outlook,DC=com; 
Snapshot: Owner:    Sid~NAMPR05A001\.......~WSMan~false
BudgetType: WSMan
ActiveRunspaces:    0/3
Balance:    600000/1800000/-3000000
PowerShellCmdletsLeft:  199/200
ExchangeCmdletsLeft:    9223372036854775807/Unlimited
CmdletTimePeriod:   5
DestructiveCmdletsLeft: 60/Unlimited
DestructiveCmdletTimePeriod:    Unlimited
QueueDepth: Unlimited
MaxRunspaces:   5
MaxRunspacesTimePeriod: 60
LastTimeFrameUpdate:    4/23/2013 8:48:20 PM
LastTimeFrameUpdateDestructiveCmdlets:  4/23/2013 8:40:36 PM
LastTimeFrameUpdateMaxRunspaces:    4/23/2013 8:48:08 PM
Locked: False
LockRemaining:  00:00:00 For more information, see the about_Remote_Troubleshooting Help topic
private Collection<PSObject> GetUser(string userName, string password)
{
    try
    {
        serverName = "https://outlook.office365.com/powershell-LiveID?PSVersion=2.0"
        SecureString pass = new SecureString();

        foreach (char x in password.ToCharArray())
        {
            pass.AppendChar(x);
        }

        WSManConnectionInfo connectionInfo = new WSManConnectionInfo(
            new Uri(serverName), PSNamespace, new PSCredential(userName, pass));
        connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
        connectionInfo.SkipCNCheck = true;
        connectionInfo.SkipCACheck = true;



        CallContext.SetData("WSManConnectionInfo", connectionInfo);

        using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
        {
            using (PowerShell powershell = PowerShell.Create())
            {
                try
                {
                    powershell.AddCommand("Get-User");
                    powershell.AddArgument(userName);

                    runspace.Open();
                    powershell.Runspace = runspace;

                    return powershell.Invoke();

                }
                catch (Exception e)
                {
                    return null;
                }
                finally
                {
                    runspace.Close();
                    runspace.Dispose();
                }

            }
        }
    }
    catch (Exception e)
    {

        return null;
    }
}