2017-10-13 63 views
0
讀取屬性`MS-MCS-AdmPwdExpirationTime`

我想從ActiveDirectory讀取管理員密碼的到期時間:如何從ActiveDirectory中

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>enter image description here

但是,管理員密碼的到期時間可以通過輕鬆讀取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]) 

而且我知道,有這樣一個特性: enter image description here

有趣,但我可以讀取其他參數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() 

和值可以從調試器中可以看出:

enter image description here

如何正確讀取屬性ms-Mcs-AdmPwdExpirationTime

+1

這不是VBA。請將標籤更正爲VB.NET。 –

回答

1

返回值是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 by

Dim pwdExpTime = DateTime.FromFileTime(ConvertLargeIntegerToLong(de.Properties("ms-Mcs-AdmPwdExpirationTime").Value)) 
+0

非常感謝!這是雄偉! :) – StepUp

相關問題