0 votes

Need to exclude spaces, dashes and appostrophes.

related to an answer for: Need a script to check email uniqueness?
by (1.0k points)

1 Answer

+1 vote
by (279k points)

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")
0

Works great thanks!

Related questions

0 votes
1 answer

Example: If a user has a ' in theirname: Fred J O'neal. Normally the username is set as %lastname:lower,4%%firstname:lower,3%%initials:lower% Problem is o'nefrej would be the result. ... name", "Information") $username = #this is what I'm not sure how to do?

asked Dec 6, 2022 by mightycabal (1.0k points)
0 votes
1 answer

Then I could put the approval on the custom command.

asked Dec 4, 2023 by mightycabal (1.0k points)
0 votes
1 answer

We are developing a process to mange mailboxes for terminated users. At the time of termination we would like to: convert the mailbox to a shared mailbox. Send an approval ... would run script to grant the manger access to the mailbox. Can this be done?

asked Oct 27, 2023 by mightycabal (1.0k points)
0 votes
1 answer

For instance to execute a powershell script that enable MFA for all member in that group?

asked Jan 27, 2023 by samuel.anim-addo (20 points)
0 votes
0 answers

Is there a way to exclude characters in the random password generator? The number 1, the lowercase letter l (lunch), and the uppercase letter I (eye)all look the same.

asked Mar 1, 2021 by Derek.Axe (480 points)
3,434 questions
3,130 answers
7,984 comments
546,352 users