我正在尝试从读取管理员密码的终止时间
Dim DC = New PrincipalContext(ContextType.Domain)
Dim cmp = ComputerPrincipal.FindByIdentity(DC, hostnm)
Dim desting As String = cmp.DistinguishedName
Dim de As New DirectoryEntry("LDAP://" & desting)
pwdexp = de.Properties("ms-Mcs-AdmPwdExpirationTime").Value.ToString()但我看到的只是<COM Type>:

但是,PowerShell很容易读取管理员密码的过期时间。
$TestValue = [adsi]"LDAP://CN=xxx,OU=xxx,OU=xxx,OU=xxx,OU=xxx,DC=xxx,DC=xxx,DC=xx"
$TestValue.ConvertLargeIntegerToInt64($Testvalue."ms-Mcs-AdmPwdExpirationTime"[0])我知道有这样一种财产:

有趣的是,但是我可以读到另一个参数ms-Mcs-AdmPwd
Dim DC = New PrincipalContext(ContextType.Domain)
Dim cmp = ComputerPrincipal.FindByIdentity(DC, hostnm)
Dim desting As String = cmp.DistinguishedName
Dim de As New DirectoryEntry("LDAP://" & desting)
pwdexp = de.Properties("ms-Mcs-AdmPwdExpirationTime").Value.ToString()从调试器中可以看出值:

如何正确读取属性ms-Mcs-AdmPwdExpirationTime?
发布于 2017-10-15 07:52:05
返回值是DateTime,它在AD中表示为LargeInteger。你必须把它转换成能够读懂它。
注意,在PowerShell中,您正在使用ConvertLargeIntegerToInt64转换值。所以,我们也需要先做同样的事。
C#中的代码
/// <summary>
/// Decodes IADsLargeInteger objects into a FileTime format (long)
/// </summary>
public static long ConvertLargeIntegerToLong(object largeInteger)
{
var type = largeInteger.GetType();
var highPart = (int)type.InvokeMember("HighPart", BindingFlags.GetProperty, null, largeInteger, null);
var lowPart = (int)type.InvokeMember("LowPart", BindingFlags.GetProperty, null, largeInteger, null);
return (long)highPart << 32 | (uint)lowPart;
}在VB.NET中(使用http://converter.telerik.com/)
''' <summary>
''' Decodes IADsLargeInteger objects into a FileTime format (long)
''' </summary>
Public Shared Function ConvertLargeIntegerToLong(largeInteger As Object) As Long
Dim type = largeInteger.[GetType]()
Dim highPart = CInt(type.InvokeMember("HighPart", BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
Dim lowPart = CInt(type.InvokeMember("LowPart", BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
Return CLng(highPart) << 32 Or CUInt(lowPart)
End Function然后使用以下方法读取日期值
var pwdExpTime = DateTime.FromFileTime(ConvertLargeIntegerToLong(de.Properties["ms-Mcs-AdmPwdExpirationTime"].Value));在VB.NET中
Dim pwdExpTime = DateTime.FromFileTime(ConvertLargeIntegerToLong(de.Properties("ms-Mcs-AdmPwdExpirationTime").Value))发布于 2022-06-21 10:24:27
使用接受的答案并将ms-Mcs-AdmPwdExpirationTime设置为133004656837955708 (应该会导致23.06.2022 15:48:03),我得到了一个错误。
使用此日期,lowPart为负值,不能使用CUInt进行强制转换。
所以我混合了两种解决方案:
Public Shared Function ConvertLargeIntegerToLong(largeInteger As Object) As Long
Dim type = largeInteger.[GetType]()
Dim highPart = CInt(type.InvokeMember("HighPart", BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
Dim lowPart = CInt(type.InvokeMember("LowPart", BindingFlags.GetProperty, Nothing, largeInteger, Nothing))
If lowPart < 0 Then
Dim tempStr as String = "&H" + CStr(Hex(highPart)) + CStr(Hex(lowPart))
Return Val(tempStr)
Else
Return CLng(highPart) << 32 Or CUInt(lowPart)
End If
End Functionhttps://stackoverflow.com/questions/46723874
复制相似问题