Solved

How to get computer objects with OU path from AD?

Posted on 2007-04-09
8
3,086 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html?cid=Gene_Skyport) provided 218 attendees with a step-by-step guide for…
A hard and fast method for reducing Active Directory Administrators members.
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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