OU timestamp ComObject

admin

Administrator
Staff member
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
Code:
System.__ComObject
for several of the values such as
Code:
lastLogon
,
Code:
lastLogonTimestamp
,
Code:
pwdLastSet
,
Code:
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: <a href="https://sweeneyops.wordpress.com/20...tory-timestamp-conversion-through-powershell/" rel="nofollow">https://sweeneyops.wordpress.com/20...tory-timestamp-conversion-through-powershell/</a>

Thanks

Code:
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
Code:
DateTime.FromFileTime(longType)
to get the date out of it.