Link to home
Start Free TrialLog in
Avatar of sebastienbo
sebastienbo

asked on

How to get computer objects with OU path from AD?

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
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
I believe my post satisfied the question asked.
Flip a coin, any of the 3 solutions (vbscript, csvde, adfind) were workable.
Avatar of sebastienbo
sebastienbo

ASKER

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.