使用C#和Powershell,如何设置';经理';用于在Exchange服务器2010上创建的邮箱

本文关键字:服务器 Exchange 用于 2010 创建 经理 何设置 设置 使用 Powershell | 更新日期: 2023-09-27 18:19:28

我正在使用以下代码远程连接到exchange服务器,并在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();
            }
        }
    }
}

一切都很完美,邮箱也就创建好了。

现在,我想通过此代码为邮箱设置"组织"选项卡中的"经理"字段。有办法实现吗?组织选项卡中的经理字段-屏幕截图

使用C#和Powershell,如何设置';经理';用于在Exchange服务器2010上创建的邮箱

您需要获取想要成为创建用户的管理器的用户,获取其distinguishedName属性,并将要创建的用户的manager属性设置为该值。您不需要Exchange服务器端即可实现此效果。

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