If I run the first part (ending at $Search.FinadAll() ) I get the list of groups that fit the query *PCSupport. But if I runit it fails on the rest of the groups/domains with
Get-ADGroupMember : Cannot find an object with identity: 'CN=PCSupport,OU=Groups,DC=domain2,DC=company,DC=com' under: 'DC=domain1,DC=company,DC=com'.
$forestName= ([System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()).Name
$ADsPath= [ADSI]"GC://$forestName"
$Search=New-ObjectSystem.DirectoryServices.DirectorySearcher($ADsPath)
$Search.Filter="(&(objectCategory=group)(SamAccountName=*PCSupport))"
$Search.FindAll() | ForEach {
$group=$_.Properties.cn
$path=$_.Properties.distinguishedname | Select -Unique
Get-ADGroupMember-Identity$path |
Select @{N='Group';E={$group}},SamAccountName
} | Sort Group,SamAccountName |
Export-Csv .\groupList.csv-NoTypeInformation
$ADsPath= [ADSI]"GC://$forestName"
$Search=New-ObjectSystem.DirectoryServices.DirectorySearcher($ADsPath)
$Search.Filter="(&(objectCategory=group)(SamAccountName=*PCSupport))"
$Search.FindAll() | ForEach {
$group=$_.Properties.cn
$path=$_.Properties.distinguishedname | Select -Unique
Get-ADGroupMember-Identity$path |
Select @{N='Group';E={$group}},SamAccountName
} | Sort Group,SamAccountName |
Export-Csv .\groupList.csv-NoTypeInformation