获得最后登录时间,计算机在Active Directory [英] Getting last Logon Time on Computers in Active Directory

查看:300
本文介绍了获得最后登录时间,计算机在Active Directory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href="http://stackoverflow.com/questions/5162897/how-can-i-get-a-list-of-users-from-active-directory">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?

推荐答案

你为什么不只是使用的<一个href="http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.authenticableprincipal.lastlogon.aspx"相对=nofollow> 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.

这篇关于获得最后登录时间,计算机在Active Directory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆