Querying multiple AD domains to find a list of users using filter -eq

 

Hi All,

I am currently using this script to export a list of AD users from a CSV file input. The process is running correctly, but it generates several errors. This is because I believe it queries all the domains returning an error when it doesn’t find the object in the domain.

$csvdata = Import-Csv -Path $csvfile

$result = foreach ($domain in $domains)

{

$csvdata | ForEach-Object { Get-ADUser -Server $domain -Identity $_.muid } | select-object -property DistinguishedName

}

$result | Out-file -Append -FilePath $txtfile1

I get returned the following error even though it then finds the user
Get-ADUser : Cannot find an object with identity: 'XXXXXXXX' under: 'DC=corp,DC=domain,DC=com'.
At line:3 char:33
+ ... ForEach-Object { Get-ADUser -Server $domain -Identity $_.muid } | sel ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (XXXXXXXX:ADUser) [Get-ADUser], ADIdentityNotFoundException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
I understand if I use the cmdlet filter something like -eq it would then return no errors, but I am unable to make it work so far.

Is there any way you can help me with the script above to filter only the users found and return no errors?

 

 

Thanks,

PowerDude

 

 

 

$csvdata = Import-Csv -Path $csvfile

$result = foreach ($domain in $domains)

{

    $csvdata | ForEach-Object { try { Get-ADUser -Server $domain -Identity $_.muid -EA 1 } catch { } } | select-object -property DistinguishedName

}

$result | Out-file -Append -FilePath $txtfile1

Hi Sam,

 

Many thanks, that worked like a charm :smiley: