Hello,
The thing is that you specifies the column and the property for identifying user account incorrectly. The sAMAccountName column contains the value of the sAMAccountName property, not that of the employeeNumber property. Also, as the employeeNumber is not of DN syntax and you have no such properties in the CSV file, the $aDObjectProperties should be set to an empty array. Finally, your script should be exactly as below.
Import-Module Adaxes
$csvFilePath = "\\Server\Update_User.csv" # TODO: modify me
$userIdColumn = "sAMAccountName" # TODO: modify me
$userIdProperty = "sAMAccountName" # TODO: modify me
$accountPasswordColumn = "AccountPassword" # TODO: modify me
$customColumnNames = @{
"sAMAccountName" = "sAMAccountName";
"employeeNumber" = "employeeNumber";
} # TODO: modify me
$aDObjectProperties = @() # TODO: modify me
# E-mail settings
$to = "recipient@domain.com" # TODO: modify me
$subject = "Import report" # TODO: modify me
$reportHeader = "<h2>Import report</h2>"
$reportFooter = "<hr /><p><i>Please do not reply to this e-mail, it has been sent to you for notification purposes only.</i></p>" # TODO: modify me
$domainName = $Context.GetObjectDomain("%distinguishedName%")
$importedUsers = Import-Csv -Path $csvFilePath
$moreThanOneUserFound = New-Object "System.Text.StringBuilder"
$userNotFound = New-Object "System.Text.StringBuilder"
foreach ($userFromCSV in $importedUsers)
{
$userObject = @{}
$accountPassword = $NULL
$propertiesToClear = @()
foreach ($property in $userFromCSV.PSObject.Properties)
{
$columnName = $property.Name
$value = $property.Value
if ($columnName -ieq $accountPasswordColumn -and !([System.String]::IsNullOrEmpty($value)))
{
$accountPassword = ConvertTo-SecureString -AsPlainText $value -Force
continue
}
elseif ($columnName -ieq $accountPasswordColumn -and [System.String]::IsNullOrEmpty($value))
{
continue
}
if ($customColumnNames.ContainsKey($columnName))
{
$propertyName = $customColumnNames[$columnName]
}
else
{
$propertyName = $columnName
}
if ([System.String]::IsNullOrEmpty($value))
{
$propertiesToClear += $propertyName
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)
}
# Check whether the user exists
$valueForSearch = $userObject.$userIdProperty
$userExists = Get-AdmUser -LdapFilter "($userIdProperty=$valueForSearch)" `
-AdaxesService localhost -ErrorAction SilentlyContinue -Server $domainName
if ($NULL -eq $userExists)
{
$userNotFound.Append("<li>$valueForSearch</li>")
continue
}
if ($userExists -is [System.Array])
{
$moreThanOneUserFound.Append("<li>$valueForSearch</li>")
continue
}
# If user exists, update account
$displayName = $userExists.Name
try
{
Set-AdmUser -Identity $userExists.DistinguishedName -Replace $userObject `
-AdaxesService localhost -Server $domainName -ErrorAction Stop
}
catch
{
$Context.LogMessage("An error occurred when updating user '$displayName'. Error: " + $_.Exception.Message, "Warning")
}
if ($propertiesToClear.Length -ne 0)
{
try
{
Set-AdmUser -Identity $userExists.DistinguishedName -Clear $propertiesToClear `
-AdaxesService localhost -Server $domainName -ErrorAction Stop
}
catch
{
$Context.LogMessage("An error occurred when updating user '$displayName'. Error: " + $_.Exception.Message, "Warning")
}
}
if ([System.String]::IsNullOrEmpty($accountPassword))
{
continue
}
try
{
Set-AdmAccountPassword -Identity $userExists.DistinguishedName -NewPassword $accountPassword `
-Reset -Server $domainName -AdaxesService localhost -ErrorAction Stop
}
catch
{
$Context.LogMessage("An error occurred when updating the password for user '$displayName'. Error: " + $_.Exception.Message, "Warning")
}
}
if ($moreThanOneUserFound.Length -eq 0 -and $userNotFound.Length -eq 0)
{
return
}
# Build report
$html = New-Object "System.Text.StringBuilder"
$html.Append($reportHeader)
if ($userNotFound.Length -ne 0)
{
$html.Append("<b>The following users were not found in Active Directory:</b>")
$html.Append("<ol>")
$html.Append($userNotFound.ToString())
$html.Append("</ol>")
}
if ($moreThanOneUserFound.Length -ne 0)
{
$html.Append("<b>Found more than one user with the following value of the $userIdProperty property:</b>")
$html.Append("<ol>")
$html.Append($moreThanOneUserFound.ToString())
$html.Append("</ol>")
}
# Send report
$Context.SendMail($to, $subject, $NULL, $html.ToString())