Here is some sample code I have that finds all computer objects in an OU. When I print out the property fields, I get a System.__ComObject
for several of the values such as lastLogon
, lastLogonTimestamp
, pwdLastSet
, uSNChanged
, etc. I assume these are all date-ish type values of some sort.
How do I get the date value out of it? I'd like a c# solution not a powershell solution such as this: https://sweeneyops.wordpress.com/2012/06/11/active-directory-timestamp-conversion-through-powershell/
Thanks
using (DirectoryEntry entry = new DirectoryEntry("LDAP://" + ou))
{
using (DirectorySearcher searcher = new DirectorySearcher(entry))
{
searcher.Filter = ("(objectClass=computer)");
searcher.SizeLimit = int.MaxValue;
searcher.PageSize = int.MaxValue;
foreach (SearchResult result in searcher.FindAll())
{
DirectoryEntry computer = result.GetDirectoryEntry();
foreach(string propName in computer.Properties.PropertyNames)
{
foreach(object value in computer.Properties[propName])
{
Console.WriteLine($"{propName}: {value}");
}
}
}
}
}
I know there is a long inside of the object that I can use DateTime.FromFileTime(longType)
to get the date out of it.