Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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# 从c执行Microsoft Exchange 2013命令的PowerShell执行缓慢#_C#_Powershell_Runtime_Exchange Server - Fatal编程技术网

C# 从c执行Microsoft Exchange 2013命令的PowerShell执行缓慢#

C# 从c执行Microsoft Exchange 2013命令的PowerShell执行缓慢#,c#,powershell,runtime,exchange-server,C#,Powershell,Runtime,Exchange Server,我有以下执行exchange cmdlet的代码。它在返回一些输出的命令中工作得很快,但在命令没有输出的情况下工作得很慢 比如说 Invoke(“获取邮箱”) 打印以下输出: 在11:44:43开始执行 11:44:51完成执行 输出: 管理员 发现搜索邮箱 Artem Romanchik 执行时间约为8秒(加载exchange snappin需要6秒,执行命令需要2秒) 缓慢的例子是Invoke(“设置邮箱-标识tema-MaxSendSize 10MB”) 在11:53:34开始执行 11:

我有以下执行exchange cmdlet的代码。它在返回一些输出的命令中工作得很快,但在命令没有输出的情况下工作得很慢

比如说
Invoke(“获取邮箱”)
打印以下输出:

在11:44:43开始执行
11:44:51完成执行
输出:
管理员
发现搜索邮箱
Artem Romanchik

执行时间约为8秒(加载exchange snappin需要6秒,执行命令需要2秒)

缓慢的例子是
Invoke(“设置邮箱-标识tema-MaxSendSize 10MB”)

在11:53:34开始执行
11:54:36完成执行
输出:

现在是62秒(2秒命令,60秒等待)

如何减少第二个示例的执行时间

调用方法代码:

 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();
            }
        }
    }

设置邮箱将接受多个标识参数

从获取帮助集邮箱:

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
这需要很长时间,因为它必须搜索所有邮箱,在每个邮箱中查找与您提供的标识字符串匹配的任何属性

您可以通过首先使用一个过滤器执行Get-Mailbox操作来加快速度,该过滤器精确地指定您要使用哪个属性作为标识。如果找不到邮箱,请不要尝试进行设置

除此之外,我认为您最好改用隐式远程处理,而不是加载管理单元。在Exchange服务器上设置到其中一个管理会话的PS会话连接,然后使用Invoke命令在该会话中运行Exchange cmdlet要快得多。它还消除了在运行脚本的位置安装Exchange管理工具并在每次向Exchange服务器添加service pack或RU时对其进行更新的需要