2

I have an application that programmatically invokes PowerShell commands on Exchange Server. Connecting the app to Exchange online server outlook.office365.com is successful, and invoking individual commands works, but sending several commands in a sequence in a short period of time, server becomes clogged and replies with the message bellow.
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
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

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 for any input.

Boris

Boris R.
  • 371
  • 4
  • 11

1 Answers1

1

As I've understood this there is a limit as to how many runspaces can be created during said time period, irrespective of runspaces being torn down again or not. I have not, however, been able to find any documentation to back this interpretation.

WeekendHacker
  • 196
  • 1
  • 6