I'm having this issue where I can't retrieve a AD attribute via DirectoryEntry
.
I can get it via DirectorySearcher
, but I'm unable to get or set it via DirectoryEntry
.
The attribute needed is ms-Mcs-AdmPwdExpirationTime
which contains a NT TimeStamp, I have read and write to this attribute.
DirectoryEntry
C# error in console
Error HRESULT E_FAIL has been returned from a call to a COM component
I've tried using the following yet still unable to retrieve the attribute.
RefreshCache (string[] propertyNames);
EDIT:
ComputerPrincipal comp = ComputerPrincipal.FindByIdentity(ctx, MachineName);
DirectoryEntry de = (DirectoryEntry)comp.GetUnderlyingObject();
if (de.Properties.Contains("ms-Mcs-AdmPwd") == true)
{
string Password = (String)de.Properties["ms-Mcs-AdmPwd"][0];
Password_Input.Text = Password;
DateTime NTTime = DateTime.FromFileTime(ConvertLargeIntegerToLong(de.Properties["ms-Mcs-AdmPwdExpirationTime"].Value));
PasswordExpiry_Value.Text = NTTime.ToString("dd/MM/yyyy hh:mm:ss");
Console.WriteLine();
}
else
{
Password_Input.Text = "Password not set by LAPS";
}
// down the bottom of the .cs
private static long ConvertLargeIntegerToLong(object largeInteger)
{
var type = largeInteger.GetType();
var highPart = Convert.ToInt32(type.InvokeMember("HighPart", BindingFlags.GetProperty, null, largeInteger, null));
var lowPart = Convert.ToInt32(type.InvokeMember("LowPart", BindingFlags.GetProperty, null, largeInteger, null));
return (long)highPart << 32 | (uint)lowPart;
}