确定AD中的计算机是否有用户登录

本文关键字:用户 登录 是否 计算机 AD 确定 | 更新日期: 2023-09-27 18:35:40

我正在尝试接收当前连接到AD的所有计算机以及其中哪些计算机的用户登录到AD。我已经尝试过使用ComputerPrincipal.LastLogon属性,但我得到的值完全关闭,大约一周。

我想知道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:真

如果计算机当前已登录,是否无法查询计算机?我只需要一个布尔值,真或假。

确定AD中的计算机是否有用户登录

这是一个

经典的MCSE问题。 最后登录字段是该特定 DC 的本地字段,不会全局复制。

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

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

您可能感兴趣的事件是具有交互式登录类型的登录事件(ID 4624 表示登录,4634 表示注销)。

但是,当电脑失去与域的连接(在笔记本中很常见)和用户注销时,任何域控制器都不会收到注销事件。

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

正如其他人所说,lastlogon和lastlogontimestamp不能用于跟踪用户登录的可靠方式,请检查这个和这个

这里有一些例子

更多信息在这里和这里