Hello,
Sorry for the delayed reply.
Find the updated script below. A report with errors will be sent to the specified email address after the script execution. If a user does not exist, an error will be added to the report.
The Scheduled Task must be configured for the Domain-DNS object type. The domain here will be used only to trigger the script and does not define the scope of users it will be executed on. If you select, for example, an Organizational Unit as target object, the script will be executed for each OU in the domain.
When selecting activity scope, we are trying to select the domain and do not get the option to deselect child objects.
This behavior is by design. Deselecting domain child objects would remove all the objects under the domain from Activity Scope of the Scheduled Task.
Import-Module Adaxes
$csvFilePath = "\\server\share\ImportedUsers.csv" # TODO: modify me
$accountPasswordColumn = "AccountPassword" # TODO: modify me
$sAMAccountNameColumn = "sAMAccountName" # TODO: modify me
# E-mail settings
$recipient = "recipient@domain.com" # TODO: Modify me
$subject = "Error Report: Import data from csv" # TODO: Modify me
$reportHeader = "<h1><b>Error Report: Import data from csv</b></h1><br/>"# TODO: Modify me
$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%")
$report = New-Object "System.Text.StringBuilder"
try
{
$importedUsers = Import-Csv -Path $csvFilePath -ErrorAction Stop
}
catch
{
$message = "An error occurred while importing CSV file '$csvFilePath'. Error: " + $_.Exception.Message
$Context.LogMessage($message, "Warning")
[void]$report.Append("<li>$message</li>")
$importedUsers = @()
}
foreach ($userFromCSV in $importedUsers)
{
$userObject = @{}
$accountPassword = $NULL
foreach ($property in $userFromCSV.PSObject.Properties)
{
$propertyName = $property.Name
$value = $property.Value
if($propertyName -ieq $accountPasswordColumn -and !([System.String]::IsNullOrEmpty($value)))
{
$accountPassword = ConvertTo-SecureString -AsPlainText $value -Force
continue
}
elseif ($propertyName -ieq $accountPasswordColumn -and [System.String]::IsNullOrEmpty($value))
{
continue
}
if ([System.String]::IsNullOrEmpty($value))
{
continue
}
if ($value -ieq "True" -or $value -ieq "False")
{
$value = [System.Boolean]::Parse($value)
}
$userObject.Add($propertyName, $value)
}
# Check whether the user exists
$userIdentity = $userObject.$sAMAccountNameColumn
try
{
$userExists = Get-AdmUser -Identity $userIdentity `
-AdaxesService localhost -ErrorAction Stop -Server $domainName
}
catch
{
$message = "$userIdentity`: An error occurred while searching user. Error: " + $_.Exception.Message
$Context.LogMessage($message, "Warning")
[void]$report.Append("<li>$message</li>")
continue
}
# If user exists, update account
try
{
Set-AdmUser -Identity $userExists.DistinguishedName -Replace $userObject `
-AdaxesService localhost -Server $domainName -ErrorAction Stop
}
catch
{
$message = "$userIdentity`: An error occurred while updating user. Error: " + $_.Exception.Message
$Context.LogMessage($message, "Warning")
[void]$report.Append("<li>$message</li>")
}
if ([System.String]::IsNullOrEmpty($accountPassword))
{
continue
}
try
{
Set-AdmAccountPassword -Identity $userExists.DistinguishedName -NewPassword $accountPassword `
-Reset -Server $domainName -AdaxesService localhost -ErrorAction Stop
}
catch
{
$message = "$userIdentity`: An error occurred while updating the password for user. Error: " + $_.Exception.Message
$Context.LogMessage($message, "Warning")
[void]$report.Append("<li>$message</li>")
}
}
if ($report.Length -eq 0)
{
return
}
# Build html
$html = $reportHeader + "<ul style=""list-style: none;"">" + $report.ToString() + "</ul>" + $reportFooter
# Send report
$Context.SendMail($recipient, $subject, $NULL, $html)