Hello,
I am looking for assistance in modifying our user creation script. When users with multiple names are being created for example, Jesus Geraldo Lopez, the user is being created as either L@123.com or jgeraldo l@123.com. Is there a way to modify our script to check for spaces in the first and last name field to and stop the user from being created? Also, would it be possible to modify the script to also check for special characters and replace them?
Here is our current user creation script. I appreciate any assitance. Thanks.
Import-Module Adaxes
function ReplaceCharacters($value)
{
$map =@{ "å"="a"; "ö"="o"; "ä"="a";"ü"="u"; "ñ"="n"; "é"="e"; " "=""; "'"="" } # TODO: modify me
foreach ($key in $map.Keys)
{
$value = $value.Replace($key, $map[$key])
}
return $value
}
function IsUserNameUnique($username)
{
$user = Get-AdmUser $username -erroraction silentlycontinue
return $user -eq $Null
}
# Get the username
$username = $Context.GetModifiedPropertyValue("samAccountName")
# Replace special characters in username
$username = ReplaceCharacters $username
# Check if the username is unique
$uniqueUsername = $Null
if (IsUserNameUnique($username))
{
$uniqueUsername = $username
}
# If the username is not unique, generate a unique one
if ($uniqueUsername -eq $NULL)
{
$firstName = $Context.GetModifiedPropertyValue("givenName")
if ($firstName -ne $NULL)
{
# Add characters from first name, one by one
$firstName = ReplaceCharacters $firstName
for ($i = 0; $i -le $firstName.length; $i++)
{
$initial = $initial + $firstName[$i]
$uniqueUsername = $username + $initial
if (IsUserNameUnique($uniqueUsername))
{
break
}
$uniqueUsername = $Null
}
}
}
if ($uniqueUsername -eq $NULL)
{
for ($i = 1; $True; $i++)
{
$uniqueUsername = $firstName + $username + $i
if (IsUserNameUnique($uniqueUsername))
{
break
}
}
}
# Update User Logon Name (pre-Windows 2000)
$Context.SetModifiedPropertyValue("samAccountName", $uniqueUsername)
$Context.LogMessage("User Logon Name (pre-Windows 2000): $uniqueUsername", "Information")
# Get domain name
$domaiName = $Context.GetObjectDomain("%distinguishedName%")
# Update User Logon Name
$Context.SetModifiedPropertyValue("userPrincipalName", "$uniqueUsername@$domaiName")
$Context.LogMessage("User Logon Name: $uniqueUsername@$domaiName", "Information")