mystikal1000
asked on
Script not working to query a certain query
I am trying to query users in AD and only receive output that have a certain group that start with 'IT-'
I am not getting anything in the output, any reasons why?
I am not getting anything in the output, any reasons why?
Const ADS_SCOPE_SUBTREE = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
strDomain = "dc=domain,dc=com"
strUsers = "c:\Users.txt"
Set objOutput = objFSO.CreateTextFile("c:\output.txt")
aryUsers = Split(objFSO.OpenTextFile(strUsers).ReadAll, vbNewLine)
For Each strUser In aryUsers
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
objCommand.CommandText = _
"SELECT ADsPath FROM 'LDAP://" & strDomain & "' WHERE objectCategory='user' " & _
"AND samAccountName = '" & strUser & "'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
If Not objRecordSet.EOF Then
Do Until objRecordSet.EOF
strADsPath = objRecordSet.Fields("ADsPath").Value
Set objUser = GetObject(strADsPath)
objOutput.WriteLine objUser.Name & " is a member of the following groups:"
For Each strGroup In objUser.memberOf
If mid(strGroup,4,4)="IT-" Then
Set objGroup = GetObject("LDAP://" & strGroup)
objOutput.WriteLine objGroup.CN
End If
Next
objOutput.WriteLine
objRecordSet.MoveNext
Loop
Else
objOutput.WriteLine strUser & " does not have an AD account!"
End If
Next
objOutput.Close
Set objOutput = Nothing
Set objGroup = Nothing
Set objUser = Nothing
Set objFSO = Nothing
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is an alt method.
Replace Line 29-33
Replace Line 29-33
Set colGroups = ObjUser.Groups
For Each objGroup in ColGroups
If mid(strGroup,1,3)="IT-" Then
objOutput.WriteLine objGroup.CN
Do Until objRecordSet.EOF
strADsPath = objRecordSet.Fields("ADsPath").Value
Set objUser = GetObject(strADsPath)
objOutput.WriteLine objUser.Name & " is a member of the following groups:"
Set colGroups = ObjUser.Groups
For Each objGroup in ColGroups
If mid(strGroup,1,3)="IT-" Then
objOutput.WriteLine objGroup.CN
End If
ASKER