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.