Hi all. I am beating myself up on a rather simple thing, but I keep missing something. I am sure that I will feel like an idiot once you guys help me out of it.
Anyway, I need to write a query to report out users from a domain that meet certain criteria. That is simple. There a couple exceptions:
- Do not report users in certain OU's (have not finished this part...just grabbing Parent as of now)
- Do not report users from a specific list in a text file. The list has an email on each new line.
This is the one I need help with. My loop is messed up and I cannot get it to work. It will report back data now, but the 2 loops are not working together if you know what i mean. Here is the code I am working on. Please excuse it, it is just dev right now. Once this is fixed I plan to clean it up and add the OU exclusion.
Thanks!
Anyway, I need to write a query to report out users from a domain that meet certain criteria. That is simple. There a couple exceptions:
- Do not report users in certain OU's (have not finished this part...just grabbing Parent as of now)
- Do not report users from a specific list in a text file. The list has an email on each new line.
This is the one I need help with. My loop is messed up and I cannot get it to work. It will report back data now, but the 2 loops are not working together if you know what i mean. Here is the code I am working on. Please excuse it, it is just dev right now. Once this is fixed I plan to clean it up and add the OU exclusion.
Code:
Option Explicit
Dim objRootDSE, strDNSDomain, objCommand, objConnection
Dim strQuery, strBase, strFilter, strAttributes
Dim objRecordSet, strName, objUser, strParent, objParent
Dim strDN, strFirst, strLast, strEmail, strEmpID
Dim objFSO, objExcludeFile, strBadOU, strEmailCompare
Dim strPreferredEmail, objExcel, intRow, strExcelPath
Dim strExcludeFile, strExcludeFlag, strNextLine
Const ForReading = 1, ForWriting = 2, ForAppending = 8
strBadOU = "test"
intRow = "1"
strExcelPath = "c:\OutPutFile.xls"
strExcludeFile = "c:\test.txt"
Set objExcel = SetupExcel(strExcelPath)
objExcel.Cells(intRow, 1).Value = "Name"
objExcel.Cells(intRow, 2).Value = "First"
objExcel.Cells(intRow, 3).Value = "Last"
objExcel.Cells(intRow, 4).Value = "EmployeeID"
objExcel.Cells(intRow, 5).Value = "Email"
objExcel.Cells(intRow, 6).Value = "Preferred Email"
objExcel.Rows(intRow).Interior.ColorIndex = 6
objExcel.Rows(intRow).Font.Bold = True
intRow = intRow + 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strExcludeFile) Then
Set objExcludeFile = objFSO.OpenTextFile("c:\test.txt", ForReading)
strExcludeFlag = True
Else
strExcludeFlag = False
End If
' Determine DNS domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
' Use ADO to search Active Directory.
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
' Search for all user objects. Return Values.
strBase = "<LDAP://" & strDNSDomain & ">"
strFilter = "(&(objectCategory=person)(objectClass=user)(homeMDB=*)(!userAccountControl:1.2.840.113556.1.4.803:=2))"
strAttributes = "displayName,distinguishedName,givenName,sn,employeeID,mail"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
objCommand.Properties("Sort On") = "displayName"
Set objRecordSet = objCommand.Execute
Do Until objRecordSet.EOF
strName = objRecordSet.Fields("displayName")
strDN = objRecordSet.Fields("distinguishedName")
strFirst = objRecordSet.Fields("givenName")
strLast = objRecordSet.Fields("sn")
strEmpID = objRecordSet.Fields("employeeID")
strEmail = objRecordSet.Fields("mail")
Set objUser = GetObject("LDAP://" & strDN)
strParent = objUser.Parent
Set objParent = GetObject(strParent)
strEmailCompare = Split(strEmail,"@")
If strEmailCompare(0) <> strFirst Then
strPreferredEmail = strEmailCompare(0)
Else
strPreferredEmail = ""
End If
If objUser.AccountDisabled = False And Replace(objParent.Name,"CN=","") <> strBadOU And strExcludeFlag = True Then
'Wscript.Echo strName & ";" & Replace(objParent.Name,"CN=","") & ";" & strPreferredEmail
Do Until objExcludeFile.AtEndOfStream
strNextLine = objExcludeFile.Readline
If strNextLine <> strEmail Then
objExcel.Cells(intRow, 1).Value = strName
objExcel.Cells(intRow, 2).Value = strFirst
objExcel.Cells(intRow, 3).Value = strLast
objExcel.Cells(intRow, 4).Value = strEmpID
objExcel.Cells(intRow, 5).Value = strEmail
objExcel.Cells(intRow, 6).Value = strPreferredEmail
intRow = intRow + 1
End If
Loop
End If
objRecordSet.MoveNext
Loop
objConnection.Close
Call ExcelCleanUp(objExcel, strExcelPath)
Function SetupExcel(strExcelPath)
WScript.Echo strExcelPath
' Check for required arguments.
If Not strExcelPath <> "" Then
WScript.Echo "Argument <SpreadsheetName> required. For example:" _
& VbCrLf _
& "blah" 'fix
Wscript.Quit(0)
End If
' Bind to Excel object.
On Error Resume Next
Dim objExcel : Set objExcel = CreateObject("Excel.Application")
If Err.Number <> 0 Then
WScript.Echo "Excel application not found."
Wscript.Quit
End If
On Error GoTo 0
' Open spreadsheet.
On Error Resume Next
objExcel.Workbooks.Add
objExcel.Visible = True
If Err.Number <> 0 Then
On Error GoTo 0
WScript.Echo "Spreadsheet cannot be opened: " & strExcelPath
Wscript.Quit
End If
On Error GoTo 0
Set SetupExcel = objExcel
End Function
Sub ExcelCleanUp(objExcel, strExcelPath)
Dim objRange
Set objRange = objExcel.ActiveWorkbook.Worksheets(1).UsedRange
objRange.EntireColumn.Autofit()
objExcel.ActiveWorkbook.SaveAs strExcelPath
objExcel.Quit
End Sub
Thanks!