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.5k 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

We are using the below snippet to grab the email of a single custom attribute object. Can I get guidance on the best way to modify this to get all the emails of each ... "The user specified in parameter 'MyParameter' has no email address. ", "Information") }

asked 4 days ago by msheppard (660 points)
0 votes
1 answer

I have a number of custom Powershell scripts that add users to Teams, groups, etc. I re-use these scripts dozens of times for different conditions and only change one ... possible to convert this script to a custom command and pass parameters to it instead?

asked Dec 16 by cwyant-hfg (40 points)
0 votes
1 answer

Looking for a script (unless there is a built in way, which I don't think there is) to grant User A full access to User B's OneDrive

asked Dec 16 by msheppard (660 points)
0 votes
1 answer

I am trying to get the script from the link below working, but it's not producing any results. I copied the two column IDs, and I adjusted the param-Date ... the script: https://www.adaxes.com/script-repository/recently-enabled-users-with-initiator-s681.htm

asked Nov 27 by apruitt (120 points)
3,589 questions
3,278 answers
8,303 comments
548,153 users