Hello Mike,
You could try with the below sample:
connect-azuread
$app= get-azureadserviceprincipal -all $true | where {$_.tags -eq "<Your app tag>"}
foreach($entry in $app){
write-host "Application Name: $($entry.displayname)"
$assignments=get-azureadserviceapproleassignment -objectid $entry.objectid
if(!$assignments){
write-host "No app assignments found"
}
else{
foreach($assignment in $assignments){
$user= get-azureaduser -filter "Displayname eq '$($assignment.principaldisplayname)'" | select userprincipalname,mail
if($user.mail -notlike "<Your domain name>"){
write-host "user $($user.mail) has app role assigned in app $($assignment.resourcedisplayname)"}
}
}
}
The result I got was:
I have 3 app role assigned for msalapp1 in total and with the filter only one showed in the result. This should be your expectation.
If you need to exclude 2 domains, a nest if justification will be required since "or" condition can be passed in this case. Also, if you want an output, you can add the export function after the "if" justification for user mail.
Best Regards,
Ian Xue
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.