Here is the script I'm working with.
A working dump all AD users including (name,
AMAccountName ,displayname, accountExpires & description)
Works perfect. One little thing I would like to change is - Instead of
converting the dates to my local timezone, would it be possible to use
the actual date on the in AD/ on the server?
Is there any way to filter the description field? I would like to only
output accounts that which their description field begin with a
specific set of characters. So only description fields that start with
say - "PPC A, followed but random text etc..."
I tried to add in the "Is account disabled" record set but was
unsuccessful. Any suggestions? :)
Option explicit
Dim Con
Dim ocommand
Dim message, strText
Dim sADSPath
Dim fso,ofolders
dim wshShell, RS
Set wshShell = WScript.CreateObject("WScript.Shell")
dim quote,title
quote=chr(34)
dim objDate
dim lngDate
dim objUser
dim objShell, lngBias, lngBiasKey
Set objShell = CreateObject("Wscript.Shell")
' Obtain local Time Zone bias from machine registry.
lngBiasKey = objShell.RegRead("HKLM\System\CurrentControlSet
\Control\" _
& "TimeZoneInformation\ActiveTimeBias")
If (UCase(TypeName(lngBiasKey)) = "LONG") Then
lngBias = lngBiasKey
ElseIf (UCase(TypeName(lngBiasKey)) = "VARIANT()") Then
lngBias = 0
For k = 0 To UBound(lngBiasKey)
lngBias = lngBias + (lngBiasKey(k) * 256^k)
Next
End If
Set objShell = Nothing
If (Not IsCScript()) Then 'If not CScript, re-run with
cscript...
WshShell.Run "CScript.exe " & quote & WScript.ScriptFullName &
quote, 0, true
WScript.Quit '...and stop running as WScript
End If
' Create FileSystemObject object to access file system.
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
'get path name, ending in
Dim desktoppath, logfile, appendout
desktoppath = wshShell.ExpandEnvironmentStrings("%USERPROFILE%")&
"\Desktop\"
dim root
'Get the default ADsPath for the domain to search.
Set root = GetObject("LDAP://rootDSE")
sADSPath = root.Get("defaultNamingContext")
Call ADOConnect
logfile = desktoppath &
left(WScript.ScriptName,Len(WScript.ScriptName)-3)& "csv"
'If fso.FileExists(logfile) Then fso.DeleteFile logfile,True
'setup log
Const ForAppend = 8
set AppendOut = fso.OpenTextFile(logfile, ForAppend, True)
appendout.WriteLine
"name,sAMAccountName,displayname,description,mail,accountExpires"'
LastLogoff,AccountDisabled,IsAccountLocked,mail,accountExpires"
Call ADOQuery
'Show done
appendout.Close
wshshell.Popup "The logfile, " & fso.GetFileName(quote & logfile) & ",
is on your desktop.",15,"Done"
Wscript.Quit 'Script ends
' Functions and Subroutines
'This is the tedious MS way that I no longer use
Sub BailOnFailure(ErrNum, ErrText)
strText = "Error 0x" & Hex(ErrNum) & " " & ErrText
MsgBox strText, vbInformation, "ADSI Error"
WScript.Quit
End Sub
Sub ADOConnect
'Create ADO connection object for Active Directory
Set Con = CreateObject("ADODB.Connection")
If (Err.Number <> 0) Then
BailOnFailure Err.Number, "on CreateObject"
End If
Con.Provider = "ADsDSOObject"
If (Err.Number <> 0) Then
BailOnFailure Err.Number, "on Provider"
End If
Con.Open "Active Directory Provider"
If (Err.Number <> 0) Then
BailOnFailure Err.Number, "on Open"
End If
'Create ADO command object for the connection.
Set ocommand = CreateObject("ADODB.Command")
If (Err.Number <> 0) Then
BailOnFailure Err.Number, "on CreateObject"
End If
ocommand.ActiveConnection = Con
If (Err.Number <> 0) Then
BailOnFailure Err.Number, "on Active Connection"
End If
End Sub
Sub ADOQuery
dim i
Dim domain,sfilter, sAttribsToReturn, sDepth
'Build the ADsPath element of the commandtext
sADsPath = "<LDAP://" & sADSPath & ">"
'set filter for users only
sFilter = "(&(objectClass=user)(objectCategory=person))"
'Build the returned attributes element of the commandtext.
sAttribsToReturn =
"name,adsPath,sAMAccountName,displayname,description,mail,accountExpires"
',lastlogoff,accountdisabled,isaccountlocked"
'Build the depth element of the commandtext.
sDepth = "subTree"
'Assemble the commandtext.
ocommand.CommandText = sADsPath & ";" & sFilter & ";" &
sAttribsToReturn & ";" & sDepth
'WScript.Echo "CommandText: " & ocommand.CommandText
ocommand.Properties("Page Size") = 1000 'Get 1000 then
continue. Without it, stops at 1000
'Execute the query.
Set rs = ocommand.Execute
If (Err.Number <> 0) Then
BailOnFailure Err.Number, "on Execute"
End If
' Navigate the record Set
rs.MoveFirst
While Not rs.EOF
GetInfo
rs.MoveNext
Wend
End Sub
Set objDate = objUser.accountExpires
lngDate = (objDate.HighPart * (2^32)) * objDate.LowPart
Sub GetInfo()
Dim strDescription, tArray, strUName, objUser, strExp
'Description is sometimes and array. Annoying
If IsArray (RS.Fields("Description").Value) Then
tArray = RS.Fields("Description").Value
strDescription = tArray(0)
Else
strDescription = RS.Fields("Description").Value
End If
strUName = quote & rs.Fields("Name").Value & quote
message = strUName & "," & rs.Fields("sAMAccountName").Value & ","
& _
quote & rs.Fields("displayname").Value & quote & "," & _
quote & strDescription & quote & "," & _
rs.Fields("mail").Value & ","
On Error Resume next 'connect to user object
Set objUser = GetObject(RS.Fields("AdsPath").Value)
If objUser.AccountExpirationDate = "1/1/1970" Or Err.Number =
-2147467259 Then
strExp = "Never Expires"
Else
strExp = objUser.AccountExpirationDate
End If
message = message & strExp
EchoAndLog message
Err.Clear
On Error goto 0
End Sub
Sub EchoAndLog(message)
WScript.Echo message
appendout.WriteLine message
End Sub
Function IsCScript()
If (InStr(UCase(WScript.FullName), "CSCRIPT") <> 0) Then
IsCScript = True
Else
IsCScript = False
End If
End Function