I'm trying to receive all computers that are currently connected to AD and which of them who has a user logged on to AD. I've tried with the ComputerPrincipal
's .LastLogon
property, but I get a value that is completely off, about a week.
I want to know which computers in AD who's availiable. Is there another method I can use?
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 />");
}
}
EDIT:
I want the print-out to be like this:
Computer 1: True Computer 2: False Computer 3: False Computer 4: True
Is it impossible to query a computer if it's currently logged on? I just need an bool, True or False.