I tried with runspace.Close and runspace.Dispose, but it has no effect.
From the message bellow, I see there is a maximum of 5 runspaces in 60 sec period, but with my code I am closing and disposing runspaces and that limit shouldn't be and issue. Of course, there is always a chance that I am doing something wrong.
message (some text is masked with dots):
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 PowerShellCmdlets199/200 ExchangeCmdlets9223372036854775807/Unlimited CmdletTimePeriod: 5 DestructiveCmdlets60/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
Here is the simplified code (actual is more complex, but basically this is it):
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;
}
}Thanks,
Boris