我有一个以编程方式在 Exchange Server 上调用 PowerShell 命令的应用程序。将应用程序连接到 Exchange 在线服务器 outlook.office365.com 成功,并且可以调用单个命令,但是在短时间内按顺序发送多个命令,服务器会阻塞并回复以下消息。
我尝试使用 runspace.Close 和 runspace.Dispose,但没有效果。
从下面的消息中,我看到在 60 秒内最多有 5 个运行空间,但是使用我的代码,我正在关闭和处理运行空间,这个限制不应该是问题。当然,我总是有可能做错了什么。
消息(一些文本被点掩盖):
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;
}
}
感谢您的任何意见。
鲍里斯