Forum Discussion
Get computer objects based on list of OUs
I have a query that will get OUs which block GP inheritance. I am not able to use get-adcomputer to search through that OU list. I can't find a way to get the -searchbase parameter to cycle through the OU names. I've also tried get-adcomputer -filter * | where-object DistinguishedName -match "OU=blahblah,DC=myorg,DC=com" This command works if I enter one of the OU names, but I also can't find a way to cycle through the list that way.
So how do I get the computer objects from a list of OU names?
Thanks for any help.
Here's a very basic example.
Get-ADObject -Filter { (objectClass -eq "organizationalUnit") -and (gPOptions -eq 1) } | ForEach-Object { Get-ADComputer -Filter * -SearchBase ($_.distinguishedName); }
Note: If inheritance is blocked on one organisational unit, and then blocked again in a nested organisational unit, this could result in a computer being listed twice (once for each search).
You could script your way around this but I've ignored this scenario in order to keep things basic.
Cheers,
Lain
3 Replies
- Leandro1960Copper Contributorolar boa noite eu acho tem que ser melhorado em quais tudo pra chamar mais atença público.
- LainRobertsonSilver Contributor
Here's a very basic example.
Get-ADObject -Filter { (objectClass -eq "organizationalUnit") -and (gPOptions -eq 1) } | ForEach-Object { Get-ADComputer -Filter * -SearchBase ($_.distinguishedName); }
Note: If inheritance is blocked on one organisational unit, and then blocked again in a nested organisational unit, this could result in a computer being listed twice (once for each search).
You could script your way around this but I've ignored this scenario in order to keep things basic.
Cheers,
Lain
- CSullivan55Copper ContributorThanks much, absolutely perfect! Adding to it slightly, this gives us just what we need and duplicates are not a problem:
Get-ADObject -searchbase "OU=foo,DC=myorg,DC=com" -Filter { (objectClass -eq "organizationalUnit") -and (gPOptions -eq 1) } | ForEach-Object { Get-ADComputer -Filter * -SearchBase ($_.distinguishedName); } | Select-Object DistinguishedName | export-csv .\cmp-NI.csv -NoTypeInformation -Append