Forum Discussion
CSullivan55
Aug 30, 2023Copper Contributor
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 t...
- Aug 31, 2023
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
LainRobertson
Aug 31, 2023Silver 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
- CSullivan55Aug 31, 2023Copper 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