Solved

How to get computer objects with OU path from AD?

Posted on 2007-04-09
8
3,082 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
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…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now