This looks good on paper but outputs incorrectly

I was trying to query for a few attributes of a User that exists in multiple domains and knew something was up when the same values cam back for i.e. UPN (which should be different)

What am I missing?

$Properties = @('Name','SamAccountName','UserPrincipalName','extensionAttribute10','extensionAttribute11')
$domains = (Get-ADForest).domains
$dcs = ForEach ($domain in $domains) {Get-ADDomainController -DomainName $domain -Discover -Service PrimaryDC | Select -ExpandProperty hostname}

ForEach ($dc in $dcs) {Get-ADUser MyUser -Properties $Properties | select $Properties }| FL

My variables are correctly enumerating.


Not clear on the issue you are facing, are you getting any error ?
You are just iterating for each $dcs but not telling Get-ADUser about the DC info using -Server parameter.

Apologies for that. I get output but for each domain (count = 4) I get the same exact data from just 1. I want to query for the one User in each of the returned Domains (from each of the respective Domains’ DCs).

You just need to add the -Server parameter to Get-AdUser within your scriptblock on line 5. That Foreach construct runs the same command over and over with out the variable $dc in the scriptblock.


Thank guys, working now…(great to see your newest avatar, kvprasoon)

@jeff-taylor :slight_smile:

Hope your issue got addressed.