Search for a group in AD

bpl5000
bpl5000 used Ask the Experts™
on
Is there a way to search for a group using vb script?  I want to search for a group, and if it exists, return the location.  Anyway to do this?  I have code to list the members of a group, but right now I have it pointing to the Accounting OU.  I need to first search for the group, then grab the members if the group does exist.  Any help would be appreciated!
   
Dim arrNames()
intSize = 0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("members.txt", True)

strInput = UserInput( "Enter Group Name:" )

On Error Resume Next

Set objGroup = GetObject("LDAP://CN=" & strInput & ",OU=Accouting,OU=abc_Users,DC=abc,DC=local")

For Each strUser in objGroup.Member
    Set objUser =  GetObject("LDAP://" & strUser)
    ReDim Preserve arrNames(intSize)
    arrNames(intSize) = objUser.CN
    intSize = intSize + 1
Next

For i = (UBound(arrNames) - 1) to 0 Step -1
    For j= 0 to i
        If UCase(arrNames(j)) > UCase(arrNames(j+1)) Then
            strHolder = arrNames(j+1)
            arrNames(j+1) = arrNames(j)
            arrNames(j) = strHolder
        End If
    Next
Next 

For Each strName in arrNames
     objFile.WriteLine strName
Next

Wscript.Echo "Output Complete"

Function UserInput( myPrompt )
    ' Check if the script runs in CSCRIPT.EXE
    If UCase( Right( WScript.FullName, 12 ) ) = "\CSCRIPT.EXE" Then
        ' If so, use StdIn and StdOut
        WScript.StdOut.Write myPrompt & " "
        UserInput = WScript.StdIn.ReadLine
    Else
        ' If not, use InputBox( )
        UserInput = InputBox( myPrompt )
    End If
End Function

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Here you go.  Change the domain on Line 22
strInput = InputBox("Enter Group Name:")
Const ADS_SCOPE_SUBTREE = 2
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 Name, ADSPath FROM 'LDAP://dc=domain,dc=com' WHERE objectCategory='group' " 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
	If Trim(LCase(strInput)) = Trim(LCase(objRecordSet.Fields("Name").Value)) Then
		Set oGroup = GetObject(objRecordSet.Fields("ADSPath").Value)
		arrMemberOf = oGroup.GetEx("member")
		For Each member In arrMemberOf
			WScript.Echo member
		Next
		Exit Do
	End If
	objRecordSet.MoveNext
Loop

Open in new window

Commented:
Domain on Line 10 actually ^

Commented:
And here it will output the members to a text file at 'c:\members.txt'

Once again, change the domain on Line 12 this time:

strInput = InputBox("Enter Group Name:")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("c:\members.txt", True)
Const ADS_SCOPE_SUBTREE = 2
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 Name, ADSPath FROM 'LDAP://dc=domain,dc=com' WHERE objectCategory='group' " 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
	If Trim(LCase(strInput)) = Trim(LCase(objRecordSet.Fields("Name").Value)) Then
		Set oGroup = GetObject(objRecordSet.Fields("ADSPath").Value)
		arrMemberOf = oGroup.GetEx("member")
		For Each member In arrMemberOf
			Set oUser = GetObject("LDAP://" & member)
			objFile.WriteLine oUser.cN
		Next
		Exit Do
	End If
	objRecordSet.MoveNext
Loop
WScript.Echo "Output Complete."
objFile.Close

Open in new window

Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Author

Commented:
That is awesome!  The code is very concise!  The only thing I needed to add is a statement checking if the group was found.  Before the Do loop, I put "foundGroup = 0" and within the group I put "foundGroup = 1".  After the loop I put...

If foundGroup = 0 Then
      MsgBox """" & strInput & """ is not a group"
Else
      MsgBox "Output Complete"
End If

Is there a better way of doing this?  I'm thinking this is probably as brief as it can be to check if the group exists, but you seem to have an ability to lessen the lines of code and make it more concise so I'm just curious if my above code could be reduce to a few lines?
Commented:
I usually use booleans for situations like this.  They are either True or False.

strInput = InputBox("Enter Group Name:")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("c:\members.txt", True)
Const ADS_SCOPE_SUBTREE = 2
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 Name, ADSPath FROM 'LDAP://dc=domain,dc=com' WHERE objectCategory='group' " 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
bFound = False
Do Until objRecordSet.EOF
        If Trim(LCase(strInput)) = Trim(LCase(objRecordSet.Fields("Name").Value)) Then
		bFound = True
                Set oGroup = GetObject(objRecordSet.Fields("ADSPath").Value)
                arrMemberOf = oGroup.GetEx("member")
                For Each member In arrMemberOf
                        Set oUser = GetObject("LDAP://" & member)
                        objFile.WriteLine oUser.cN
                Next
                Exit Do
        End If
        objRecordSet.MoveNext
Loop
If bFound Then
	MsgBox("Output Complete.")
Else
	MsgBox("Group """ & strInput & """ not found.")
End If
objFile.Close

Open in new window

Author

Commented:
Thanks for all the help, josika!  Now I have decided to make this script program into a VB.NET program.  If you're a VB.NET guru too, then maybe you can help me.  I have posted the code in the vb.net zone.

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_27244857.html

Commented:
Sorry haven't used VB.NET in over 4 years.  Not too familiar with the language anymore.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial