I have edited and merged multiple scriptlets found here and elswhere to allow me to do the following.
1. query AD for list of users
2. step through this list and get additional information
3. write this info to an excel spreadsheet
This script works perfectly on my test domain but not on my production domain. If I run it from a client as domain admin it errors on line 83 char 5 (where it calls the dumpuser sub). If I run it on a domain controller as domain admin it returns a redirect from the server. (I do not know what this is and cannot find it on MS site)
Both Domains are mixed mode and AD OU structure is the same,
but only about one percent of the number of users on test domain as on production domain. I am adding more now to see if it breaks.
If anyone can tell me what I am doing wrong or overlooking I would appreciate it.
Option Explicit
' On Error Resume Next
' Declare information variables
Dim objUser
Dim strExcelPath
Dim oSheet
Dim oExcel
Dim objFSO
Dim ix
Dim strtmpusr
Dim i
Dim objConnection
Dim objCommand
Dim objRecordSet
Dim strDNSPath
Const ADS_SCOPE_SUBTREE = 2
strExcelPath = "Path and name for excel workbook"
strDNSPath = "MyDomain"
' ****************************************************
i = Main
' Save spreadsheet and close
oExcel.ActiveWorkbook.SaveAs strExcelPath
oExcel.ActiveWorkbook.Close
Set oSheet = Nothing
Set oExcel = Nothing
wscript.echo "This script is done"
wscript.quit
' *******************************************************
Function Main
' Prepare Spreadsheet
Set oExcel = CreateObject("Excel.Application"
oExcel.Workbooks.Add
oExcel.ActiveWorkbook.Worksheets.Add
Set oSheet = oExcel.ActiveWorkbook.Worksheets(1)
oSheet.Cells.Font.Size = 8
oSheet.Name = "User Information"
oSheet.Cells(1,1).Value = "Domain User Account Information"
oSheet.Cells(1,1).Font.Bold = True
oSheet.Cells(1,1).Font.Size = 10
oSheet.Range("A3:L3".Font.Bold = True
oSheet.Range("A3:L3".Interior.Color = RGB(192,192,192)
SetupCol oSheet, 3, 1, 10, "User Name"
SetupCol oSheet, 3, 2, 15, "First Name"
SetupCol oSheet, 3, 3, 20, "Last Name"
SetupCol oSheet, 3, 4, 25, "Display Name"
SetupCol oSheet, 3, 5, 7, "MailBox"
SetupCol oSheet, 3, 6, 25, "Address"
SetupCol oSheet, 3, 7, 12, "Telephone"
SetupCol oSheet, 3, 8, 36, "Fax Number"
SetupCol oSheet, 3, 9, 36, "E-Mail"
SetupCol oSheet, 3, 10, 50, "Description"
SetupCol oSheet, 3, 11, 15, "Title"
SetupCol oSheet, 3, 12, 20, "Department"
Set objConnection = CreateObject("ADODB.Connection"
Set objCommand = CreateObject("ADODB.Command"
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select distinguishedName from 'LDAP://" & strDNSPath & "' " _
& "where objectClass='user'"
objCommand.Properties("Page Size" = 1000
objCommand.Properties("Timeout" = 30
objCommand.Properties("Searchscope" = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results" = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
ix = 0
Do Until objRecordSet.EOF
strtmpusr = objRecordSet.Fields("distinguishedName".Value
ix = ix + 1
Set objUser = GetObject("LDAP://" & strtmpusr)
objUser.GetInfo
DumpAccount oSheet, ix ,objUser
objRecordSet.MoveNext
Loop
End Function
' ****************************************************
Sub SetupCol(oSheet, nRow, nCol, nWidth, sTitle)
oSheet.Cells(nRow, nCol).Value = sTitle
oSheet.Cells(nRow, nCol).ColumnWidth = nWidth
End Sub
' *****************************************************
Sub DumpAccount(oSheet, ix, objUser)
' Setup cell values
oSheet.Cells(4 + ix, 1).Value = objUser.sAMAccountName
oSheet.Cells(4 + ix, 2).Value = objUser.givenName
oSheet.Cells(4 + ix, 3).Value = objUser.sn
oSheet.Cells(4 + ix, 4).Value = objUser.displayName
oSheet.Cells(4 + ix, 5).Value = objUser.postOfficeBox
oSheet.Cells(4 + ix, 6).Value = objUser.physicalDeliveryOfficeName
oSheet.Cells(4 + ix, 7).Value = objUser.telephoneNumber
oSheet.Cells(4 + ix, 8).Value = objUser.facsimileTelephoneNumber
oSheet.Cells(4 + ix, 9).Value = objUser.mail
oSheet.Cells(4 + ix, 10).Value = objUser.description
oSheet.Cells(4 + ix, 11).Value = objUser.Title
oSheet.Cells(4 + ix, 12).Value = objUser.Department
End Sub
1. query AD for list of users
2. step through this list and get additional information
3. write this info to an excel spreadsheet
This script works perfectly on my test domain but not on my production domain. If I run it from a client as domain admin it errors on line 83 char 5 (where it calls the dumpuser sub). If I run it on a domain controller as domain admin it returns a redirect from the server. (I do not know what this is and cannot find it on MS site)
Both Domains are mixed mode and AD OU structure is the same,
but only about one percent of the number of users on test domain as on production domain. I am adding more now to see if it breaks.
If anyone can tell me what I am doing wrong or overlooking I would appreciate it.
Option Explicit
' On Error Resume Next
' Declare information variables
Dim objUser
Dim strExcelPath
Dim oSheet
Dim oExcel
Dim objFSO
Dim ix
Dim strtmpusr
Dim i
Dim objConnection
Dim objCommand
Dim objRecordSet
Dim strDNSPath
Const ADS_SCOPE_SUBTREE = 2
strExcelPath = "Path and name for excel workbook"
strDNSPath = "MyDomain"
' ****************************************************
i = Main
' Save spreadsheet and close
oExcel.ActiveWorkbook.SaveAs strExcelPath
oExcel.ActiveWorkbook.Close
Set oSheet = Nothing
Set oExcel = Nothing
wscript.echo "This script is done"
wscript.quit
' *******************************************************
Function Main
' Prepare Spreadsheet
Set oExcel = CreateObject("Excel.Application"
oExcel.Workbooks.Add
oExcel.ActiveWorkbook.Worksheets.Add
Set oSheet = oExcel.ActiveWorkbook.Worksheets(1)
oSheet.Cells.Font.Size = 8
oSheet.Name = "User Information"
oSheet.Cells(1,1).Value = "Domain User Account Information"
oSheet.Cells(1,1).Font.Bold = True
oSheet.Cells(1,1).Font.Size = 10
oSheet.Range("A3:L3".Font.Bold = True
oSheet.Range("A3:L3".Interior.Color = RGB(192,192,192)
SetupCol oSheet, 3, 1, 10, "User Name"
SetupCol oSheet, 3, 2, 15, "First Name"
SetupCol oSheet, 3, 3, 20, "Last Name"
SetupCol oSheet, 3, 4, 25, "Display Name"
SetupCol oSheet, 3, 5, 7, "MailBox"
SetupCol oSheet, 3, 6, 25, "Address"
SetupCol oSheet, 3, 7, 12, "Telephone"
SetupCol oSheet, 3, 8, 36, "Fax Number"
SetupCol oSheet, 3, 9, 36, "E-Mail"
SetupCol oSheet, 3, 10, 50, "Description"
SetupCol oSheet, 3, 11, 15, "Title"
SetupCol oSheet, 3, 12, 20, "Department"
Set objConnection = CreateObject("ADODB.Connection"
Set objCommand = CreateObject("ADODB.Command"
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select distinguishedName from 'LDAP://" & strDNSPath & "' " _
& "where objectClass='user'"
objCommand.Properties("Page Size" = 1000
objCommand.Properties("Timeout" = 30
objCommand.Properties("Searchscope" = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results" = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
ix = 0
Do Until objRecordSet.EOF
strtmpusr = objRecordSet.Fields("distinguishedName".Value
ix = ix + 1
Set objUser = GetObject("LDAP://" & strtmpusr)
objUser.GetInfo
DumpAccount oSheet, ix ,objUser
objRecordSet.MoveNext
Loop
End Function
' ****************************************************
Sub SetupCol(oSheet, nRow, nCol, nWidth, sTitle)
oSheet.Cells(nRow, nCol).Value = sTitle
oSheet.Cells(nRow, nCol).ColumnWidth = nWidth
End Sub
' *****************************************************
Sub DumpAccount(oSheet, ix, objUser)
' Setup cell values
oSheet.Cells(4 + ix, 1).Value = objUser.sAMAccountName
oSheet.Cells(4 + ix, 2).Value = objUser.givenName
oSheet.Cells(4 + ix, 3).Value = objUser.sn
oSheet.Cells(4 + ix, 4).Value = objUser.displayName
oSheet.Cells(4 + ix, 5).Value = objUser.postOfficeBox
oSheet.Cells(4 + ix, 6).Value = objUser.physicalDeliveryOfficeName
oSheet.Cells(4 + ix, 7).Value = objUser.telephoneNumber
oSheet.Cells(4 + ix, 8).Value = objUser.facsimileTelephoneNumber
oSheet.Cells(4 + ix, 9).Value = objUser.mail
oSheet.Cells(4 + ix, 10).Value = objUser.description
oSheet.Cells(4 + ix, 11).Value = objUser.Title
oSheet.Cells(4 + ix, 12).Value = objUser.Department
End Sub