Reputation: 139
I want to create a condition that validates 3 things in powershell.
I'm tryign the following: lets say I have a bunch of AD users with a mismatch on userPrincipalName and Mail fields, and some dont have the mail filed populated. i'm trying to build a condition that checks 1 . mail and upn domains to match and 2 to check if its the correct domain.
I created 2 simple functions i to split the either UPN or Mail field by "@domain.com" and im trying to build a second function ta checks multiple conditions and return a case
Is this a valid condition? if($upn -and $mail -eq $cdomain){ #<<<<-- ---is this a valid condition?
lets say i got 3 users Objects in AD with the th the following info:
victor reyes
UPN = [email protected]
mail = [email protected]
pablo reyes
UPN = [email protected]
mail = [email protected]
robert reyes
UPN = [email protected]
mail = ""
in the case of victor reyes both domains are the same but are incorrect (supposed to be correctdomain.com)
pablo reyes has a mixup of domains and need to check what the corec one is
and robert reyes does not have a mail but dos have a correct upn how could i go about creating a condition that could generate a case per everyone of these conditions ?
Sorry i cant seem to find the correct words for what i want to do. Please see example bellow
Example:
function GetDomain{
param (
$Domain
)
$output = $domain.Split("@")
return $output[1]
}
function GetCase {
param (
$UPNDomain,
$MailDomain
)
$cdomain = "@correctdomain.com"
$upn = GetDomain $UPNDomain
$mail = GetDomain $MailDomain
# if $upn and $mail are equals and are equals to $cdomain return Case 1
if($upn -and $mail -eq $cdomain){ #<<<<-- ---is this a valid condition?
return Case1
}
#if $upn and $mail are not equals, and $upn is equal to $cdomain return Case2
elseif(){return Case2}
#if $upn and $mail are not equals, and $mail is equal to $cdomain return Case3
elseif(){return Case3}
etc
}
Upvotes: 0
Views: 162
Reputation: 60045
Here is how I would personally approach it, definitely would use the MailAddress
class to check if they're valid email addresses, and by doing so, there would be no need for the GetDomain
function.
Hopefully the inline comments helps you understand the logic.
function Get-Case {
[CmdletBinding()]
param(
[parameter()]
[string] $UPNDomain,
[Parameter()]
[string] $MailDomain,
[parameter(Mandatory)]
[string] $Domain
)
end {
$upn = $UPNDomain -as [mailaddress]
$mail = $MailDomain -as [mailaddress]
# check if both parameters are valid email addresses
if(-not $upn -or -not $mail) {
# one of them or both are not valid
return 'case0'
}
# check if the upn and mail Domain match,
if($upn.Host -ne $mail.Host) {
# if they don't, no need to keep checking
return 'case1'
}
# here we assume they're the same,
# check if their Domain is the same as `$Domain`
if($upn.Host -eq $Domain) {
# mail and upn Domain are the same and Domain matches both
return 'case2'
}
# if above was not true, then their Domain is incorrect
return 'case3'
}
}
# case2: both mails Domain match and they also match with expected Domain
Get-Case [email protected] [email protected] -Domain gmail.com
# case1: mails Domain don't match
Get-Case [email protected] [email protected] -Domain correctdomain.com
# case0: one of the arguments is not a valid mail
Get-Case [email protected] '' -Domain correctdomain.com
Upvotes: 1