We help IT Professionals succeed at work.

Retrieve a list of computers grom a particular OU

YZlat
YZlat asked
on
Medium Priority
433 Views
Last Modified: 2012-05-11
Can someone help me with a script that would return a list of computer objects from a particular AD group.

Here is what I got so far:

 
$ou = [ADSI]"LDAP://OU=MyGroup,OU=Section1,DC=domainname,DC=com"
foreach ($child in $ou.psbase.Children) { 
    	if ($child.ObjectCategory -like '*computer*') { 
		Write-Host $child.Name 
	} 
}

Open in new window

Comment
Watch Question

LDAP://OU=YOUROU,DC=DOMAIN,DC=LOCALldap://OU=YOUROU,DC=DOMAIN,DC=LOCAL">LDAP://OU=YOUROU,DC=DOMAIN,DC=LOCAL</A< A>>
foreach ($child in $ou.psbase.Children )
{
  if ($child.ObjectCategory -like '*computer*')
{
Write-Host $child.Name
}
}
CERTIFIED EXPERT

Author

Commented:
BlackSnowman, that's exactly what I have.

Except, where do you assign a value to variable $ou?
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Try this version:
$ADsPath = [ADSI]"LDAP://OU=Ellsworth,DC=Ellsworth,DC=com"
$Search = New-Object DirectoryServices.DirectorySearcher($ADsPath)
$Search.filter = "(objectClass=computer)"
$Search.PageSize = 1000
$Search.SearchScope = "subtree" 
$results = $Search.Findall()

foreach ($result in $results)
{
    $Computer = $result.GetDirectoryEntry()
   $Computer.Name
}

Open in new window

CERTIFIED EXPERT

Author

Commented:
no error, it just does not do anything, no output to the screen
Did you change the 1st line to point to your OU and domain?
Where in AD are the computers you are trying to list?
Was your comment to my question about the error, or my new script?
CERTIFIED EXPERT

Author

Commented:
rlandquist, my code worked, turns out I was not running it the correct way.

Could you tell me how to put all the returned data into an array instead of printing it to the screen?
Please show the code you are using now.
CERTIFIED EXPERT

Author

Commented:
Nevermind, I figured it out. I have another question but since it is a different question, I am gonna open a new request. Could you help me there?

https://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_26971979.html
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.