Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

Query a OU and all OU's below it and get the details

Query a OU and all OU's below it and get the Display name,Description,Email id and forwarded user name and email id.
We have a contact set as forward thats in a different OU. So need a combined list from it
Avatar of Nuno Martins
Nuno Martins
Flag of Portugal image

Hi There,

You can use this tool :
http://www.manageengine.com/products/free-windows-active-directory-tools/free-windows-active-directory-query-tool.html
is very easy to use and to configure.
If you prefer i can post the correct syntax for you to query from command prompt but i perfer that tool to make that kind of query ;)

NM
Use CSVDE:

csvde -f OUTPUT.CSV -d "cn=users,DC=DOMAINNAME,DC=Microsoft,DC=Com" -r "(objectClass=user)"

You can also add the -l switch and specify specific attributes such as sAMAccountName
Avatar of RobSampson
Hi, try this.

Regards,

Rob.
strOU = "OU=SecondOU,OU=FirstOU,DC=domain,DC=com"
strOutput = "UserForwards.csv"

' Setup FSO objects.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objOutput = objFSO.CreateTextFile(strOutput, True)
objOutput.WriteLine """Username"",""DisplayName"",""Description"",""Email"",""Alt Username"",""Alt DisplayName"",""Alt Email"""

' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection
strBase = "<LDAP://" & strOU & ">"
strFilter = "(&(objectCategory=person)(objectClass=user))"
strAttributes = "adsPath"
strScope = "subtree"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";" & strScope
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

Set adoRecordset = adoCommand.Execute
Do Until adoRecordset.EOF
	Set objUser = adoRecordset.Fields("adsPath").Value
	strUsername = objUser.samAccountName
	strDisplayName = objUser.DisplayName
	strDescription = objUser.Description
	strEmail = objUser.mail
	strAltRecipient = objUser.altRecipient
	If strAltRecipient <> "" Then
		Set objForwardedUser = GetObject("LDAP://" & strAltRecipient)
		strForwardedUsername = objForwardedUser.samAccountName
		strForwardedDisplayName = objForwardedUser.displayName
		strForwardedEmail = objForwardedUser.mail
	Else
		strForwardedUsername = ""
		strForwardedDisplayName = ""
		strForwardedEmail = ""
	End If
	objOutput.WriteLine """" & strUsername & """,""" & strDisplayName & """,""" & strDescription & """,""" & strEmail & """,""" & strForwardedUsername & """,""" & strForwardedDisplayName & """,""" & strForwardedEmail & """"
	adoRecordset.MoveNext
Loop

adoRecordset.Close
adoConnection.Close
      
objOutput.Close

WScript.Echo "Finished"

Open in new window

Avatar of bsharath

ASKER

Thanks Rob
I get this

---------------------------
Windows Script Host
---------------------------
Script:      D:\Get details AD with forwards.vbs
Line:      25
Char:      1
Error:      Table does not exist.
Code:      80040E37
Source:       Provider

---------------------------
OK  
---------------------------
You will need to make sure that this line is structured properly:
strOU = "OU=SecondOU,OU=FirstOU,DC=domain,DC=com"

and do not put the LDAP:// at the start.

Rob.
Now get this


---------------------------
Windows Script Host
---------------------------
Script:      D:\Get details AD with forwards.vbs
Line:      27
Char:      2
Error:      Object required: '[string: "LDAP://CN=Uha Si,O"]'
Code:      800A01A8
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
Oops, plase change
      Set objUser = adoRecordset.Fields("adsPath").Value

to
      Set objUser = GetObject(adoRecordset.Fields("adsPath").Value)

Regards,

Rob.
perfect Rob
One change
Can we have 4 OU's excluded within the root we scan
Do you want each OU, and then each sub OU excluded?

For example, if you scan
domain.com/FirstOU/SecondOU

and then exclude
domain.com/FirstOU/SecondOU/ThirdOU

do you also want it to exclude
domain.com/FirstOU/SecondOU/ThirdOU/FourthOU

as well, or ONLY ThirdOU?

Rob.
I scan OURoot
Exclude 3 OU's that i mention exactly
Not the Ou;s that are within the excluded OU

Say i have

>OU1
>>OU2
>>OU3

If i exclude OU1 then OU2 and OU3 has to be scanned
If excluded OU3 then OU1 and OU2 should be scanned
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial