Solved

Script not working to query a certain query

Posted on 2014-02-17
3
305 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 22

Accepted Solution

by:
yo_bee earned 500 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 22

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…

777 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