I have a list of users but I don't know each user email domain so I'm just wondering how can I concatenate all the domains from $alldomans at the end of their name one by one and try to find their employeeId.
For example, I'm doing like this right now and this is working but I'll have more than one email domain so I'm just wondering how can I pass the domain from my list one by one and try to find their employee Id?.
$EmployeeId= $policy.name | foreach { (Get-AzureADUser -ObjectId "$($_)@gmail.com").ExtensionProperty.employeeId}
I've tried it like this but still not working for some reason
$allDomains = @(
"gmail.com",
"yahoo.com",
"outlook.com"
"hotmail.com"
)
$EmployeeId= $policy.name | foreach { (Get-AzureADUser -ObjectId "$($_)@$allDomains").ExtensionProperty.employeeId}
CodePudding user response:
You need an inner loop for the Domains:
$allDomains = @(
"gmail.com",
"yahoo.com",
"outlook.com"
"hotmail.com"
)
$EmployeeId = $policy.name | ForEach-Object {
foreach($domain in $allDomains) {
try {
$azUsr = Get-AzureADUser -ObjectId "$_@$domain" -ErrorAction Stop
# using `return` here so that if `$azUsr` could be found,
# we stop this inner loop and go to the next user
return $azUsr.ExtensionProperty.employeeId
}
catch {
Write-Warning $_.Exception.Message
}
}
}