[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 748
  • Last Modified:

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.
0
Simon336697
Asked:
Simon336697
  • 3
1 Solution
 
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
 
Ron MalmsteadInformation 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
 
Simon336697Author Commented:
Hi xuserx2000!

ThankU so much for your help!
S
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now