【问题标题】:How to enable AD user's exchange and Lync accounts programmatically using LDAP in C#如何在 C# 中使用 LDAP 以编程方式启用 AD 用户的 Exchange 和 Lync 帐户
【发布时间】:2014-12-22 12:39:15
【问题描述】:

我正在使用以下代码 sn-p 在域服务器上创建一个新的 AD 用户:

DirectoryEntry newUser = directoryEntry.Children.Add("CN=" + model.Account.FullName, "user");
if (model.Account.SamAccountName != null) newUser.Properties["sAMAccountName"].Value = model.Account.SamAccountName;
newUser.CommitChanges();

setUserPassword("CN=" + model.Account.FullName + "," + model.Account.Path, model.Account.Password);

newUser.RefreshCache();

if (model.Account.FirstName != null) newUser.Properties["givenName"].Add(model.Account.FirstName);
if (model.Account.LastName != null) newUser.Properties["sn"].Add(model.Account.LastName);
if (model.Account.MiddleName != null) newUser.Properties["initials"].Add(model.Account.MiddleName);

if (model.Account.UPNLogon != null && model.Account.DomainName != null) newUser.Properties["userPrincipalName"].Add(model.Account.UPNLogon + "@" + model.Account.DomainName);
if (model.Organization.DisplayName != null) newUser.Properties["displayName"].Add(model.Organization.DisplayName);
if (model.Organization.Email != null) newUser.Properties["mail"].Add(model.Organization.Email);

newUser.Properties["LockOutTime"].Value = 0; //unlock account
newUser.Properties["userAccountControl"].Value = 0x200; // enable account
newUser.CommitChanges();

string homeMDB = profile.Exchange13_Profile.ExchangeDB;

IMailboxStore mailbox;
try
{
     IMailboxStore mailbox = (IMailboxStore)NewUser;
     mailbox.CreateMailbox(sHomeMDB);
     NewUser.CommitChanges();
}
catch (InvalidCastException e)
{
     MessageBox.Show(e.Message.ToString());
}

以上代码成功创建新用户并在AD服务器上启用。但我无法创建/启用 Exchange 邮箱,因为IMailboxStore 命名空间需要cdoexm.dll。我试图在域控制器、邮箱服务器和客户端访问服务器上找到cdoexm.dll,但还是有规律的。

我知道另一种方法是使用 Powershell cmdlet,但我不想这样做。

现在准确地陈述我的问题:

  • 如何添加COMcdoexm.dll?或者
  • 还有其他方法可以使用IMailBoxStore吗?或者
  • 除了 PowerShell 之外,还有其他方法可以启用 AD 用户的邮箱和 Lync 帐户吗?

前两个问题已得到解决,因为 CDOEXM 现在已从 Exchange 2010 及更高版本中淘汰。

【问题讨论】:

    标签: c# active-directory exchange-server lync directoryentry


    【解决方案1】:

    从 Exchange 2007 开始删除 CDOEXM。 这不是一个程序集,它是 COM。

    http://blogs.msdn.com/b/deva/archive/2010/01/13/update-technologies-not-available-with-exchange-2010-their-migration-reference-s.aspx

    做了一些搜索,我只能使用 PowerShell 找到解决方案,
    或者使用c#调用PowerShell。

    使用 C# 调用 PowerShell:

    添加“使用”:

    using System.Management.Automation;
    using System.Management.Automation.Remoting;
    using System.Management.Automation.Runspaces;
    

    调用 cmdlet:

    PSCredential newCred = (PSCredential)null;
    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://exchangeserver01.my.domain/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("Enable-Mailbox");
    command.AddParameter("Identity", user.Guid.ToString());
    command.AddParameter("Alias", user.UserName);
    command.AddParameter("DomainController", ConnectingServer);
    powershell.Commands = command;
    
    try
    {
        runspace.Open();
        powershell.Runspace = runspace;
        Collection<psobject> results = powershell.Invoke();
    }
    catch (Exception ex)
    {
        string er = ex.InnerException.ToString();
    }
    finally
    {
        runspace.Dispose();
        runspace = null;
    
        powershell.Dispose();
        powershell = null;
    }
    

    以上是从以下链接复制过来的(未测试):

    http://codingchris.com/2012/02/15/creating-exchange-2010-mailboxes-in-c/ http://codingchris.com/2014/02/11/creating-exchange-2013-mailbox-in-c/

    但是由于您想要PowerShell以外的解决方案,可能以上没有用......

    【讨论】:

    • 感谢您提供有关 CDOEXM 的信息。虽然 powershell 脚本可以解决问题,但如果有其他问题,请告诉我
    【解决方案2】:

    要为现有 AD 用户启用邮箱,请使用 enable-mailuser powershell 命令。

    要启用 Lync 帐户,请使用 enable-csuser powershell 命令。

    【讨论】:

    • 谢谢威廉。我知道 powershell 替代品,但我正在寻找更好的方法来做到这一点。
    • 我不知道有一个,尤其是就 Lync 而言。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多