0 votes

This will run in a business rule "after creating a user" The email should be set to %firstname:lower%%lastname:lower%@mydomain. If the email isn't unique then it shoud be set to %firstname:lower%%initials:lower%%lastname:lower%@mydomain and if that isn't unique fail and log the failure. This should update the emailaddress attribute in AD(not updating anything in Azure).

by (1.0k points)
0

Hello,

What exactly do you mean by if that isn't unique fail? Should user creation be cancelled in this case?

0

Since it the business rule would run after the user is created the user creation wouldn't fail just the script would log that it couldn't create a unique email account.

0

Hello,

Thank you for clarifying. For us to help you with the script, please, specify the version of Adaxes you are currently using. For information on how to check it, have a look at the following help article: https://www.adaxes.com/help/CheckServiceVersion.

0

image.png

1 Answer

+1 vote
by (292k points)

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

Thank you works perfectly.

Related questions

0 votes
1 answer

I'm trying to automate adding users who are enrolled in MFA to an AD group. The scripts I found elsewhere here that do not work so I believe they may have been written against a prior Adaxes version or referencing a report that does not meet our needs.

asked May 31 by neal (50 points)
0 votes
1 answer

We're looking at adding a powershell script to check for duplicate Exchange Aliases within our exchange organization prior to a user modification. We've noticed that ... .LogMessage($uniqueUsername + "@domain.com", "Error"); Thanks for your assistance!

asked Aug 14, 2014 by VTPatsFan (610 points)
0 votes
1 answer

Greetings. When I create the parameters to make a business rule that looks for users whose Email Proxy Adresses does not contain 'SMTP:%userPrincipalName%', it still generates profiles ... and primary SMTP address don't match. Version is 2023 How rule is set

asked Dec 19, 2022 by MShep (80 points)
0 votes
1 answer

So this works for us however we would like to add to check if the last group is at 3 users we would like to send a seperate email but would still like all the above to continue to happen the way it is.

asked Mar 2, 2022 by Keonip (180 points)
0 votes
1 answer

For creating a computer object, we want to check if the entered CN is already used in our AD. And for that we want to use a powershell script. An other dot ... powershell script should be start before creating the computer object, right? Thanks for your help.

asked Jun 4 by KEME (80 points)
3,569 questions
3,259 answers
8,275 comments
547,985 users