Hello,
Thank you for specifying. You can use the below script:
$initialEmailTemplate = "%firstname:lower%%lastname:lower%@mydomain.com" # TODO: modify me
$secondaryEmailTemplate = "%firstname:lower%%initials:lower%%lastname:lower%@mydomain.com" # 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 temaplte for uniqueness
if (CheckEmailUniqueness $initialEmailTemplate)
{
UpdateUser $initialEmailTemplate
$Context.LogMessage("$initialEmailTemplate set as user email.", "Information")
return
}
# Check second temaplte for uniqueness
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")