Here you go :D (Sanitised a little)
The other script to reset works fine and would set the number to a different number that 888000
$usernameFormat = "A{0:000000}" # TODO: modify me
$initialNumber = 888000 # TODO: modify me
$maxNumber = 900000 # TODO: modify me
$settingsPath = $Context.GetWellKnownContainerPath("ConfigurationSetSettings")
$settings = $Context.BindToObject($settingsPath)
# Get the next contractor number from global configuration
try
{
$number = [int]($settings.Get("adm-CustomAttributeInt1"))
$number++
}
catch
{
# If no number is set in the global configuration, use the initial number
$number = $initialNumber
}
$uniqueUsername = [System.String]::Format($usernameFormat, $number)
do
{
if ($number -gt [int]$maxNumber)
{
$Context.Cancel("Cannot generate a username for the contractor because the maximum allowed contractor number has been reached. Please contact your system administrator.")
return
}
# Check whether the username is unique
$searcher = $Context.BindToObject("Adaxes://rootDse")
$searcher.PageSize = 500
$searcher.SearchScope = "ADS_SCOPE_SUBTREE"
$searcher.SearchFilter = "(&(sAMAccountType=805306368)(sAMAccountName=$uniqueUsername))"
$searcher.ReferralChasing = "ADS_CHASE_REFERRALS_NEVER"
$searcher.VirtualRoot = $True
try
{
$searchResult = $searcher.ExecuteSearch()
$result = $searchResult.FetchAll()
# If the username is not unique, find a unique one
if ($result.Count -ne 0)
{
$number++
$uniqueUsername = [System.String]::Format($usernameFormat, $number)
}
}
finally
{
$searchResult.Dispose()
}
}
while ($result.Count -ne 0)
# Save the new number in the global settings
if ($number -lt [int]$initialNumber)
{
$settings.Put("adm-CustomAttributeInt1", $number)
$settings.SetInfo()
}
#Gather employee number, upper and lower case versions and set password
$cemployeeValue = $uniqueUsername
$cemployeeValueLow = $uniqueUsername.ToLower()
$defPassword = "<PASSWORD>"
#Gather user's names and initials
$clastNameValue = ($Context.GetModifiedPropertyValue("sn")).Trim()
$cfirstNameValue = ($Context.GetModifiedPropertyValue("givenName")).Trim()
$cinitialsValue = ($Context.GetModifiedPropertyValue("initials")).Trim()
#Calculate user's display name, email, homedir, logonname, pobox
$upnSuffix = $Context.GetObjectDomain("%distinguishedName%")
$cdisplayNameValue = "$($clastNameValue) $($cfirstNameValue) $($cinitialsValue)"
$cemailValue = "$($cfirstNameValue.ToLower()).$($clastNameValue.ToLower()).<SUFFIX>"
$chomeDirValue = "\<SERVERNAME>\$cemployeeValue$"
$clogonNameValue = $($cemployeeValue)
$clogonNameValueSuffix = $($cemployeeValue) + "@" + $upnSuffix
$cprofilePathValue = "\<SERVERNAME>\$cemployeeValue$\Settings"
$cpoBoxValue = "$($cfirstNameValue.ToLower()).$($clastNameValue.ToLower()).<SUFFIX>"
#Set some static user properties
$ccityValue = "Chat"
$ccountryValue = "GB"
$chomeDriveValue = "O:"
$cofficeValue = "<OFFICE>"
$cpostCodeValue = "<POSTCODE>"
$expiryValue = Get-Date
$expiryValue = $expiryValue.AddDays(365)
#Comit changes to the user record
$Context.SetModifiedPropertyValue("displayName", ($cdisplayNameValue)) #Set "displayname"
$Context.SetModifiedPropertyValue("mail", ($cemailValue)) #Set "mail"
$Context.SetModifiedPropertyValue("homeDirectory", ($chomeDirValue)) #Set "home directory"
$Context.SetModifiedPropertyValue("homeDrive", ($chomeDriveValue)) #Set "home drive" to O:
$context.SetModifiedPropertyValue("profilePath", ($cprofilePathValue)) #Set "Profile path"
$Context.SetModifiedPropertyValue("userPrincipalName", ($clogonNameValueSuffix)) #Set "logon name" (Mnumber)
$Context.SetModifiedPropertyValue("sAMAccountName", ($clogonNameValue)) #Set "pre 2000 logon name" (Mnumber)
$Context.SetModifiedPropertyValue("postOfficeBox", ($cpoBoxValue))
$Context.SetModifiedPropertyValue("cn", ($cemployeeValue)) #Set "fullname" to MNumber
$Context.SetModifiedPropertyValue("l", ($ccityValue)) # Set "city" to Chat
$Context.SetModifiedPropertyValue("c", ($ccountryValue)) #Set "country" to GB (united kngdom)
$Context.SetModifiedPropertyValue("physicalDeliveryOfficeName", ($cofficeValue)) #Set "office" to <OFFICE>
$Context.SetModifiedPropertyValue("postalCode", ($cpostCodeValue)) #Set "postalcode" to <POSTCODE>
$Context.SetModifiedPropertyValue("accountExpires", ($expiryValue)) #Set account expiry to 1yr from now
$Context.SetNewPassword($defPassword) #Set default password
#$Context.LogMessage("Contractor username: $userLogonName", "Information")