asked on
'Define Constants
Const ForReading = 1
Const ADS_SCOPE_SUBTREE = 2 ' Search target object and all sub levels
Const ADS_GROUP_TYPE_SECURITY_ENABLED = &H80000000
DQ = Chr(34) 'Double Quote
WScript.Echo "Creating user file"
'Create Objects
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set SecurityGroups = CreateObject("System.Collections.ArrayList")
Set DistributionGroups = CreateObject("System.Collections.ArrayList")
'Construct an ADsPath to the Current Domain with rootDSE
Set objRootDSE = GetObject("LDAP://rootDSE")
strADsPath = "LDAP://" & objRootDSE.Get("defaultNamingContext")
'Connect to Active Directory
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
'Create output text file
strScriptPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
strOutputFilePath = strScriptPath & "users.txt"
Set objOutputFile = objFSO.CreateTextFile(strOutputFilePath)
'Search AD for user
objCommand.CommandText = _
"SELECT ADsPath FROM '" & strADsPath & _
"' WHERE objectCategory='user'"
Set objRecordSet = objCommand.Execute
'Verify user was found
If objRecordSet.EOF Then
WScript.echo "No users were found."
Else
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strUserADsPath = objRecordSet.Fields("ADsPath").Value
'Connect to user account
Set objUser = GetObject(strUserADsPath)
'Format Last Logon Time Stamp
'Set objLastLogon=null
dtmLastLogon=""
on error resume next
Set objLastLogon = objUser.Get("lastLogonTimestamp")
if not isNull(objLastLogon) then
intLastLogonTime = objLastLogon.HighPart * (2 ^ 32) + objLastLogon.LowPart
intLastLogonTime = intLastLogonTime / (60 * 10000000)
intLastLogonTime = intLastLogonTime / 1440
dtmLastLogon = intLastLogonTime + # 1 / 1 / 1601 #
end if
on error goto 0
'Populate text file
ws=DQ & objUser.sAMAccountName & DQ
ws=ws & "," & DQ & objUser.extensionAttribute1 & DQ
ws=ws & "," & DQ & objUser.description & DQ
ws=ws & "," & DQ & objUser.whenCreated & DQ
ws=ws & "," & DQ & dtmLastLogon & DQ
ws=ws & "," & DQ & objUser.mail & DQ
ws=ws & "," & DQ & objUser.info & DQ
objOutputFile.WriteLine ws
objRecordSet.MoveNext
Loop
End If
WScript.Echo "Finished creating file"
objOutputFile.Close
'Open file
strOpenFile = MsgBox("Do you want to open the output file?", VbYesNo + VBQuestion, "Open Output File?")
If strOpenFile = VbYes Then
objShell.Run("notepad.exe " & DQ & strOutputFilePath & DQ)
End If