By gabrielsrokaCreated 03 Aug 2011, Modified 30 Oct 2011
My Rating:
Vote
Rating:
Not Rated
Views:8176
Downloads:81
Source:

Get pwdLastSet from Active Directory using ADSI

Language:  VBScript

Compatibility

Windows XP Unknown Windows 2003 Unknown
Windows 2000 Unknown Windows NT Unknown
Vista Yes Windows 2008 Unknown
Description

Get pwdLastSet from Active Directory using ADSI

Code

Line Numbers: On  Off      Plain Text
Option Explicit
Dim ADsPath
ADsPath = WScript.Arguments.Named.Item("ADsPath")
If ADsPath = "" Then
	MsgBox "Go back to Password Control and enter a username."
Else
	Dim User
	Set User = GetObject(ADsPath)
	On Error Resume Next
	MsgBox User.cn & " password last changed on " & User.PasswordLastChanged & " local time."
	If Err Then MsgBox User.cn & " must change password at next logon."
	'MsgBox User.PasswordExpirationDate ' this takes a long time to come up.
	'MsgBox DecodeLargeInt(User.pwdLastSet) & " UTC"
End If
Function DecodeLargeInt(LgInt)
	Dim dVal
	Const Ticks_per_sec = 10000000 ' 1E7. 1 tick = 100 nanoseconds
	Const Tick_start_date = #1/1/1601#
	Const MinuteInterval = "n" ' "n" is Minute ("m" is Month)
	Const SecondInterval = "s"
	' See Correction in Community Content section under http://msdn.microsoft.com/en-us/library/aa706038
	dVal = LgInt.HighPart * 2 ^ 32 + (LgInt.LowPart Xor &H80000000) + 2 ^ 31
	If (LgInt.HighPart = -2147483648 And LgInt.LowPart = 0) Or (LgInt.HighPart = 2147483647 And LgInt.LowPart = -1) Then
		DecodeLargeInt = "(never)"
	ElseIf dVal > 1E+17 And dVal < 2E+17 Then ' Assume dVal represents a Date between Nov 1917 and Oct 2234.
		Dim ticks, seconds, minutes
		ticks = dVal ' dVal contains 100-nanosecond intervals (ticks) since Jan 1, 1601.
		seconds = ticks / Ticks_per_sec
		' DateAdd() converts the 2nd argument to a Long. Passing in seconds will overflow, so use minutes even though there's some rounding.
		minutes = CLng(seconds / 60)
		DecodeLargeInt = DateAdd(SecondInterval, seconds - minutes * 60, DateAdd(MinuteInterval, minutes, Tick_start_date))
	Else
		DecodeLargeInt = dVal ' Assume it's a Large Int, not a Date.
	End If
End Function

 


Got a useful script? Click here to upload!


 

  Post Comment
Order By:  
User Comments
      
Be the first to post a comment!