2012-05-29 58 views
1

我试图接收当前连接到AD的所有计算机,以及哪些用户已登录到AD。我已经尝试了ComputerPrincipal.LastLogon属性,但我得到的价值完全是关闭的,大概一周。确定AD中的计算机是否有用户登录

我想知道AD中的哪些计算机可用。有另一种方法可以使用吗?

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "192.168.0.101:389", "Administrator", "XXXX"); 

// 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 = new DateTime(); 
     lastLogon = cp.LastLogon; 
     DateTime convertedDate = DateTime.SpecifyKind(DateTime.Parse(cp.LastLogon.ToString()), DateTimeKind.Utc); 

     var kind = convertedDate.Kind; 
     DateTime dt = convertedDate.ToLocalTime(); 

     Response.Write(cp.Name+" : "+ dt.ToString()+ "<br />"); 
    } 
} 

编辑:

我想打印出来是这样的:

电脑1:真 电脑2:假 电脑3:假 电脑4:真

查询电脑当前是否登录不可能?我只需要一个布尔,对或错。

+0

提供的一个例子你得到了什么以及你的期望。 –

+0

Active Directory是一个**静态**资源 - 它没有“动态”运行时信息,例如谁在哪台计算机上登录。恐怕您无法从Active Directory中检索您要查找的信息。 –

+0

lastLogon发生了什么 - 为什么当您立即用'cp.LastLogin'替换'new DateTime()'时, '?同样,当你刚刚在前一行指定时,为什么要访问'convertedDate.Kind'? –

回答

0

这是一个经典的MCSE问题。 lastlogon字段对于该特定DC是本地的,并且不是全局复制的。

您需要查询每个AD域控制器并查找每个AD域控制器的最近日期。

+0

我只有一个域控制器。 –

0

您需要查询forest/domain中所有域控制器上的安全事件日志,以确保该用户已登录某台计算机。然后应使用WMI联系该工作站以检查用户是否仍登录。

您可能感兴趣的事件是Logon events,其中包含交互式登录类型(登录ID 4624,登出4634)。

但是,当PC与域(笔记本非常常见)和用户注销失去连接时,没有任何域控制器会收到注销事件。

用户可以在没有域的情况下实际登录以前已经在PC上创建了他的帐户的本地缓存。

正如有人说lastlogon和的lastLogonTimestamp不能用于可靠的方法来追踪用户登录,检查thisthis

一些示例here

等详细信息herehere

+0

我想查询安全事件日志有点棘手? 你有什么例子吗? 谢谢! –

+0

查看我的更新回答 – rkosegi

相关问题