2014-12-03 94 views
0

我編寫了在我們公司域中創建用戶帳戶的應用程序。我們安裝了Exchange 2010服務器,並且需要爲每個新的用戶帳戶電子郵件地址創建並啓用統一按摩。 我創建電子郵件沒有問題,但是當我嘗試啓用UMmailbox時出現錯誤,無法找到我做錯的地方。下面我的代碼部分:如何啓用UMMailbox Exchange 2010

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 

回答

0

我發現只有一個辦法如何使用啓用-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); 
       }