2012-04-11 100 views
1

我想在C#中創建一個Exchange郵箱。下面的代碼不會產生一個錯誤,但它也沒有出現創建郵箱,我會想到:如何創建Exchange郵箱?

private void buttonCreateUser_Click(object sender, EventArgs e) 
{ 
    Boolean Success = CreateUser(textBoxFirstName.Text, textBoxLastName.Text, 
     textBoxAlias.Text, textBoxPassword.Text, 
     comboBoxDomain.SelectedItem.ToString(), 
     comboBoxOrganizationalUnit.SelectedItem.ToString()); 

    if (Success) 
    { 
     labelStatus.Text = "User Created"; 
    } 
    else 
    { 
     labelStatus.Text = "There Is Some Error"; 
    } 

} 

public Boolean CreateUser(string FirstName, string LastName, string Alias, 
    string PassWord, string DomainName, string OrganizationalUnit) 
{ 
    string Name = FirstName + " " + LastName; 
    string PrincipalName = FirstName + "." + LastName + "@" + DomainName; 

    Boolean success = false; 
    RunspaceConfiguration rsConfig = RunspaceConfiguration.Create(); 
    SecureString spassword = new SecureString(); 
    spassword.Clear(); 

    foreach (char c in PassWord) 
    { 
     spassword.AppendChar(c); 
    } 

    PSSnapInException snapInException = null; 
    PSSnapInInfo info = rsConfig.AddPSSnapIn(
     "Microsoft.Exchange.Management.PowerShell.E2010", out snapInException); 
    Runspace myRunSpace = RunspaceFactory.CreateRunspace(rsConfig); 
    myRunSpace.Open(); 
    Pipeline pipeLine = myRunSpace.CreatePipeline(); 

    Command myCommand = new Command("New-MailBox"); 
    myCommand.Parameters.Add("Name", Name); 
    myCommand.Parameters.Add("Alias", Alias); 
    myCommand.Parameters.Add("UserPrincipalName", PrincipalName); 
    myCommand.Parameters.Add("Confirm", true); 
    myCommand.Parameters.Add("SamAccountName", Alias); 
    myCommand.Parameters.Add("FirstName", FirstName); 
    myCommand.Parameters.Add("LastName", LastName); 
    myCommand.Parameters.Add("Password", spassword); 
    myCommand.Parameters.Add("ResetPasswordOnNextLogon", false); 
    myCommand.Parameters.Add("OrganizationalUnit", OrganizationalUnit); 
    pipeLine.Commands.Add(myCommand); 
    pipeLine.Invoke(); 
    myRunSpace.Dispose(); 

    success = true; 

    return success; 
} 

我沒有得到一個錯誤,所以我不知道我是什麼做錯了。

更新

我使用的Web服務this.If我運行Windows應用程序,它的工作原理相同的代碼,但不能與web服務?我應該在Exchange Server中進行任何更改嗎?雖然我可以使用Get-MailBox獲取MailBox的信息,但New-MailBox不能創建用戶。

+0

您正在構建的POSH命令是否符合您的期望? – 2012-04-11 23:13:57

+0

@ M.Babcock用戶未添加到郵箱中 – Hiren 2012-04-11 23:18:51

+1

在嘗試使用會使問題複雜化的API動態構建它之前,手動解決POSH命令的詳細信息。 – 2012-04-11 23:20:12

回答

1

我得到inproxy.dll的這個。我變更許可水平E解決方案,並whooooo其工作的偉大...

2

我一直在掙扎就此問題了幾天(我也不知道C#作爲本週之前的語言)。無論如何,對於像我這樣的人,希望實現這一點,但掙扎,這裏是一個適用於我的例子:

我們使用Exchange2010,我可以從沒有安裝交換工具的機器運行此。

using System.Management.Automation; 
using System.Management.Automation.Remoting; 
using System.Management.Automation.Host; 
using System.Collections.ObjectModel; 
using Microsoft.PowerShell.Commands; 

static string createmailbox(string name, string alias, string email, string database, string UPN) 
     { 

      SecureString spassword = new SecureString(); 
      string PassWord = "<set default password here or read in input from form>"; 
      spassword.Clear(); 

      foreach (char c in PassWord) 
      { 
       spassword.AppendChar(c); 
      } 

      string orgunit = "<define the OU here if you always use the same one, alternatively, add as parameter in function call>"; 
      string dc = "<similarly, add DC here if needed, or call from function>"; 
      PSCredential newCred = (PSCredential)null; 
      WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("<put in CAS server FQDN here>/powershell?serializationLevel=Full"), 
       "http://schemas.microsoft.com/powershell/Microsoft.Exchange", newCred); 
      connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; 
      Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo); 
      PowerShell powershell = PowerShell.Create(); 

      PSCommand command = new PSCommand(); 
      command.AddCommand("New-Mailbox"); 
      command.AddParameter("-Name", name); 
      command.AddParameter("-Alias", alias); 
      command.AddParameter("-UserPrincipalName", email); 
      command.AddParameter("-PrimarySMTPAddress", email); 
      command.AddParameter("-Password",spassword); 
      // (ConvertTo-SecureString -AsPlainText "P4ssw0rd" -Force) 
      command.AddParameter("-Database", database); 
      command.AddParameter("-OrganizationalUnit", orgunit); 
//   command.AddParameter("-Email", email); 
      command.AddParameter("-DomainController", dc); 
      powershell.Commands = command; 
      try 
      { 
       runspace.Open(); 
       powershell.Runspace = runspace; 
       Collection<PSObject> results = powershell.Invoke(); 
       return results.ToString(); 
      } 
      catch (Exception ex) 
      { 
       string er = ex.InnerException.ToString(); 

      } 
      finally 
      { 
       runspace.Dispose(); 
       runspace = null; 

       powershell.Dispose(); 
       powershell = null; 

      } 
     } 

我的用例是通過WPF窗體,所以參數從窗體上的文本框填充。我已經設置了密碼(我們有一個共享郵箱的默認密碼,用戶帳戶被禁用),OU和域控制器爲靜態文本,但它們可以通過變量輕鬆調用。

相關問題