The below code will check multiple lines of email address against the Exchange Online as per https://learn.microsoft.com/en-us/powershell/module/exchange/get-exorecipient?view=exchange-ps
Try {
Clear
$EmailAliases = @(
'Myself.actual@domain.com'
'Good.Boss@domain.org'
'fake.person1@domain.org'
'fake.person2@domain.org'
)
$EmailAliases | Get-EXORecipient | Select Name, RecipientType, @{Label = 'Email Address'; Expression = {($_.EmailAddresses | Where-Object {($_ -like 'SMTP*') -and ($_ -notlike '*onmicrosoft.com') } | Sort-Object -CaseSensitive -Descending | ForEach-Object {$_.Split(':')[1]}) -join ', ' }} | Out-GridView
}
Catch {
Write-Warning -Message "The email address $($EmailAliases) cannot be found"
Write-Warning -Message $Error[0].Exception.Message
$out.Details = $_.Exception.Message
Write-Host " ERROR: $($out.Details)" -ForegroundColor Red
}
However, if the email address cannot be found, it is throwing error
Error code:
Get-EXORecipient : Error while querying REST service. HttpStatusCode=404 ErrorMessage={"error":{"code":"NotFound","message":"Error executing request.
","details":[{"code":"Context","target":"","message":"Ex6F9304|Microsoft.Exchange.Configuration.Tasks.ManagementObjectNotFoundException|The operation couldn't be performed because object 'Fake.person1@domain.org' couldn't be found on
'NY2PR01A001DC02.NAMPR01A001.PROD.OUTLOOK.COM'."}],"innererror":{"message":"Error executing request. ","type":"Microsoft.Exchange.Admin.OData.Core.ODataServiceException"}}}}
At line:11 char:18
+ $EmailAliases | Get-EXORecipient | Select Name, RecipientType, @{ ...
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : ProtocolError: (:) [Get-EXORecipient], RestClientException
+ FullyQualifiedErrorId : An error occurred while processing this request.,Microsoft.Exchange.Management.RestApiClient.GetExoRecipient
The Identity
parameter on Get-EXORecipient
takes a single item, not an array of email addresses, so I believe you need a loop do do this.
Also, you do things inside the Try{}
block that should not be there, like defining the array of email addresses to test. That is beter done before entering the loop where the try{}..catch{}
is doing its job.
Try:
Clear-Host
$EmailAliases = 'Myself.actual@domain.com', 'Good.Boss@domain.org',
'fake.person1@domain.org', 'fake.person2@domain.org'
$result = foreach ($eml in $EmailAliases) {
Try {
Get-EXORecipient -Identity $eml -ErrorAction Stop |
Select-Object Name, RecipientType,
@{Name = 'Email Address'; Expression = {
($_.EmailAddresses |
Where-Object { ($_ -like 'SMTP*') -and ($_ -notlike '*onmicrosoft.com') } |
Sort-Object -CaseSensitive -Descending |
ForEach-Object { $_ -replace '^smtp:'}) -join ', ' }
},
@{Name = 'Details'; Expression = { 'OK' }}
}
Catch {
# inside the Catch block, the $_ automatic variable is the exception object
Write-Warning -Message "The email address $eml cannot be found"
$err = $_.Exception.Message
Write-Warning -Message $err
# output a similar object as in the catch block showing the failed email address
"" | Select-Object @{Name = 'Name'; Expression = { 'Not found' }}, RecipientType,
@{Name = 'Email Address'; Expression = { $eml }},
@{Name = 'Details'; Expression = { $err }}
}
}
$result | Out-GridView