简体   繁体   中英

Get a list of all computers and also if it is logged into AD

I'm trying to recive all computers in my AD and also which of them whos currently logged in. I've tryed doing this by checking the "lastLogonStamp" but that returns the wrong value, saying my server was logged into AD eight days ago. Even if I restart the server it says the same. I got the code from another question here:

How to list all computers and the last time they were logged onto in AD?

public DataTable GetListOfComputers(string domain, string userName, string password)
    {
        DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain,
                userName, password, AuthenticationTypes.Secure);
        DirectorySearcher search = new DirectorySearcher(entry);
        string query = "(objectclass=computer)";
        search.Filter = query;

        search.PropertiesToLoad.Add("name");
        search.PropertiesToLoad.Add("lastLogonTimestamp");

        SearchResultCollection mySearchResultColl = search.FindAll();

        DataTable results = new DataTable();
        results.Columns.Add("name");
        results.Columns.Add("lastLogonTimestamp");

        foreach (SearchResult sr in mySearchResultColl)
        {
            DataRow dr = results.NewRow();
            DirectoryEntry de = sr.GetDirectoryEntry();
            dr["name"] = de.Properties["Name"].Value;
            dr["lastLogonTimestamp"] = DateTime.FromFileTimeUtc(long.Parse(sr.Properties["lastLogonTimestamp"][0].ToString()));
            results.Rows.Add(dr);
            de.Close();
        }

        return results;
    }

If you're using .NET 3.5 and up, you can use a PrincipalSearcher and a "query-by-example" principal to do your searching:

// 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;
    }
}

If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement . Or see the MSDN documentation on the System.DirectoryServices.AccountManagement namespace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM