Solved

How to get computer objects with OU path from AD?

Posted on 2007-04-09
8
3,085 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
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

821 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