Here's an example of some code to verify that all exclusions have the same format. After determining exclusions consistency, it creates constructs a string from the users UPN that matches the examples in the $Exclusions variable.
$user = [PSCustomObject]@{UserPrincipalName='******@A-DOMAIN.org.uk'}
# setermine the pattern used in the exclusions list
# all exclusions must be of the same type
$MatchDomain = 0 # 0=no determination, 1=domain, 2=@domain, 3=*@domain, 4=full UPN
#$Exceptions = @("@A-DOMAIN.org.uk")
$Exceptions = @("A-DOMAIN.org.uk","*@domain.com")
ForEach ($Exception in $Exceptions){
$pieces = $Exception -split "@"
if ($pieces.count -eq 1){
if($MatchDomain -eq 0 -OR $MatchDomain -eq 1){
$MatchDomain = 1
}
else{
Throw "Exceptions list is not consistent"
}
}
elseif ($pieces.count -eq 2){
if ($pieces[0] -eq '*'){
if ($MatchDomain -eq 0 -OR $MatchDomain -eq 3){
$MatchDomain = 3
}
else{
Throw "Exceptions list is not consistent"
}
}
elseif($pieces[0].length -eq 0){
if ($MatchDomain -eq 0 -OR $MatchDomain -eq 2){
$MatchDomain = 2
}
else{
Throw "Exceptions list is not consistent"
}
}
else{
if ($MatchDomain -eq 0 -OR $MatchDomain -eq 4){
$MatchDomain = 4
}
else{
Throw "Exceptions list is not consistent"
}
}
}
else{
Throw "Exceptions list is not consistent"
}
}
# build a string based on the exclusion pattern
$x = ""
Switch ($MatchDomain){
1 {$x = ($user.UserPrincipalName -split "@")[1]; break} # domain
2 {$x = '@' + ($user.UserPrincipalName -split "@")[1]; break} # @domain
3 {$x = '*@' + ($user.UserPrincipalName -split "@")[1]; break} # *@domain
4 {$x = $user.UserPrincipalName; break} # user@domain
Default {$x = $user.UserPrincipalName; break} # in case of empty $Exclusions
}
if ($Exceptions -Contains $x) {
Write-Host "$($user.UserPrincipalName) is an exception, don't touch permissions" -ForegroundColor Red
}
EDIT: Needed a "Default" condition for the Switch to deal with an empty $Exceptions list.