VBScript – Simple Reporting from Active Directory

email me

Using this VBScript, you can generate a report on many of the user attributes in Active Directory.

'SET OU HERE
strOU = "OU=Users,DC=YourDomain,DC=com"

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000'if you have problems, change this to 500

Set objShell = CreateObject("WScript.Shell")
Set objSysInfo = CreateObject("ADSystemInfo")

objCommand.CommandText = "<LDAP://" & strOU & ">;" & "(&(objectclass=user)(objectcategory=person));" & _
"adspath,distinguishedname,sAMAccountName;subtree"
Set objRecordSet = objCommand.Execute
<span style="color: #ff0000;">''''if you receive the error: "A referral was returned from the server", try adding LDAP://<em>DomainControllerName</em>/</span>

Do Until objRecordSet.EOF

strParse = replace(objRecordSet.Fields("adspath").Value, "LDAP://", "")
Set objUser = GetObject("LDAP://" & strParse)

'Testing area
'WScript.Echo objUser.displayName
'WScript.Echo objUser.sAMAccountName

'Create output you want in a CSV
strOutput = objUser.displayName & "," & objUser.sAMAccountName & "," & objUser.description

'Output single user info to CSV
objShell.Run "%comspec% /c echo " & strOutput & ">>Report.csv",0,true

'Move to next record in AD
objRecordSet.MoveNext

Loop

'Pop up message when done
msgbox "  Report is done!"

'Clear Session
objRecordSet.Close
Set objRecordSet = Nothing
Set objCommand = Nothing
objConnection.Close
Set objConnection = Nothing

&nbsp;

<strong>Some things you can report on:</strong>

WScript.Echo "First name: " & objUser.givenName
'WScript.Echo "First name: " & objUser.FirstName
WScript.Echo "Initials: " & objUser.initials
WScript.Echo "Last name: " & objUser.sn
'WScript.Echo "Last name: " & objUser.LastName
WScript.Echo "Display name: " & objUser.displayName
'WScript.Echo "Display name: " & objUser.FullName
WScript.Echo "Description: " & objUser.description
WScript.Echo "Office: " & objUser.physicalDeliveryOfficeName
WScript.Echo "Telephone number: " & objUser.telephoneNumber
WScript.Echo "Other Telephone numbers: " & objUser.otherTelephone
WScript.Echo "Email: " & objUser.mail
' WScript.Echo "Email: " & objUser.EmailAddress
WScript.Echo "Web page: " & objUser.wWWHomePage
WScript.Echo "Other Web pages: " & objUser.url
WScript.Echo ""
WScript.Echo "ADDRESS"
WScript.Echo "Street: " & objUser.streetAddress
WScript.Echo "P.O. Box: " & objUser.postOfficeBox
WScript.Echo "City: " & objUser.l
WScript.Echo "State/province: " & objUser.st
WScript.Echo "Zip/Postal Code: " & objUser.postalCode
WScript.Echo "Country/region: " & objUser.countryCode
'WScript.Echo "Country/region: " & objUser.c '(ISO 4217)
WScript.Echo ""
WScript.Echo "ACCOUNT"
WScript.Echo "User logon name: " & objUser.userPrincipalName
WScript.Echo "pre-Windows 2000 logon name: " & objUser.sAMAccountName
WScript.Echo "AccountDisabled: " & objUser.AccountDisabled
' WScript.Echo "Account Control #: " & objUser.userAccountControl
WScript.Echo "Logon Hours: " & objUser.logonHours
WScript.Echo "Logon On To (Logon Workstations): " & objUser.userWorkstations
' WScript.Echo "User must change password at next logon: " & objUser.pwdLastSet
WScript.Echo "User cannot change password: " & objUser.userAccountControl
WScript.Echo "Password never expires: " & objUser.userAccountControl
WScript.Echo "Store password using reversible encryption: " & objUser.userAccountControl
' WScript.Echo "Account expires end of (date): " & objUser.accountExpires
WScript.Echo ""
WScript.Echo "PROFILE"
WScript.Echo "Profile path: " & objUser.profilePath
' WScript.Echo "Profile path: " & objUser.Profile
WScript.Echo "Logon script: " & objUser.scriptPath
WScript.Echo "Home folder, local path: " & objUser.homeDirectory
WScript.Echo "Home folder, Connect, Drive: " & objUser.homeDrive
WScript.Echo "Home folder, Connect, To:: " & objUser.homeDirectory
WScript.Echo ""
WScript.Echo "TELEPHONE"
WScript.Echo "Home: " & objUser.homePhone
WScript.Echo "Other Home phone numbers: " & objUser.otherHomePhone
WScript.Echo "Pager: " & objUser.pager
WScript.Echo "Other Pager numbers: " & objUser.otherPager
WScript.Echo "Mobile: " & objUser.mobile
WScript.Echo "Other Mobile numbers: " & objUser.otherMobile
WScript.Echo "Fax: " & objUser.facsimileTelephoneNumber
WScript.Echo "Other Fax numbers: " & objUser.otherFacsimileTelephoneNumber
WScript.Echo "IP phone: " & objUser.ipPhone
WScript.Echo "Other IP phone numbers: " & objUser.otherIpPhone
WScript.Echo "Notes: " & objUser.info
WScript.Echo ""
WScript.Echo "ORGANIZATION"
WScript.Echo "Title: " & objUser.title
WScript.Echo "Department: " & objUser.department
WScript.Echo "Company: " & objUser.company
WScript.Echo "Manager: " & objUser.manager