I am using the below code to get the LAPS password and expiration date for a specific computer:
string computerHostName = "hostname";
string domainController = "domain.lan";
DirectoryContext dirCtx = new DirectoryContext(DirectoryContextType.Domain, domainController);
using (Domain compsDomain = Domain.GetDomain(dirCtx))
using (DirectorySearcher adSearcher = new DirectorySearcher(compsDomain.GetDirectoryEntry()))
{
//this is the search criteria for the domain query
adSearcher.Filter = "(&(objectClass=computer) (cn=" + computerHostName + "))";
adSearcher.SearchScope = SearchScope.Subtree;
adSearcher.PropertiesToLoad.Add("ms-Mcs-AdmPwd");
adSearcher.PropertiesToLoad.Add("ms-Mcs-AdmPwdExpirationTime");
SearchResult searchResult = adSearcher.FindOne();
//Get the LAPS password
Console.WriteLine(searchResult.GetDirectoryEntry().Properties["ms-Mcs-AdmPwd"].Value);
//Should get the LAPS password expiration time
Console.WriteLine(searchResult.GetDirectoryEntry().Properties["ms-Mcs-AdmPwdExpirationTime"].Value);
}
Console.ReadLine();
However, the output looks like this:
[LAPS password here]
System.__ComObject
I have tried looking online and have already seen this question and this one but I still can't get it to work. How can I get the script to output the expiration time normally?
Any help is appreciated :)