Hello,
Was wondering if there is a script for a business rule or a property pattern that can be used for this request. I am wondering how can I do a check anytime a user is modified that checks if the email address (Proxy Address and Mail) values are not already present in the AD. If they are then id like it to error and let the person know that the SMTP address is already taken.
Essentially when editing a user account and assigning it a new email address id like a check to be done to confirm uniqueness and then allow to proceed if its unique. I have something like this but doesnt work and errors no matter what.
Import-Module Adaxes
function IsMailUnique($isOnPremiseObject, $emailAddress)
{
# Filter to check if the mail address or proxyAddresses already exist
if ($isOnPremiseObject)
{
$filter = {mail -eq $emailAddress -or proxyAddresses -contains "SMTP:$emailAddress"}
}
else
{
$filter = {mail -eq $emailAddress -or proxyAddresses -contains "SMTP:$emailAddress"}
}
$domain = $Context.GetObjectDomain("%distinguishedName%")
$user = Get-AdmUser -Filter $filter -Server $domain -AdaxesService localhost
# Return false if no user is found, meaning email is unique
return $null -eq $user
}
# Check directory type.
$isOnPremiseObject = $Context.TargetObject.DirectoryType -eq 1
# Get the email address and proxy addresses.
$emailAddress = $Context.GetModifiedPropertyValue("mail")
$proxyAddresses = $Context.GetModifiedPropertyValue("proxyAddresses")
# Check if the email address is unique.
if (-not (IsMailUnique $isOnPremiseObject $emailAddress))
{
# If email is already taken, return True
$Context.LogMessage("The email address '$emailAddress' is already in use.", "Information")
return $true
}
else
{
# If email is unique (not taken), return False
$Context.LogMessage("The email address '$emailAddress' is available.", "Information")
return $false
}