Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Script not working to query a certain query

Posted on 2014-02-17
3
Medium Priority
?
312 Views
Last Modified: 2014-02-17
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?



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

Open in new window

0
Comment
Question by:mystikal1000
  • 2
3 Comments
 
LVL 24

Accepted Solution

by:
yo_bee earned 2000 total points
ID: 39865478
Have you confirmed that you are catching the IT- part of the string.

If CN=IT-xxxxx

I would think if you are starting at the 4 character which is I of IT-, but it looks like you are only comparing 3 characters not 4.
if that is the case you probably not going to get any true results.

I would try to parse the Group name and with your MID function and see what the output is.
0
 
LVL 1

Author Comment

by:mystikal1000
ID: 39865644
Whoops, thanks!
0
 
LVL 24

Expert Comment

by:yo_bee
ID: 39865675
Here is an alt method.

Replace Line 29-33
Set colGroups = ObjUser.Groups
For Each objGroup in ColGroups

     If mid(strGroup,1,3)="IT-" Then  
            objOutput.WriteLine objGroup.CN

Open in new window


    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

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question