2015-10-27 1 views
0

J'utilise le code ci-dessous pour vous connecter à un serveur d'échange à distance et créer une boîte aux lettres dans Exchange Server 2010.utilisant C# et Powershell, comment configurer « Manager » pour boîte aux lettres créée sur le serveur Exchange 2010

public class Exchange2010 
{ 
    public string strAccountName; 
    public string strAccountPwd; 
    public string strExchange2010PSURI; 
    public string strAccountSufix; 

    private string dirPathADStafftest; 
    private string dirPathADStafftestUsr; 
    private string dirPathADStafftestpwdUsr; 

    public Exchange2010() 
    { 
     strAccountName = ConfigurationManager.AppSettings["AccountName"]; 
     strAccountPwd = ConfigurationManager.AppSettings["AccountPwd"]; 
     strExchange2010PSURI = ConfigurationManager.AppSettings["Exchange2010PSURI"]; 
     strAccountSufix = ConfigurationManager.AppSettings["AccountName"].Substring(ConfigurationManager.AppSettings["AccountName"].IndexOf("@") + 1); 

     dirPathADStafftest = ConfigurationManager.AppSettings["PathAD"]; 
     dirPathADStafftestUsr = ConfigurationManager.AppSettings["PathADUser"]; 
     dirPathADStafftestpwdUsr = ConfigurationManager.AppSettings["PathADPwd"]; 
    } 

    public Boolean AddMailbox(string mailboxName, string displayName, string alias, string password) 
    { 
     Command psCmd = new Command("New-Mailbox"); 
     var secure_password = new SecureString(); 

     foreach (char c in password) 
      secure_password.AppendChar(c); 

     mailboxName = mailboxName.Replace(" ", ""); 

     psCmd.Parameters.Add(new CommandParameter("Name", mailboxName)); 
     psCmd.Parameters.Add(new CommandParameter("DisplayName", displayName)); 
     psCmd.Parameters.Add(new CommandParameter("Password", secure_password)); 
     psCmd.Parameters.Add(new CommandParameter("Alias", alias)); 
     psCmd.Parameters.Add(new CommandParameter("PrimarySmtpAddress", mailboxName + "@xxx.edu.au")); 
     psCmd.Parameters.Add(new CommandParameter("UserPrincipalName", mailboxName + "@xxx.edu.au")); 

     Collection<PSObject> psExecResult = fnGetPSData(psCmd, null); 

     if (psExecResult.Count == 0) 
      return false; 
     else 
      return true; 
    } 

    Private Collection<PSObject> fnGetPSData(Command psCmd1, Command psCmd2) 
    { 
     Pipeline psPipeLine = null; 
     Runspace psRunSpace = null; 
     var varSecurePwd = new SecureString(); 
     try 
     { 
      foreach (var c in strAccountPwd) 
      { 
       varSecurePwd.AppendChar(c); 
      } 
      PSCredential psCreds = new PSCredential(strAccountName, varSecurePwd); 
      WSManConnectionInfo psConnInfo = new WSManConnectionInfo(new Uri(strExchange2010PSURI), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", psCreds); 
      psConnInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos; 
      psRunSpace = RunspaceFactory.CreateRunspace(psConnInfo); 

      psRunSpace.Open(); 
      psPipeLine = psRunSpace.CreatePipeline(); 

      if (psCmd1 != null) 
      { 
       psPipeLine.Commands.Add(psCmd1); 
      } 
      if (psCmd2 != null) 
      { 
       psPipeLine.Commands.Add(psCmd2); 
      } 
      Collection<PSObject> psObjects = psPipeLine.Invoke(); 

      return psObjects; 
     } 
     catch (Exception ex) 
     { 
      throw ex; 
     } 
     finally 
     { 
      if (psPipeLine != null && psPipeLine.Commands != null) 
      { 
       psPipeLine.Commands.Clear(); 
       psPipeLine.Dispose(); 
      } 

      if (psRunSpace != null) 
      { 
       psRunSpace.Close(); 
       psRunSpace.Dispose(); 
      } 
     } 

    } 
} 

Tout fonctionne parfaitement et la boîte aux lettres est créée.

Maintenant, je veux définir le champ Manager dans l'onglet Organisation pour la boîte aux lettres à travers ce code. Y a-t-il un moyen de l'atteindre? Manager field in Organization tab - Screenshot

Répondre

0

Vous devez obtenir l'utilisateur que vous souhaitez être le gestionnaire de l'utilisateur créé, obtenir son attrubute distinguishedName et définir l'attribut manager de l'utilisateur créé à cette valeur. Vous n'avez pas besoin du côté serveur Exchange pour obtenir l'effet.

$user=get-aduser "New user" # or create 
$manager=get-aduser "Manager" 
$user.manager = $manager.DistinguishedName 
set-aduser -instance $user