Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 获取所有计算机的列表,以及是否已登录到AD_C#_Active Directory_Directoryentry - Fatal编程技术网

C# 获取所有计算机的列表,以及是否已登录到AD

C# 获取所有计算机的列表,以及是否已登录到AD,c#,active-directory,directoryentry,C#,Active Directory,Directoryentry,我正在尝试接收广告中的所有计算机,以及其中哪些计算机当前已登录。我试着通过检查“lastLogonStamp”来实现这一点,但它返回了错误的值,说我的服务器是在八天前登录到AD的。即使我重新启动服务器,它也会这样说。我从这里的另一个问题得到了代码: 如果您使用的是.NET 3.5及更高版本,则可以使用PrincipalSearcher和“示例查询”主体进行搜索: // create your domain context PrincipalContext ctx = new Principal

我正在尝试接收广告中的所有计算机,以及其中哪些计算机当前已登录。我试着通过检查“lastLogonStamp”来实现这一点,但它返回了错误的值,说我的服务器是在八天前登录到AD的。即使我重新启动服务器,它也会这样说。我从这里的另一个问题得到了代码:


如果您使用的是.NET 3.5及更高版本,则可以使用
PrincipalSearcher
和“示例查询”主体进行搜索:

// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for a ComputerPrincipal 
ComputerPrincipal qbeComputer = new ComputerPrincipal(ctx);

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeComputer);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
    ComputerPrincipal cp = found as ComputerPrincipal;

    if(cp != null)
    {
       string computerName = cp.Name;
       DateTime lastLogon = cp.LastLogon;
    }
}

如果您还没有-请阅读MSDN文章,这篇文章很好地展示了如何最好地利用
System.DirectoryServices.AccountManagement
中的新功能。或者查看名称空间。

感谢您的快速响应!我在哪里传递凭证?在我的示例中,我将IP地址传递给AD服务器,“管理员”,“密码”。我如何认证自己?另外,我的网络服务器不在广告中。我只是从外部连接到它。DS。
// create your domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);

// define a "query-by-example" principal - here, we search for a ComputerPrincipal 
ComputerPrincipal qbeComputer = new ComputerPrincipal(ctx);

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeComputer);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
    ComputerPrincipal cp = found as ComputerPrincipal;

    if(cp != null)
    {
       string computerName = cp.Name;
       DateTime lastLogon = cp.LastLogon;
    }
}