Hello Sandra,
As per our check, just removing that part from your script will not work. Here is the updated script that should do the trick:
Import-Module Adaxes
$csvFilePath = "\adaxes01\c$\HR-9\Test Files\ImportUniqueIdentifiers.csv"
$userIdColumn = "EMPLOYEE_ID"
$userIdProperty = "employeeId"
$accountPasswordColumn = "AccountPassword"
$customColumnNames = @{
"LAST_FOUR" = "adm-CustomAttributeText20";
"DOB" = "adm-CustomAttributeText21";
} # TODO: modify me
$aDObjectProperties = @("Manager", "Secretary") # TODO: modify me
# E-mail settings
$to = "recipient@domain.com"
$subject = "Import Unique Identifiers Report"
$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 = $value
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
$user = $Context.BindToObjectByDN($userExists.DistinguishedName)
foreach ($property in $userObject.Keys)
{
$user.Put($property, $userObject[$property])
}
try
{
$user.SetInfoEx(@($userObject.Keys))
}
catch
{
$Context.LogMessage("An error occurred when updating user '$displayName'. Error: " + $_.Exception.Message, "Warning")
}
if ($propertiesToClear.Length -ne 0)
{
foreach ($property in $propertiesToClear)
{
$user.Put($property, $NULL)
}
try
{
$user.SetInfo(@($userObject.Keys))
}
catch
{
$Context.LogMessage("An error occurred when updating user '$displayName'. Error: " + $_.Exception.Message, "Warning")
}
}
if ([System.String]::IsNullOrEmpty($accountPassword))
{
continue
}
try
{
$user.SetPassword($accountPassword)
}
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())