首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用C#,如何检查活动目录中是否禁用了计算机帐户?

在C#中,您可以使用System.DirectoryServices.AccountManagement库来检查活动目录中是否禁用了计算机帐户。以下是一个示例代码:

代码语言:csharp
复制
using System;
using System.DirectoryServices.AccountManagement;

namespace CheckDisabledComputerAccount
{
    class Program
    {
        static void Main(string[] args)
        {
            string computerName = "your_computer_name";
            string domainName = "your_domain_name";
            string domainUsername = "your_domain_username";
            string domainPassword = "your_domain_password";

            using (PrincipalContext principalContext = new PrincipalContext(ContextType.Domain, domainName, domainUsername, domainPassword))
            {
                ComputerPrincipal computerPrincipal = ComputerPrincipal.FindByIdentity(principalContext, computerName);

                if (computerPrincipal != null)
                {
                    bool isDisabled = computerPrincipal.Enabled.HasValue && !computerPrincipal.Enabled.Value;

                    if (isDisabled)
                    {
                        Console.WriteLine($"The computer account '{computerName}' is disabled.");
                    }
                    else
                    {
                        Console.WriteLine($"The computer account '{computerName}' is not disabled.");
                    }
                }
                else
                {
                    Console.WriteLine($"The computer account '{computerName}' was not found.");
                }
            }
        }
    }
}

在这个示例中,您需要将your_computer_nameyour_domain_nameyour_domain_usernameyour_domain_password替换为您的计算机名称、域名、域用户名和域密码。

这个示例使用了System.DirectoryServices.AccountManagement库,它是.NET框架中的一个库,可以用于管理和查询活动目录对象。在这个示例中,我们使用了ComputerPrincipal类来查找计算机帐户,并检查其Enabled属性来确定它是否被禁用。

您可以使用这个示例代码来检查活动目录中计算机帐户是否被禁用,并根据需要进行相应的操作。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的视频

领券