Hi Support,
We are looking to add a few things to one of the username creation scripts
If the upn/username is not unique, add a character of the first name to the last name until there are no more charters to add.
John Doe (Jdoe , Jodoe, JohDoe, JohnDoe)
If the upn/username is still not unique add a digit.
JohnDoe1
If someone with the same name already exists add the department field (IT) to the display name
Doe, John (IT)
If the Employee type has a value of c than the display name should be
Doe, John (Consultant)
If they have the employee type of i
Doe, John (Intern)
If they have an employee type of C or i and name already exists add the department (IT)
Doe, John (IT Intern)
Import-Module Adaxes
$upnSuffix = "xxxxx" # TODO: modify me
#Check if UPN exists
function IsUPNUnique($userLogonName)
{
if ($userLogonName -eq $NULL)
{
return $False
}
# Search users in all managed domain with specific UPN
$searcher = New-Object "Softerra.Adaxes.Adsi.Search.DirectorySearcher" $NULL, $False
$searcher.SearchParameters.PageSize = 500
$searcher.SearchParameters.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SearchParameters.Filter = "(&(objectCategory=user)(userPrincipalName=$userLogonName))"
$searcher.VirtualRoot = $True
$result = $searcher.ExecuteSearch()
$users = $result.FetchAll()
$result.Dispose()
if ($users.Count -eq 0)
{
return $True
}
return $False
}
function IsUPNUnique2($userLogonName)
{
if ($userLogonName -eq $NULL)
{
return $False
}
# Search users in all managed domain with specific UPN
$searcher = New-Object "Softerra.Adaxes.Adsi.Search.DirectorySearcher" $NULL, $False
$searcher.SearchParameters.PageSize = 500
$searcher.SearchParameters.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SearchParameters.Filter = "(&(objectCategory=user)(userPrincipalName=$userLogonName))"
$searcher.VirtualRoot = $True
$result = $searcher.ExecuteSearch()
$users = $result.FetchAll()
$result.Dispose()
if ($users.Count -eq 0)
{
return $false
}
return $true
}
function IsPropertyNameUnique($objectName, $domainName)
{
$user = Get-AdmUser -Filter {name -eq $objectName} -erroraction silentlycontinue -AdaxesService "localhost" -Server $domainName
return $user -eq $Null
}
function IsUserNameUnique($username, $domainName)
{
$user = Get-AdmUser $username -erroraction silentlycontinue -AdaxesService localhost -Server $domainName
return $user -eq $Null
}
# Get the user name info
$username = $Context.GetModifiedPropertyValue("samAccountName")
$userLogonName = $Context.GetModifiedPropertyValue("userPrincipalName")
$sn = $Context.GetModifiedPropertyValue("sn")
$givenName = $Context.GetModifiedPropertyValue("givenName")
$domainName = $Context.GetObjectDomain("%distinguishedName%")
# Check if the username is unique
if (!(IsUPNUnique $userLogonName))
{
# Add Initials and check for uniqueness
$initals = $Context.GetModifiedPropertyValue("initials")
if ($initals -ne $NULL)
{
$uniqueUserLogonName = "%firstname:lower,1%%lastname:lower%@$upnSuffix"
$uniqueUsername = "%firstname:lower,1%%lastname:lower%"
}
# If the username is not unique, generate a unique one
for ($i = 2; $True; $i++)
{
if (IsUPNUnique $uniqueUserLogonName)
{
break
}
$uniqueUsername = $username + $i
# Build new UPN
$uniqueUserLogonName = "%firstname:lower,1%%lastname:lower%$i@$upnSuffix"
}
# Check sAMAccountName
if (!(IsUserNameUnique $uniqueUsername $domainName))
{
$Context.Cancel("The username (SAMAccountName property) is not unique.")
return
}
# Update User Logon Name (pre-Windows 2000)
$Context.SetModifiedPropertyValue("samAccountName", $uniqueUsername)
$Context.LogMessage("The username has been changed to " + $uniqueUsername `
+ ".", "Information")
# Update User Logon Name
$Context.SetModifiedPropertyValue("userPrincipalName", $uniqueUserLogonName)
$Context.LogMessage("The UPN has been changed to " + $uniqueUserLogonName `
+ ".", "Information")
}
# Get the object DN
$objectDN = $Context.TargetObject.ObjectInfo.DN;
$objectLeaf = $objectDN.Leaf
if (!(IsPropertyNameUnique $objectLeaf.Value $domainName))
{
for ($i = 1; $True; $i++)
{
$objectName = $objectLeaf.Value + " " + "(" + "%department%" + ")"
if (IsPropertyNameUnique $objectName $domainName)
{
break
}
}
# Rename the object
$Context.SetModifiedPropertyValue("name", $objectName)
$Context.LogMessage("The name has been changed to " + $objectName `
+ ".", "Information")
}