如何启用UMMailbox Exchange 2010

时间:2014-12-03 13:57:28

标签: c# exchange-server-2010

我在公司域中编写用于创建用户帐户的应用程序。我们已经安装了Exchange 2010服务器,我需要为每个新的用户帐户电子邮件地址创建并启用统一按摩。 我没有问题地创建电子邮件,但是当我尝试启用UMmailbox时,我收到了一个错误,但是找不到我做错了什么。我的代码的一部分:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;


private string RunLocalExchangePowerShell(string script)
    {
        // create the runspace and load the snapin
        RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
        PSSnapInException snapInException = null;
        Runspace runSpace = RunspaceFactory.CreateRunspace(rsConfig);
        runSpace.Open();
        rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapInException);

    string DN = @"CN=User Name,OU=Company Users,DC=local,DC=contoso,DC=com";

        Command enableUMMB = new Command("Enable-UMMailbox");
        enableUMMB.Parameters.Add("Identity", DN);
        enableUMMB.Parameters.Add("PinExpired", false);
        enableUMMB.Parameters.Add("UMMailboxPolicy", "UM2 Default Policy");
        enableUMMB.Parameters.Add("IgnoreDefaultScope");

        Pipeline enableUMMailbox = runSpace.CreatePipeline();

        enableUMMailbox.Commands.Add(enableUMMB);

        Collection<PSObject> enabelUMmaiiboxResults = enableUMMailbox.Invoke();

        enableUMMailbox.Dispose();
        runSpace.Close();
    }

错误如下:

“Microsoft.Exchange.UM.UMCommon.UmGlobals”的类型初始化程序引发了异常。

如果我在Powershell控制台中使用上面的命令UMmailbow创建没有任何问题。 在powerShell控制台中,我使用了以下字符串:

Enable-UMMailbox -Identity "CN=User Name,OU=Company Users,DC=local,DC=contoso,DC=com" -PinExpired $false -UMMailboxPolicy "UM2 Default Policy" -IgnoreDefaultScope

1 个答案:

答案 0 :(得分:0)

我发现只有一种方法可以使用Enable-UMMailbox命令行开关。我通过URI连接到Exchange服务器。我的代码的部分内容:

string exchangePowershellRPSURI = "http://my.domain/powershell?serializationLevel=Full";

    PSCredential credentials = (PSCredential)null;
    //Provides the connection information that is needed to connect to a remote runspace
    // Prepare the connection           
    WSManConnectionInfo connInfo = new WSManConnectionInfo((new Uri(exchangePowershellRPSURI)),
        "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credentials);
    connInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; 
    connInfo.SkipCACheck = true;
    connInfo.SkipCNCheck = true;
    connInfo.SkipRevocationCheck = true;

    // Create the runspace where the command will be executed           
    Runspace runspace = RunspaceFactory.CreateRunspace(connInfo);

    // Add the command to the runspace's pipeline           
        runspace.Open();
        try
                {
                    Command enableUMMB = new Command("Enable-UMMailbox");
                    enableUMMB.Parameters.Add("Identity", UserPrincipalName);
                    enableUMMB.Parameters.Add("PinExpired", false);
                    enableUMMB.Parameters.Add("UMMailboxPolicy", "UM2 Default Policy");

                    Pipeline enableUMMailboxPipiLine = runspace.CreatePipeline();
                    enableUMMailboxPipiLine.Commands.Add(enableUMMB);
                    enableUMMailboxPipiLine.Invoke();
                }
                catch (ApplicationException e)
                {
                    MessageBox.Show("Unable to connect to UMMailbox.\n Error:\n" + e.Message,
                        "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }