Querying the AD for all OUs and their OU path

Hi guys!

Id like to build a script that does the following:

Queries our AD domain and returns a list of all OUs in the AD, and their location in AD
So,
------------------- textfile.txt
OUName       OUPath
OUName       OUPath
OUName       OUPath
OUName       OUPath
etc

Any help appreciated.
LVL 1
Simon336697Asked:
Who is Participating?
 
Ron MalmsteadConnect With a Mentor Information Services ManagerCommented:
Here....this is better...   This will create a semi-colon " ; " delimited file that you can import into excel.

strComputer = "."
Const ForWriting = 2
set WshShell = WScript.CreateObject("WScript.Shell")
wshShell.run "cmd /c echo . > c:\outputtest.txt"
Wscript.sleep 100
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\outputtest.txt", ForWriting)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\directory\LDAP")
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM ads_organizationalunit",,48)
For Each objItem in colItems
    objFile.WriteLine " " & objItem.DS_distinguishedName & " ; "  & Join(objItem.DS_ou, ",")

Next
objFile.Close
0
 
Ron MalmsteadInformation Services ManagerCommented:
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\directory\LDAP")
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM ads_organizationalunit",,48)
For Each objItem in colItems
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "ads_organizationalunit instance"
    Wscript.Echo "-----------------------------------"
    Wscript.Echo "DS_distinguishedName: " & objItem.DS_distinguishedName
    If isNull(objItem.DS_ou) Then
        Wscript.Echo "DS_ou: "
    Else
        Wscript.Echo "DS_ou: " & Join(objItem.DS_ou, ",")
    End If
Next
0
 
Ron MalmsteadInformation Services ManagerCommented:
oh sorry....here's to write it to a file.

strComputer = "."
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\outputtest.txt", ForWriting)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\directory\LDAP")
Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM ads_organizationalunit",,48)
For Each objItem in colItems
    objFile.WriteLine "-----------------------------------"
    objFile.WriteLine "ads_organizationalunit instance"
    objFile.WriteLine "-----------------------------------"
    objFile.WriteLine "DS_distinguishedName: " & objItem.DS_distinguishedName
    If isNull(objItem.DS_ou) Then
        objFile.WriteLine "DS_ou: "
    Else
        objFile.WriteLine "DS_ou: " & Join(objItem.DS_ou, ",")
    End If
Next

objFile.Close
0
 
Simon336697Author Commented:
Hi xuserx2000!

ThankU so much for your help!
S
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.