简体   繁体   中英

Slow PowerShell execution of Microsoft Exchange 2013 commands from c#

I have the following code that executes an exchange cmdlet. It works fast with command that return some output but work slow if command has no output.

for example Invoke("Get-Mailbox") print following output:

Begin execution at 11:44:43 Finish execution at 11:44:51 Output : Administrator Discovery Search Mailbox Artem Romanchik

Execution time was about 8 second(6 second for loading exchange snappin + 2 seconds for command execution)

Slow example is Invoke("Set-Mailbox -identity tema -MaxSendSize 10MB")

Begin execution at 11:53:34 Finish execution at 11:54:36 Output :

Now it was 62 seconds(2 seconds for command and 60 seconds of waiting for something)

How can I reduce execution time of second example?

Invoke method code:

 public void Invoke(string command)
    {
        var config = RunspaceConfiguration.Create();
        PSSnapInException warning;
        config.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Snapin", out warning);

        using(var runspace = RunspaceFactory.CreateRunspace(config))
        {
            runspace.Open();

            using(var _psInstance = new RunspaceInvoke(runspace))
            {

                var psCommand = new PSCommand();
                Console.WriteLine(String.Format("Begin execution at {0}", DateTime.Now.ToLongTimeString()));

                var result = _psInstance.Invoke(command);

                Console.WriteLine(String.Format("Finish execution at {0}", DateTime.Now.ToLongTimeString()));

                var output = "";
                foreach (var line in result)
                {
                    if (line == null)
                        continue;

                    output += "\n" + line.BaseObject.ToString();
                }
                Console.WriteLine(String.Format("Output: {0}", output));

                runspace.Close();
            }
        }
    }

Set-Mailbox will accept multiple identity parameters

From Get-Help Set-Mailbox:

Identity: 
The Identity parameter specifies the mailbox. 

This parameter accepts the following values:
•Alias 
 Example: JPhillips

•Canonical DN 
 Example: Atlanta.Corp.Contoso.Com/Users/JPhillips

•Display Name 
 Example: Jeff Phillips

•Distinguished Name (DN) 
 Example: CN=JPhillips,CN=Users,DC=Atlanta,DC=Corp,DC=contoso,DC=com

•Domain\Account 
 Example: Atlanta\JPhillips

•GUID 
 Example: fb456636-fe7d-4d58-9d15-5af57d0354c2

•Immutable ID 
 Example: fb456636-fe7d-4d58-9d15-5af57d0354c2@contoso.com

•Legacy Exchange DN 
 Example: /o=Contoso/ou=AdministrativeGroup/cn=Recipients/cn=JPhillips

•SMTP Address 
 Example: Jeff.Phillips@contoso.com

•User Principal Name 
 Example: JPhillips@contoso.com

It's taking so long because it's having to search all the mailboxes looking for any of those properties in every mailbox that match the identity string you've given it.

You can speed that up by first doing a Get-Mailbox using a filter that specifies exactly which property you're using for identity. If it doesn't find the mailbox, don't try to do the set.

Beyond that, I think you'd be much better off switching to using implicit remoting instead of loading the snap in. It's much faster to set up a PS Session connection to one of the management sessions on the Exchange server and then use Invoke-Command to run the Exchange cmdlets in that session. It also eliminates the need to have the Exchange management tools installed where you're running the script and keep them updated every time you add a service pack or RU to your Exchange servers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM