Hello,
As we understand, you need all the unwanted characters to just be removed from the generated emails before checking them for uniqueness and setting for a user. To achieve the desired, use the below script. In the script, we added the $excludeCharacters variable that specifies the characters.
$initialEmailTemplate = "%firstname:lower%%lastname:lower%@mydomain.com" # TODO: modify me
$secondaryEmailTemplate = "%firstname:lower%%initials:lower%%lastname:lower%@mydomain.com" # TODO: modify me
$excludeCharacters = @("-", "'") # TODO: modify me
function CheckEmailUniqueness ($mail)
{
# Build search criteria
$criteria = New-AdmCriteria "user" {mail -eq $mail}
# Search for users with the username or email address specified
$searcher = $Context.TargetObject
$searcher.Criteria = $criteria
$searcher.VirtualRoot = $True
$searcher.SizeLimit = 1
try
{
$searchResultIterator = $searcher.ExecuteSearch()
$searchResults = $searchResultIterator.FetchAll()
return $searchResults.Length -eq 0
}
finally
{
# Release resources
$searchResultIterator.Dispose()
}
}
function UpdateUser ($mail)
{
# Update the user
$Context.TargetObject.Put("mail", $mail)
$Context.TargetObject.SetInfo()
}
# Check first template for uniqueness
foreach ($character in $excludeCharacters)
{
$initialEmailTemplate = $initialEmailTemplate.Replace($character, "")
}
if (CheckEmailUniqueness $initialEmailTemplate)
{
UpdateUser $initialEmailTemplate
$Context.LogMessage("$initialEmailTemplate set as user email.", "Information")
return
}
# Check second template for uniqueness
foreach ($character in $excludeCharacters)
{
$secondaryEmailTemplate = $secondaryEmailTemplate.Replace($character, "")
}
if (CheckEmailUniqueness $secondaryEmailTemplate)
{
UpdateUser $secondaryEmailTemplate
$Context.LogMessage("$secondaryEmailTemplate set as user email.", "Information")
return
}
$Context.LogMessage("Could not generate a unique email address for user %fullname%.", "Error")