0 votes

First off I'm trying to Remove spaces from a user name "before creating the user".

I used this script: http://www.adaxes.com/tutorials_Simplif ... Script.htm

$spacesRemoved = $False;
# User Logon Name (pre-Windows 2000)
if ($Context.IsPropertyModified("samAccountName"))
{
    $samAccountName = $Context.GetModifiedPropertyValue("samAccountName");
    if ($samAccountName.Contains(" "))
    {
        # Remove spaces
        $samAccountName = $samAccountName.Replace(" ", "");
        # Update sAMAccountName
        $Context.SetModifiedPropertyValue("samAccountName", $samAccountName);
        $spacesRemoved = $True;
    }
}
# User Logon Name
if ($Context.IsPropertyModified("userPrincipalName"))
{
    $userPrincipalName = $Context.GetModifiedPropertyValue("userPrincipalName");
    if ($userPrincipalName.Contains(" "))
    {
        # Remove spaces
        $userPrincipalName = $userPrincipalName.Replace(" ", "");
        # Update the username
        $Context.SetModifiedPropertyValue("userPrincipalName", $userPrincipalName);
        $spacesRemoved = $True;
    }
}
# Log a message
if ($spacesRemoved)
{
    $Context.LogMessage("Spaces have been removed from the username.", "Information");
}

It doesn't error in the PS editor, and doesn't remove spaces.

Second one is checking the User Length and Uniqueness for a user. This Uniqueness portion of the script works and adds a number as designed, but it's allowing for usernames over 8 as specified.

This script does throw an error, of "Cannot validate argument on parameter 'Identity'. The argument is null. Supply a non-null argument and try the command again."

Import-Module Adaxes
$maximumLength = 8 # TODO: modify me

function IsUserNameUnique($username)
{
   $user = Get-AdmUser $username -erroraction silentlycontinue
   return $user -eq $Null
}

# Get the username
$username = $Context.GetModifiedPropertyValue("samAccountName")

# Check whether the username is unique
if (IsUserNameUnique($username))
{
    return
}

# If the username is not unique, generate a unique one
$uniqueUsername = $Null
for ($i = 1; $True; $i++)
{
    $difference = $maximumLength - $username.Length - $i.ToString().Length
    if ($difference -lt 0)
    {
        $username = $username.Substring(0, $username.Length + $difference)
    }

    if ([System.String]::IsNullOrEmpty($username))
    {
        $Context.Cancel("Unable to generate a unique username, because the number length exceeds the maximum length of the username")
        return
    }

    $uniqueUsername = $username + $i;
    if (IsUserNameUnique($uniqueUsername))
    {
        break
    }
}

# Update User Logon Name (pre-Windows 2000)
$Context.SetModifiedPropertyValue("samAccountName", $uniqueUsername)

# Update User Logon Name
$upnSuffix = $Context.GetObjectDomain("%distinguishedName%")
$userLogonName = $uniqueUsername + "@" + $upnSuffix
$Context.SetModifiedPropertyValue("userPrincipalName", $userLogonName)
$Context.LogMessage("The username has been changed to " + $userLogonName `
  + ".", "Information")

Help is appreciated, thanks.

by (1.4k points)
0

Hello,

RE: Script for removing spaces from username

t doesn't error in the PS editor, and doesn't remove spaces.

The script can't be tested in the PS editor. Try creating a Business Rule triggered before creating a user based on instructions in the tutorial, and then try creating a user with spaces in the username.

RE: Script for generating a unique username
Hmm ... how do you test the script? Also, can you show a screenshot of your Business Rule created on the basis of the script?

0

Ok, yes I did create a Business Rule to run BEFORE user creation, here it is.

http://imgur.com/biQyHwr

I tested it by creating a user in the web interface and trying all the different ways needed, spaces in the name, too long of a name, name not unique, etc.

Here's one where I create the user, and after the first name, I put a space. It doesn't detect the space in this scenario.

http://imgur.com/yigtDVJ

In this case, I throw a space in the middle of the name, and it detects it, but it has an issue with the Email:

http://imgur.com/hwmhpOw

And this one is the username is way too long, but it doesn't correct it:

http://imgur.com/IbQwumc

0

Ok, thinking on this and looking at the code, I understand why the spaces script doesn't work correctly for me. It's only fixing the username. I construct my email address by doing firstname.lastname@domain.com, so what can I do to pull spaces out of the Firstname and Lastname to construct a proper email address?

0

Hello,

For the script that removes spaces in the username, use the following script instead of the script that you currently have:

$properties = @("givenName", "sn", "samAccountName", "userPrincipalName", "mail") # TODO: modify me

foreach ($property in $properties)
{
    if ($Context.IsPropertyModified($property))
    {
        $value = $Context.GetModifiedPropertyValue($property)
        if ($value.Contains(" "))
        {
            # Remove spaces
            $value = $value.Replace(" ", "")

            # Update property
            $Context.SetModifiedPropertyValue($property, $value)

            # Log message
            $Context.LogMessage("Spaces have been removed from $property", "Information")
        }
    }
}

It will remove spaces from all properties whose names are specified in the $properties variable.

As for the script that generates a unique username, there was a small error in the script. it didn't check whether the username fits within the length limit if the username specified on the Create User form was already unique. We've updated it in the Script Repository. Copy it from here: http://www.adaxes.com/script-repository ... e-s298.htm.

0

I am using property patterns with regex. Then restricting the count too.

^[a-zA-Z0-9àâäôéèëêïîçùûüÿæœÀÂÄÔÉÈËÊÏΟÇÙÛÜÆŒáéíñóúüÁÉÍÑÓÚÜ-]*$

Please log in or register to answer this question.

Related questions

0 votes
1 answer

Is it possible using PowerShell to copy group memberships from an already existing user without copying 2 specific groups named for example test and test 1 ? We are currently ... groups are not included. I can share the PowerShell script if needed. KR, Cas

asked Oct 30, 2023 by Cas (200 points)
0 votes
1 answer

Hi, would it be possible to script a workstation in AD and also directly from our local SCCM environment ?

asked Oct 28 by ddesmedt (40 points)
0 votes
1 answer

The rule runs but since the first name and last name are passed as parameters, I only get the sequential # as a userID without the initials.

asked Oct 24 by curtisa (290 points)
0 votes
1 answer

We try to use ADSI scripting to automate some tasks using Adaxes 2023. One such task is to try to check whether an answer provided by a user to his question is correct or not. ... . But I do not see an easy way to do this using ADSI script and/or interfaces.

asked Oct 22 by gfang (20 points)
0 votes
1 answer

During our offboarding of a user we need to connect to SharePoint Online to grant the users line manager permission to their OneDrive. I'm not finding a way to do this ... set / condition. Need help with script that allows us to connect to SharePoint Online.

asked Oct 1 by Peter.S (20 points)
3,551 questions
3,242 answers
8,243 comments
547,828 users