Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to get computer objects with OU path from AD?

Posted on 2007-04-09
8
Medium Priority
?
3,099 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 1600 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 200 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 200 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

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…
Wouldn't it be nice if objects in Active Directory automatically moved into the correct Organizational Units? This is what AutoAD aims to do and as a plus, it automatically creates Sites, Subnets, and Organizational Units.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 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