4

我一直在尝试编写一个快速而肮脏的 C# .exe,我可以将它分发给我们 IT 办公室的一些学生工作者。.exe 应该能够检测到运行它的机器的名称,在 Active Directory 中搜索该名称,然后禁用计算机条目。到目前为止,我在名称检测或搜索方面没有遇到任何问题,但是当我可以直接进入 Active Directory 以查看计算机条目未被禁用时,删除代码位给了我一个误报。

    private void confirmRemoveButton_Click(object sender, EventArgs e)
    {
        string computerName = Environment.MachineName;
        using (PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, null, "useraccount", "password"))
        {
            ComputerPrincipal computer = ComputerPrincipal.FindByIdentity(domainContext, computerName);

            if (computer != null)
            {
                try
                {
                    computer.Enabled = false;
                    label3.Visible = true;
                    label3.Text = "Computer was disabled in Active Directory.";
                    button1.Visible = true;
                }

                catch (Exception x)
                {
                    label3.Visible = true;
                    label3.Text = "Unable to disable computer with exception " + x;
                    button1.Visible = true;
                }
            }

            else if (computer == null)
            {
                label3.Visible = true;
                label3.Text = "Computer was not found in Active Directory.";
                button1.Visible = true;
            }

            else
            {
                label3.Visible = true;
                label3.Text = "Unexpected error in computer search.";
                button1.Visible = true;
            }
        }
    }

这是我现在拥有的代码;前面的代码是关于让用户根据检测到的计算机名称检查计算机名称并确认他们确实想要禁用计算机帐户。一旦他们点击确认(目前被误导为确认删除按钮),它应该运行此代码以报告成功或失败。但是,在测试中,它报告成功,尽管我可以看到计算机对象没有被禁用。

此链接(http://stackoverflow.com/questions/591681/using-c-how-do-you-check-if-a-computer-account-is-disabled-in-active-directory)是一个与在标题中禁用计算机帐户,但评论和代码似乎都表明这适用于禁用用户帐户。

任何见解将不胜感激:)

4

2 回答 2

4

您需要调用Save对象ComputerPrincipal

http://msdn.microsoft.com/en-us/library/bb354074.aspx

于 2012-09-26T21:30:35.453 回答
3

您必须保存 PrincipalComputer 对象。否则你的代码很好。这是一个简单的控制台应用程序版本,如果计算机不存在,它将不返回任何内容。

    static void Main(string[] args)
    {
        Console.WriteLine("Enter the name of the computer you wish to disable");
        string ComputerName = Console.ReadLine();
        if (ComputerName != "" && ComputerName != null)
        {
            using (PrincipalContext TargetDomain = new PrincipalContext(ContextType.Domain, null, "admin", "password"))
            {
                ComputerPrincipal TargetComputer = ComputerPrincipal.FindByIdentity(TargetDomain, ComputerName);

                if (TargetComputer != null)
                {
                    if ((bool)TargetComputer.Enabled)
                    {
                        Console.WriteLine("Computer is currently enabled, it will now be disabled");
                        TargetComputer.Enabled = false;
                        Console.WriteLine("Is computer now enabled? " + TargetComputer.Enabled);
                        TargetComputer.Save();
                    }
                    else
                    {
                        Console.WriteLine("Computer is currently disabled, it will now be enabled");
                        TargetComputer.Enabled = true;
                        Console.WriteLine("Is computer now enabled? " + TargetComputer.Enabled);
                        TargetComputer.Save();
                    }
                    Console.Read();
                }
            }
        }
    }

该死,基伦打败了我!

请注意,有时 AD 可能需要一段时间才能识别出所发生的事情。

于 2012-09-26T22:07:13.303 回答