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 (150 points)
0 votes
1 answer

I have made a deprovision custom command. I cannot change the attribute directReports, so was thinking - i could take the people in the directReports field of the manager ... (and its subordinates) that im running the deprovision custom command from. Any tips?

asked Mar 21 by EdgarsABG (50 points)
0 votes
1 answer

I have a fairly simple function that I want to convert to a report in Adaxes that others can use The PowerShell function as it currently exists function Get-Groups { [CmdletBinding( ... with errors or a blank report. So how can I just add values to the report?

asked Jan 10 by jcrook (100 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)
3,348 questions
3,049 answers
7,788 comments
545,045 users