I am using this script modified for my testing.
Import-Module Adaxes
$csvFilePath = "D:\TestFeed\ImportNewUsers.csv" # Path to pick up feed file
$userIdColumn = "Employee Number" # TODO: modify me
$userIdProperty = "EmployeeNumber" # TODO: modify me
$customColumnNames = @{
"Employee ID/Contingent Worker ID" = "EmployeeID";
"First Name" = "GivenName";
"Last Name" = "sn";
"Middle Initial" = "Initials"
"Primary Job Title" = "title";
"Primary Position Cost Sentor" = "Department";
"Future Term Date" = "AccountExpirationDate";
"Subsidiary" = "Division";
"Entity" = "Company";
"Location" = "City";
"Suffix" = "PersonalTitle"
} # Set Manager as an AD Object
$aDObjectProperties = @("Manager") # Set Manager as an AD Object
$ouDN = "OU=Staging,DC=ADXTEST,DC=BENEFIS,DC=ORG" # OU to create users in
# E-mail settings
#$to = "heathhaywood@benefis.org" # TODO: modify me
#$subject = "Import New Users Report" # TODO: modify me
#$reportHeader = "<h2>Import report</h2>"
#$reportFooter = "<hr /><p><i>Please See Attached Report"
#$domainName = $Context.GetObjectDomain($ouDN)
#$importedUsers = Import-Csv -Path $csvFilePath
$rootDSE = $Context.BindToObject("Adaxes://RootDSE")
$userFound = New-Object "System.Text.StringBuilder"
foreach ($userFromCSV in $importedUsers)
{
$userObject = @{}
$accountPassword = $NULL
foreach ($property in $userFromCSV.PSObject.Properties)
{
$columnName = $property.Name
$value = $property.Value
if ($customColumnNames.ContainsKey($columnName))
{
$propertyName = $customColumnNames[$columnName]
}
else
{
$propertyName = $columnName
}
if ([System.String]::IsNullOrEmpty($value))
{
continue
}
# Parse special columns
if ($columnName -ieq $userIdColumn)
{
$propertyName = $userIdProperty
}
elseif ($aDObjectProperties -icontains $columnName)
{
$aDObject = Get-AdmObject -Filter {(Name -eq $value) -or (DisplayName -eq $value) -or (distinguishedName -eq $value)} `
-AdaxesService localhost -ErrorAction SilentlyContinue -Server $domainName
if ($aDObject -is [System.Array])
{
$Context.LogMessage("Found more than one object with identity '$value'.", "Warning")
continue
}
if ($aDObject -eq $NULL)
{
$Context.LogMessage("Could not locate object with identity '$value'.", "Warning")
continue
}
$value = $aDObject.DistinguishedName
}
if ($value -ieq "True" -or $value -ieq "False")
{
$value = [System.Boolean]::Parse($value)
}
$userObject.Add($propertyName, $value)
}
# Build sAMAccountName
$sAMAccountName = $userObject.sn.substring(0,4) + $userObject.GivenName.substring(0,3) + $userObject.initials
# Check whether the user exists
$valueForSearch = $userObject.$userIdProperty
$userExists = Get-AdmUser -LdapFilter "($userIdProperty=$valueForSearch)" `
-AdaxesService localhost -ErrorAction SilentlyContinue -Server $domainName
if ($NULL -eq $userExists)
{
# Build user name
$displayName = $userObject.GivenName + " " + $userObject.SN
$parameters = @{
"Path" = $ouDN
"Name" = $displayName;
"SamAccountName" = $SAMAccountName.ToLower()
"Server" = $domainName;
"AdaxesService" = "localhost"
"Enabled" = $True
"OtherAttributes" = $userObject
"ErrorAction" = "Stop"
}
# Generate password
$userAdsPathObj = New-Object Softerra.Adaxes.Adsi.AdsPath "Adaxes://$ouDN"
$rdnValue = [Softerra.Adaxes.Ldap.Rdn]::EscapeAttributeValue($displayName)
$userAdsPathObj.CreateChildPath("CN=$rdnValue")
$passwordString = $rootDSE.GeneratePasswordForNewUser($userAdsPathObj)
$passwordSecureString = ConvertTo-SecureString -AsPlainText $passwordString -Force
$parameters.Add("AccountPassword", $passwordSecureString)
# Create a new user account
try
{
New-AdmUser @parameters
}
catch
{
$Context.LogMessage("An error occurred when creating user '$displayName'. Error: " + $_.Exception.Message, "Warning")
}
}
else
{
$userFound.Append("<li>$valueForSearch</li>")
}
}
if ($userFound.Length -eq 0)
{
return
}
# Build report
#$html = New-Object "System.Text.StringBuilder"
#$html.Append($reportHeader)
#$html.Append("<b>The following users were found in Active Directory:</b>")
#$html.Append("<ol>")
#$html.Append($userFound.ToString())
#$html.Append("</ol>")
#$html.Append($reportFooter)
#$Context.SendMail($to, $subject, $NULL, $html.ToString())
In this script I use # Build sAMAccountName
$sAMAccountName = $userObject.sn.substring(0,4) + $userObject.GivenName.substring(0,3) + $userObject.initials
This sets the Sam Name to first 4 of last name first three of first name and middle initial. I would like to add a 2 for now if the sam isn't unique.