Get-ADUser Filter Variable Issues
hello,
the following code created import list of email addresses , search domains account, email address. there 5 child domains , accounts can within 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 running issues trying query data within nested loops:
get-aduser : invalid type 'system.management.automation.pscustomobject'.parameter name: mail
@ 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 , ' ' 's little success. assuming fix minor i've gotten far.
thanks!
first thought. importing csv file. csv files have header. best guess, judging error message, header named, mail.
when using compare operator, -like, need include asterisks best results.
modify your foreach loops 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 } }
Windows Server > Windows PowerShell
Comments
Post a Comment