Monday, January 23, 2006

'LDAP Query Script
'Written By Lee Mason
'Purpose: Uses ADSI To Query an OU and its subcontainers for users that are
'NOT members of a specified group
'then writes out to a text file.
Option Explicit
'Declare and obtain Variables
Dim ObjLocation
Dim ObjMember
Dim ObjGroup
Dim FilOutputMembers
Dim FilOutputNonMembers
Dim fso
Dim ObjectType
Dim arrSubContainers(4)
dim i

dim strtargetgroup 'the group you want to find
dim strtoplevelOU 'the first part of your domain, ie for "", this
would be "mycompany"
dim strsecondlevelOU 'co
dim strthirdlevelOU 'uk
'add more in here if you need them - don't forget to add them to the getobject
statement below!
'enumerate subcontainer names in an array for looping through
ObjectType = "<subOU1>"
arrsubcontainers(0) = "<subOU2>"
arrsubcontainers(1) = "<subOU3>"
arrsubcontainers(2) = "<subOU3>"
arrsubcontainers(3) = "<subOU4>"

'set reference to PC-Duo Group

set ObjGroup = GetObject("LDAP://cn=" & strtargetgroup &amp; ",ou=" & strtoplevelOU
&amp; ",ou=" & strsecondlevelOU &amp; ",dc=" & strthirdlevelOU)

'Instantiate Output Files using FilesystemObject

Set fso = CreateObject("Scripting.FileSystemObject")

Set FilOutputMembers = fso.CreateTextFile("\\\dfs\track-IT\Reporting\Group
Lists\" &amp; & " Members.txt", True)

With FiloutputMembers

.WriteLine("Member List")


.writeline("Generated " &amp; Date())

.writeline(" ")

End With

Set FilOutputNonMembers = fso.CreateTextFile("\\\dfs\track-IT\Reporting\Group
Lists\" & & " NonMembers.txt", True)

With FiloutputNonMembers

.WriteLine("Non-Member List")


.writeline("Generated " &amp; Date())

.writeline(" ")

End With

'Bind to Ads group for each subcontainer

for i=0 to 4

'Set reference to teh subcontainers contained in the array

Set objLocation = GetObject("LDAP://ou=" & arrsubcontainers(i) &amp; ",ou=" &
Objecttype & strtoplevelOU & ",dc=" & strsecondlevelOU &amp; ",dc=" &


'test for membership and write result out to appropriate file

For Each ObjMember In ObjLocation

If ObjGroup.IsMember("LDAP://" &amp; & ",ou=" & arrsubcontainers(i) &
",ou=" & Objecttype & ",dc=" & strsecondlevelOU &amp; ",dc=" & strthirdlevelOU) Then

filoutputMembers.WriteLine(arrsubcontainers(i) &amp; " " &


filoutputNonMembers.WriteLine(arrsubcontainers(i) &amp; " " &

End If



No comments:

Breaking News