Answered by:
Get-ADUser Filter Variable Issues

Question
-
Hello,
The following code was created to import a list of email addresses and search all domains for the account, by the email address. There are five child domains and accounts can be within any of them.
$path="U:\scripts\powershell\email\test" $filename="email.txt" $data = @() ##container object $objForest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest() $DomainList = @($objForest.Domains | Select-Object Name) $Domains = $DomainList | foreach {$_.Name} $staffemails=Get-ChildItem $path\$filename | ForEach-Object {Import-Csv $_} foreach($staffemail in $staffemails) { foreach($domain in $domains) { $data += get-aduser -server $domain -filter {(EmailAddress -like $staffemail)} -Properties Name | Select Name,EmailAddress } } $data | Export-Csv $path\result.csv -NoTypeInformation
I am running into issues trying to query the data within the nested for loops:
get-aduser : Invalid type 'System.Management.Automation.PSCustomObject'.
Parameter name: mail
At U:\scripts\powershell\email\search.ps1:15 char:15
+ $data += get-aduser -server $domain -filter {(EmailAddress -like $staffemai ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
I have tried multiple configurations of {}'s, ()'s, " " 's and ' ' 's with little success. I am assuming the fix is likely minor as I've gotten this far.
Thanks!
Wednesday, February 24, 2016 11:08 PM
Answers
-
First thought. Your importing a csv file. csv files generally have a header. my best guess, judging by the error message, the header is named, mail.
When using the compare operator, -like, you need to include asterisks for best results.
modify your foreach loops as follows:
foreach($staffemail in $staffemails) { $EM = $staffemail.mail foreach($domain in $domains) { $data += get-aduser -server $domain -filter {(EmailAddress -like "*$EM*")} -Properties Name | Select Name,EmailAddress } }
- Proposed as answer by Elaine Jing Thursday, February 25, 2016 7:27 AM
- Marked as answer by Francesc0 C Thursday, February 25, 2016 7:51 PM
Thursday, February 25, 2016 12:58 AM
All replies
-
Why not use Get-Content instead of Import-Csv if the file is a list of email addresses. Also, the -Like operator generally includes the "*" wildcard character, as in
-filter {EmailAddress -like "*$staffemail"}
I would output $staffemails to make sure you get an array of values.Richard Mueller - MVP Enterprise Mobility (Identity and Access)
- Proposed as answer by Elaine Jing Thursday, February 25, 2016 7:26 AM
Wednesday, February 24, 2016 11:58 PM -
First thought. Your importing a csv file. csv files generally have a header. my best guess, judging by the error message, the header is named, mail.
When using the compare operator, -like, you need to include asterisks for best results.
modify your foreach loops as follows:
foreach($staffemail in $staffemails) { $EM = $staffemail.mail foreach($domain in $domains) { $data += get-aduser -server $domain -filter {(EmailAddress -like "*$EM*")} -Properties Name | Select Name,EmailAddress } }
- Proposed as answer by Elaine Jing Thursday, February 25, 2016 7:27 AM
- Marked as answer by Francesc0 C Thursday, February 25, 2016 7:51 PM
Thursday, February 25, 2016 12:58 AM