0 votes

I made a script that changes the username if such a name is already taken, set up a business rule before user create as written in the instructions http://www.adaxes.com/help/ValidateModifyUserInputWithScript/, but when you try to create an account, an error appears that such an object already exists, while before that the rule for changing the name worked Error: Business Rules: 1 rule encountered while processing your request 'Check for unique': Run PowerShell script 'Check for unique' for the user The username has been changed to teadaxes@domain.com. The object already exists on 'domain.com:636'.

by (80 points)

1 Answer

0 votes
by (232k points)

Hello,

The thing is that Active Directory does not allow creating users with the same Full Name in the same OU. This is something that can be adjusted in a script same like the username. For an example, have a look at Example 3 – Rename the user if the Full Name is not unique within the OU in the following tutorial: https://www.adaxes.com/help/ValidateModifyUserInputWithScript.

0

I create an account in another ou, where there are no duplicates, but still an error. I tried to check for duplicates in one OU, also an error! Screenshot 2022-11-21 112625.png Script:

Import-Module Adaxes

function IsUserNameUnique($username)
{
    $domain = $Context.GetObjectDomain("%distinguishedName%")
    $user = Get-AdmUser -Filter {userPrincipalName -eq $username}  -Server $domain -AdaxesService localhost
    return $null -eq $user
}

$username = $Context.GetModifiedPropertyValue("userPrincipalName")
$firstname = $Context.GetModifiedPropertyValue("givenName")
$lastname = $Context.GetModifiedPropertyValue("sn")

if (IsUserNameUnique($username))
{
    return
}

$localPart, $domainPart = $username.Split("@")
$uniqueUsername = $Null
for ($i = 1;; $i++)
{
    $uniqueUsername = $firstname.Substring(0, $i) + $lastname + "@" + $domainPart
    if (IsUserNameUnique($uniqueUsername))
    {
        $samAccountName = $firstname.Substring(0, $i) + $lastname
        break
    }
}

$Context.SetModifiedPropertyValue("userPrincipalName", $uniqueUsername)
if ($Context.IsPropertyModified("sAMAccountName"))
{
    $Context.SetModifiedPropertyValue("sAMAccountName", $samAccountName)
}

$Context.LogMessage("The username has been changed to " + $uniqueUsername + ".", "Information")
0

Hello,

The error about existing object is returned by Active Directory and is not related to Adaxes. Are you sure that there are no accounts in the OU with the same Name/Full Name? You r script does not check that. If you are checking in Adaxes, the existing duplicate can be in unmanaged list and not displayed when browsing AD. Please, try checking in AD itself (e.g. using Active Directory Users and Computers).

Related questions

0 votes
1 answer

We want to check, if the number of a new team (group) is unique. The number is stored in the attribute "gidNumber". I have a business rule executing before creating ... $NULL) { $Context.Cancel("Ein Team mit dieser Team-Nummer existiert bereits!") return } }

asked Oct 13, 2020 by lohnag (120 points)
0 votes
1 answer

Hi, I'd like to implement a rule to ensure that telephoneNumber value is unique accross the domain for each user, either after creation or updates. I tried to implement a ... one (before change). How to get the new value in the script ? Thanks Stephen

asked May 31, 2011 by sroux (800 points)
0 votes
0 answers

Say you have Manager A that has 30 users under them. Manager A leaves and Manager B takes the position. What is the best way to update all 30 users so their new manager is Manager B.

asked Jun 7, 2021 by Jmbrown04 (60 points)
0 votes
1 answer

We have users with a value of a space for their mobile number and telephone number. I would like to figure out who these users are and null the value. Or if the value contains a number leave it alone. if it does not have a number then null the value.

asked Nov 15 by B_Witmer (40 points)
0 votes
1 answer

Hi, I need to run a schedule task only if a customattribute is not empty. Can I use ConditionIsMet?

asked Oct 7, 2021 by Simone.Vailati (360 points)
2,870 questions
2,591 answers
6,730 comments
111,676 users