【问题标题】:C#: Creating a computer object in AD failsC#:在 AD 中创建计算机对象失败
【发布时间】:2021-12-27 15:39:45
【问题描述】:

我创建了一个工具箱来在各种管理系统(包括 Active Directory)中创建计算机。这个工具箱多年来一直完美无缺。自本月起,在 Active Directory 中创建计算机对象不再起作用。我仍在寻找原因,但似乎与域控制器上的补丁有关。 创建对象的用户没有任何更改。 当我使用以下命令手动创建具有用户凭据的客户端时,它可以正常工作。

New-ADComputer -Name NB89991 -Path "ou=nb,ou=w10,ou=clt,ou=tier2,ou=central,dc=xy-dom,dc=xy,dc=ch" -SAMAccountName NB89991

你知道我的代码有什么问题吗?

 public Boolean createComputerAccount(Client computer){
        Boolean returnValue = true;
        try {
            if (!existComputer(computer))
            {

                PrincipalContext oPrincipalContext = GetPrincipalContext(computer.ou);
                ComputerPrincipal computerPrincipal = new ComputerPrincipal(oPrincipalContext);
                computerPrincipal.SamAccountName = computer.name;
                computerPrincipal.Name = computer.name;
                if (!(computer.adDescription == "")) {
                    computerPrincipal.Description = computer.adDescription;
                }

                
                MessageBox.Show(computerPrincipal.SamAccountName, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                MessageBox.Show(computerPrincipal.Name, "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                computerPrincipal.Enabled = true;
                computerPrincipal.Save();
                returnValue = true;
            }
            else {
                returnValue = false;
            }
        }catch (Exception e){
            errorMessage = "Creating a computer in Active Directory failed!\r\nPlease contact the ITCM TEAM or check the Logfile:\r\n (c:\\temp\\ClientToolbox.log).";
            createErrorMessage(errorMessage, e);
            returnValue = false;
        }
     return returnValue;
    }

    public PrincipalContext GetPrincipalContext(string sOU) {
        PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, domain, sOU, ContextOptions.SimpleBind, ntUser, ntUserPWD);
        return oPrincipalContext;
    }

错误信息:

27.12.2021 16:19:46 System.UnauthorizedAccessException:访问被拒绝。

在 System.DirectoryServices.AccountManagement.ADStoreCtx.Insert(Principal p) 在 System.DirectoryServices.AccountManagement.Principal.Save() 在 C:\Temp\Win10Toolbox\Source\AZToolboxClient\MyClasses\ActiveDirectory.cs:line 71 中的 ClientToolbox.ActiveDirectory.createComputerAccount(Client computer)

非常感谢! 最好的祝福 伊尼克

【问题讨论】:

    标签: c# active-directory


    【解决方案1】:

    KB5008102 这么说:

    其 UserAccountControl 属性包含 UF_WORKSTATION_TRUST_ACCOUNT 标志的计算机帐户的 sAMAccountName 必须以单个美元符号 ($) 结尾。

    所以你需要将$ 添加到SamAccountName

    computerPrincipal.SamAccountName = computer.name + "$";
    

    【讨论】:

    • 谢谢加布里埃尔。这就是解决方案。您知道为什么以下命令不需要末尾的“$”吗? New-ADComputer -Name NB89991 -Path "ou=nb,ou=w10,ou=clt,ou=tier2,ou=central,dc=xy-dom,dc=xy,dc=ch" -SAMAccountName NB89991
    • New-ADComputer 为您添加 $
    【解决方案2】:

    更新:2021 年 12 月 27 日: 我认为由于以下 Microsoft 文章而出现此问题。 我还没有得到如何改变我的代码,它可以再次工作。大家都知道吗?

    Microsoft KB5008102

    【讨论】:

    • 这不是答案。如果您想澄清您的问题,请使用this link 进行编辑。
    • 哈维是对的,输入为“答案”的内容应该可以回答问题。您可以使用附加信息轻松编辑您的问题并删除此(非)答案。
    猜你喜欢
    • 2015-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    相关资源
    最近更新 更多