Solved

How to get computer objects with OU path from AD?

Posted on 2007-04-09
8
3,091 Views
Last Modified: 2010-05-18
I wonderd if there is a way to get a list of AD computer objects together with the OU path?

Example:
/computers/homecomputers/XPmachines/Host1.mydomain.test
/computers/homecomputers/XPmachines/Host2.mydomain.test
/computers/homecomputers/NTmachines/Host1.mydomain.test
0
Comment
Question by:sebastienbo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 400 total points
ID: 18878884
This will not only get the PCs, but their IP addresses to (I recently wrote this, and can remove the IP info, if you'd like).

'PollPCs.vbs
'//Declarations
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objOutput: Set objOutput=objFSO.CreateTextFile("C:\Output.txt")
Dim objRoot: Set objRoot=GetObject("LDAP://RootDSE")
Dim objDomain: Set objDomain=GetObject("LDAP://" & objRoot.Get("defaultNamingContext")

'//Main routine
EnumOUs (objDomain.AdsPath)

'//Close output and cleanup
objOutput.Close
Set objOutput=Nothing
Set objRoot=Nothing
Set objDomain=Nothing
Set objFSO=Nothing

Sub EnumOUs(AdsPath)
  Dim objOUs: Set objOUs=GetObject(AdsPath)
  objOUs.Filter=Array("OrganizationalUnit")
  objOutput.Writeline "Processing " & Replace(objOU.Name, "OU=","")
  Call EnumPCs(objOUs.AdsPath)
  For Each OU in objOUs
    Call EnumOUs(OU.AdsPath)
  Next
  Set OU=Nothing
  Set objOUs=Nothing
End Sub

Sub EnumPCs(AdsPath)
  Dim objPCs:Set objPCs=GetObject(AdsPath)
  objPCs.Filter=Array("Computer")
  For Each PC in objPCs
      objOutput.WriteLine vbTab & "Attempting to contact " & PC.CN
      PingPC(PC.CN)
  Next
  Set PC=Nothing  
  Set objPCs=Nothing
End Sub
 
Sub PingPC(pc)
  Dim objShell:Set objShell=CreateObject("Wscript.Shell")
  Dim objExec:Set objExec=objShell.Exec("ping -n 2 -w 1000 " & pc)
  strResults=lcase(objExec.StdOut.ReadAll)
    strPing=mid(strResults,instr(strResults,"[")+1,instrRev(strResults,"]")-instr(strResults,"[")-1)
    objOutput.WriteLine vbTab & vbTab & pc & " returned " & strPing
  Set objExec=Nothing
  Set objShell=Nothing
End Sub
0
 
LVL 30

Assisted Solution

by:LauraEHunterMVP
LauraEHunterMVP earned 50 total points
ID: 18879523
Though it won't return the IP address, you can also use the adfind command-line tool (a free download from www.joeware.net). The following will return the distinguished name (DN) of every computer object in your domain:

adfind -default -f (objectcategory=computer) dn

Hope this helps.

Laura E. Hunter - Microsoft MVP: Windows Server - Networking
0
 
LVL 26

Assisted Solution

by:Pber
Pber earned 50 total points
ID: 18881838
How about this:

csvde -r (objectclass=computer) -l canonicalName -f c:\pc.csv

This will dump all computers to a comma delimted file called pc.csv
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 65

Expert Comment

by:RobSampson
ID: 18887008
I wrote this procedure into an HTA program:
'========================================
Sub Filter_List_By_Site

Dim strDomainName
Dim objIADsContainer, objIADsComputer, objActiveOption
Dim strOUBits, arrOUBits, intOUBit

'MsgBox lstSiteFilter.Value
        
strDomainName = "YOURDOMAIN"
strComputers = ""
        
'MsgBox lstSiteFilter.Item(lstSiteFilter.SelectedIndex).Text

If lstSiteFilter.Item(lstSiteFilter.SelectedIndex).Text <> "Custom..." Then
  strOUBits = "Your.FQDN\"
      
  arrOUBits = Split(lstSiteFilter.Value, ",")

  For intOUBit = UBound(arrOUBits) -2 To LBound(arrOUBits) Step -1
      strOUBits = strOUBits & Replace(Trim(arrOUBits(intOUBit)), "OU=", "") & "\"
  Next
      
  spanSiteFilter.InnerHTML = strOUBits
  'spanSiteFilter.InnerHTML = lstSiteFilter.Value
                      
  If lstSiteFilter.Item(lstSiteFilter.SelectedIndex).Text = "  -- All --  " Then
      Set objIADsContainer = GetObject("WinNT://" & strDomainName)
      objIADsContainer.Filter = Array("Computer")
  Else
      Set objIADsContainer = GetObject("LDAP://" & lstSiteFilter.Value)
      objIADsContainer.Filter = Array("Computer")
  End If
      
  For Each objIADsComputer In objIADsContainer
      If Left(objIADsComputer.Name, 3) = "CN=" Then
            strComputers = strComputers & Replace(objIADsComputer.Name, "CN=", "") & VbCrLf
      End If
  Next
      
Else
      spanSiteFilter.InnerHTML = ""
End If
End Sub
'=================================

In this case the selected option from the list box (lstSiteFilter.Item(lstSiteFilter.SelectedIndex).Text)
contains something like "Your.FQDN\Sites\Civic Centre\Computers\IT Computers\" and this procedure
returns the computers into another list box.  You could just messagebox these out for testing.

Make sure you replace YOURDOMAIN with your domain name, and in the code, replace
Your.FQDN with your fully qualified domain name, like Microsoft.Local or something.

It's quite similar to Sir Bounty's.  In my HTA I've also enumerated the OU's in the first place,
so I can select an OU from the list box and get the computers.

Hope that helps.

Regards,

Rob.
0
 
LVL 26

Expert Comment

by:Pber
ID: 19022775
I believe my post satisfied the question asked.
0
 
LVL 30

Expert Comment

by:LauraEHunterMVP
ID: 19022793
Flip a coin, any of the 3 solutions (vbscript, csvde, adfind) were workable.
0
 
LVL 5

Author Comment

by:sebastienbo
ID: 19139931
SirBounty's solution was the most portable solution So I gave him the most points.

I also like the different approuch of LauraEHunterMVP and Pber by launching something external.
In most cases this makes coding less complicated (but dependant from those tools)

So I thank you all for the solutions, every one of them gave me the result that I was looking for.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question