'****************************************************
'		Script Witten by Larry Heintz
'		October 2006 www.windowsadminscripts.com
' This script will connect to the AD DC and enumerate
' all OU's (Organization Units) and the users and the 
' users description field in the OU. It will also log
' the OU's and all users in the OU.
'****************************************************
Dim logPath,args,adstring
Set args = Wscript.Arguments.Named
adstring = trim(args.item("DC"))
logPath = getLogPath()

if wscript.arguments.count = 0 then
	Call usage()
	wscript.quit
elseif adstring = "" then
	Call usage()
	wscript.quit
elseif args.exists("DC") then
	Call enumGroups(getLDAP(adstring))
else
	Call usage()
	wscript.quit
end if

Function getLDAP(adstring)
Dim temp,temp2
temp = split(adstring,".")
for i = 0 to ubound(temp)
	temp2 = temp2 & "DC=" & temp(i) & ","
next
getLDAP = left(temp2,(len(temp2)-1))
End Function

Function enumGroups(getLDAP)
Dim objgroup,egroup,count
count = 0
wscript.echo "Starting script..."
Set objgroup = GetObject("LDAP://" & getLDAP)
	getOUs (objgroup)
Set objgroup = nothing
wscript.echo ""
wscript.echo "Ending script..."
End Function

Sub getOUs (Adomain)
	For each ou in Adomain
		If ou.Class = "organizationalUnit" Then
			writetoOUList(ou.distinguishedName)
			enumUsersGroup(ou.distinguishedName)
			'wscript.echo ou.distinguishedName	'Uncomment start of line if you to see text
		getOUs (ou)
		End If
	Next
End Sub

Function enumUsersGroup(ou)
Dim objgroup,member,count
Dim temp,temp1
Set objgroup = GetObject("LDAP://" & ou)
temp = split(ou,",")
temp1 = split(temp(0),"=")
objgroup.Filter = Array("User")
For Each objItem in objgroup
    'Wscript.Echo objItem.CN & "," & objItem.Description & "," & temp1(1) 	'Uncomment start of line if you to see text
    Call writetoLog(objItem.CN,objItem.Description,temp1(1))
Next
Set objgroup = nothing
End Function

Function writetoOUList(ou)
Dim FSO,objFSOwriteline
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(logPath & "\OU_List.txt") = True Then
	Set objFSOwriteline = FSO.OpenTextFile(logPath & "\OU_List.txt", 8,True)
		objFSOwriteline.WriteLine(ou)
		objFSOwriteline.close
Else
	Set objFSOwriteline = FSO.CreateTextFile(logPath & "\OU_List.txt")
		objFSOwriteline.WriteLine(ou)
		objFSOwriteline.close
End If
Set objFSOwriteline = nothing
Set FSO = nothing
End Function

Function writetoLog(username,description,group)
Dim FSO,objFSOwriteline
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FileExists(logPath & "\" & group & ".txt") = True Then
	Set objFSOwriteline = FSO.OpenTextFile(logPath & "\" & group & ".txt", 8,True)
		objFSOwriteline.WriteLine(username & "," & description)
		objFSOwriteline.close
Else
	Set objFSOwriteline = FSO.CreateTextFile(logPath & "\" & group & ".txt")
		objFSOwriteline.WriteLine(username & "," & description)
		objFSOwriteline.close
End If
Set objFSOwriteline = nothing
Set FSO = nothing
End Function

Function getLogPath()
Dim temp,temp2
temp = split(wscript.scriptfullname,"\")
for i = 0 to ubound(temp) - 1
	temp2 = temp2 & temp(i) & "\"
next
getLogPath = temp2
End Function

Function usage()
	wscript.echo "Script Usage:cscript enumou.vbs /dc:""[AD Domain Conroller]"""
End Function
