powershell OU computer question

tdodd72
tdodd72 used Ask the Experts™
on

I have this script below that goes in a certain OU and displays all the computer accounts. Is there a way where run this recursively through sub OU's and pull computer accounts below along with the lastlogonstamp of these computers.



$OU = [ADSI]"LDAP://OU=Computers,OU=feet,OU=hair,ou=AD,DC=something,DC=local"
foreach ($child in $ou.psbase.children) {
 if($child.ObjectCategory -like '*computer*'){write-host $child.Name
}}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You would have to check if the object was a OU and then if it was loop theough
$child.ObjectCategory -like 'organizationunit'

I would recomend the Quest ADCmdlets.

get-qadcomputer  -searchroot "OU=Computers,OU=feet,OU=hair,ou=AD,DC=something,DC=local" -recurse | ft name, lastlogontimestamp

Author

Commented:
Ken thanks for the response but I would like to do this with ADSI/LDAP/Powershell
PowerShell Developer
Top Expert 2010
Commented:

Then you would be better using the DirectorySearcher:


$SearchRoot = [ADSI]"LDAP://OU=Computers,OU=feet,OU=hair,ou=AD,DC=something,DC=local"
(New-Object DirectoryServices.DirectorySearcher($SearchRoot, "(objectCategory=computer)").FindAll() |
  Select-Object @{n='Name';e={ $_.Properties["name"][0] }}


It is possible to recurse using the DirectoryEntry ([ADSI]) only, but it's a bit unnecessary and won't perform as well as the search.

This is the kind of thing tools like Get-QADComputer (and so on) wrap around, they provide a much simpler interface, System.DirectoryServices has some quirks which can take a bit of getting used to.

Chris
Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Author

Commented:
Chris the following produces errors when trying this

$SearchRoot = [ADSI]"LDAP://OU=Computers,OU=feet,OU=hair,ou=AD,DC=something,DC=local"
(New-Object DirectoryServices.DirectorySearcher($SearchRoot, "(objectCategory=computer)").FindAll() |
  Select-Object @{n='Name';e={ $_.Properties["name"][0] }}

Method invocation failed because [System.Object[]] doesn't contain a method named 'FindAll'.
At line:2 char:98
+ (New-Object DirectoryServices.DirectorySearcher($SearchRoot, "(objectCategory=computer)").FindAll <<<< () |
    + CategoryInfo          : InvalidOperation: (FindAll:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound
Chris DentPowerShell Developer
Top Expert 2010

Commented:

Apologies, I missed a ) off.

Chris
$SearchRoot = [ADSI]"LDAP://OU=Computers,OU=feet,OU=hair,ou=AD,DC=something,DC=local"
(New-Object DirectoryServices.DirectorySearcher($SearchRoot, "(objectCategory=computer)")).FindAll() |
  Select-Object @{n='Name';e={ $_.Properties["name"][0] }}

Open in new window

Author

Commented:
Thanks this works like a charm. However, my biggest thing was getting the lastlogonstamp as I placed this is my original comment. Any clues on how, thanks.
Chris DentPowerShell Developer
Top Expert 2010

Commented:

So you did, I've added that in to this version.

You're likely to get some blank values, as far as AD is concerned those will not have logged on yet.

If you want to export this to a file, add this after the last }:

| Export-Csv SomeFile.csv -NoTypeInformation

Chris
$SearchRoot = [ADSI]"LDAP://OU=Computers,OU=feet,OU=hair,ou=AD,DC=something,DC=local"
(New-Object DirectoryServices.DirectorySearcher($SearchRoot, "(objectCategory=computer)")).FindAll() |
  Select-Object `
    @{n='Name';e={ $_.Properties["name"][0] }},
    @{n='LastLogonTimeStamp';e={ [DateTime]::FromFileTime($_.Properties["lastlogontimestamp"][0]) }}

Open in new window

Author

Commented:
You rock thank you so much.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial