创建邮件后过早启用 AD 帐户

发布于 2024-12-02 15:29:02 字数 1668 浏览 1 评论 0 原文

我使用 System.DirectoryServices.AccountManagement 库创建 AD 用户帐户,然后不久之后使用 PowerShell 运行空间运行 Enable-Mailbox 命令。

当我运行此命令时,邮件启用有时会失败,并显示错误“必须为用户邮箱启用 Active Directory 帐户登录。

如果重新运行相同的命令代码,但只需尝试仅对帐户启用邮件,它就可以正常工作。再说一次,其他时候它能够创建 AD 帐户和邮件启用。

此链接表明,当 Exchange 尝试对帐户启用邮件时,AD 仍在配置该帐户:

http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/d53d91fd-c479-40e4-9791-32cb5da24721?prof=required

这是运行空间代码:

var connectionInfo = new WSManConnectionInfo(new Uri(ConfigurationManager.AppSettings["PSExchangeURI"]), ConfigurationManager.AppSettings["PSExchangeShellURI"], new PSCredential(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"].ToSecureString()));
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;

var command = new Command("Enable-Mailbox");
command.Parameters.Add("Identity", userPrincipal.UserPrincipalName);
command.Parameters.Add("Alias", userPrincipal.SamAccountName);
command.Parameters.Add("DisplayName", userPrincipal.DisplayName);
command.Parameters.Add("Database", ConfigurationManager.AppSettings["ExchangeDatabase"]);

using (var runspace = RunspaceFactory.CreateRunspace(connectionInfo)) {
    using (var pipeline = runspace.CreatePipeline()) {
        runspace.Open();
        pipeline.Commands.Add(command);

        var results = pipeline.Invoke();
    }
}

我还能做些什么来避免这个错误(除了引入线程睡眠之外)?

I'm using the System.DirectoryServices.AccountManagement library to create an AD user account, then soon after using a PowerShell runspace to run the Enable-Mailbox command.

When I run this, it is sometimes failing on the Mail-Enable with the error "Active Directory account must be logon-enabled for the user's mailbox."

If rerun the same code, but just try to Mail-Enable the account only, it works fine. And again, other times it's able to create the AD account and Mail-Enable.

This link suggests that AD is still configuring the account when Exchange tries to mail-enable it:

http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/d53d91fd-c479-40e4-9791-32cb5da24721?prof=required

Here is the runspace code:

var connectionInfo = new WSManConnectionInfo(new Uri(ConfigurationManager.AppSettings["PSExchangeURI"]), ConfigurationManager.AppSettings["PSExchangeShellURI"], new PSCredential(ConfigurationManager.AppSettings["Username"], ConfigurationManager.AppSettings["Password"].ToSecureString()));
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;

var command = new Command("Enable-Mailbox");
command.Parameters.Add("Identity", userPrincipal.UserPrincipalName);
command.Parameters.Add("Alias", userPrincipal.SamAccountName);
command.Parameters.Add("DisplayName", userPrincipal.DisplayName);
command.Parameters.Add("Database", ConfigurationManager.AppSettings["ExchangeDatabase"]);

using (var runspace = RunspaceFactory.CreateRunspace(connectionInfo)) {
    using (var pipeline = runspace.CreatePipeline()) {
        runspace.Open();
        pipeline.Commands.Add(command);

        var results = pipeline.Invoke();
    }
}

Is there something else I can do to avoid this error (besides introducing a thread sleep)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

网白 2024-12-09 15:29:02

您所看到的可能是由于复制时间延迟以及交换服务器与不同的 DC 进行通信,然后是 AD 用户创建代码。

您应该做的是将交换器和 AD 创建代码对齐以与同一个 DC 通信。

S.DS.AM 下的 PrincipalContext 对象中,从 ConnectedServer 属性中读取 DC 的 FQDN。然后将该值传递给启用邮箱 cmdlet 的 -DomainController 参数。

What you are seeing is likely to be down to replication time lag and the exchange server talking to a different DC then the AD user creation code.

What you should do is to line up exchange and your AD creation code to talk to the same DC.

From the PrincipalContext object under S.DS.AM read the DC's FQDN from the ConnectedServer property. Then pass in that value to the -DomainController parameter to the enable-mailbox cmdlet.

死开点丶别碍眼 2024-12-09 15:29:02

因此,我通过在运行时在代码中声明 $dchostname 变量来解决 DC 的“硬编码”问题。它查询域以找到合适的 DC,然后我的脚本中的所有进程都使用该域。这样,即使我更换了所有 DC,我也不必更新我的代码。

#Domain Controller Information
$dcs = (Get-ADDomainController -Filter *)
$dc = $dcs | Where {$_.OperationMasterRoles -like "*RIDMaster*"}
$dchostname = $dc.HostName

So I solve the "hard coding" issue of the DC by declaring a $dchostname variable in my code at run time. It queries the domain to find a suitable DC and then all processes in my script use that domain. This way even if I replace all my DCs, I don't have to update my code.

#Domain Controller Information
$dcs = (Get-ADDomainController -Filter *)
$dc = $dcs | Where {$_.OperationMasterRoles -like "*RIDMaster*"}
$dchostname = $dc.HostName
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文