获得最后登录时间,计算机在Active Directory计算机、时间、Directory、Active

由网友(比我轻?要么平胸、要么矮)分享简介:How我可以从活动目录用户列表? 请参见上面的页面。它回答了我大部分的问题,但我得到一个问题,当我试图让最后一次登录时间为一台电脑。很抱歉,如果有一些方法的页面上进行评论,而不是做一个全新的问题,因为我没有找到这样的选择。Please see the page above. It answered most of...

How我可以从活动目录用户列表?

请参见上面的页面。它回答了我大部分的问题,但我得到一个问题,当我试图让最后一次登录时间为一台电脑。很抱歉,如果有一些方法的页面上进行评论,而不是做一个全新的问题,因为我没有找到这样的选择。

Please see the page above. It answered most of my questions, but I get a problem when I try to get last logon time for a computer. Sorry if there was some way to comment on that page instead of making a whole new question because I didn't find such an option.

using (var context = new PrincipalContext(ContextType.Domain, "cat.pcsb.org"))
        {
            using (var searcher = new PrincipalSearcher(new ComputerPrincipal(context)))
            {
                foreach (var result in searcher.FindAll())
                {
                    DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;
                    Console.WriteLine("Name: " + de.Properties["name"].Value);
                    Console.WriteLine("Last Logon Time: " + de.Properties["lastLogon"].Value);
                    Console.WriteLine();
                }
            }
        }
        Console.ReadLine();

我取代UserPrincipal与ComputerPrincipal。姓名和其他一些属性做工精细,但登录不。我已经尝试做不同的事情喜欢铸造日期时间(投失败),但毫无效果。以上只是导致系统.__ ComObject。所以,我能做些什么来得到它正确地得到最后一次登录的时间?

I replaced UserPrincipal with ComputerPrincipal. Name and some other properties work fine, but logon doesn't. I've tried doing different things like casting it to DateTime (the cast failed) but nothing worked. The above just results in System.__ComObject. So what can I do to get it to get last logon time correctly?

推荐答案

你为什么不只是使用的 LastLogon属性返回由ComputerPrincipal ? (ComputerPrincipal是AuthenicatablePrincipal)

Why aren't you just using the the LastLogon property returned by ComputerPrincipal? (ComputerPrincipal is a AuthenicatablePrincipal)

using (var context = new PrincipalContext(ContextType.Domain, "cat.pcsb.org"))
{
    using (var searcher = new PrincipalSearcher(new ComputerPrincipal(context)))
    {
        foreach (var result in searcher.FindAll())
        {
            var auth = result as AuthenticablePrincipal;
            if(auth != null)
            {
                Console.WriteLine("Name: " + auth.Name);
                Console.WriteLine("Last Logon Time: " + auth.LastLogon);
                Console.WriteLine();
            }
        }
    }
}
Console.ReadLine();

注意LastLogon不是复制的属性,所以如果你有多个域控制器,你需要查询每个控制器,并找出谁给的最新结果。

Note that LastLogon is not a replicated property, so if you have more than one domain controller you need to query each controller and find out who gives the most recent result.

阅读全文

相关推荐

最新文章