首页
学习
活动
专区
工具
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属性来确定它是否被禁用。

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

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

相关·内容

使用非管理员实现系统事件日志的自动备份与清除

最近一个客户要对域内所有的域控制器开启日志审核,要审核的内容包括“用户的登录、注销”“活动目录账户管理”。开启审核后,在用户比较多并且登陆比较频繁的情况下,Windows安全日志会快速的增加,Windows安全日志默认的大小为128MB,可以通过修改安全日志最大大小来增加安全日志的存储量,理论上Windows安全日志的最大值可以设定为4GB,但微软官方指出安全日志在实际中最大的限制为300MB,但是即使我们调整日志最大大小为一个较大的值也避免不了长期日志过大达到该限制的情况。当Windwos安全日志满了后,每次用户登陆到域控制器都会提示如下的错误。

03
领券