Hello,
Find an updated script below:
$basePropertyName = "departmentNumber" # TODO: modify me
$baseColumnName = "ID" # TODO: modify me
$csvFilePath = "\\Server\share\MyFile.csv" # TODO: modify me
# Get base property value
try
{
$baseValue = $Context.TargetObject.Get($basePropertyName)
}
catch
{
return # Value not specified
}
# Import CSV
try
{
$records = Import-Csv -Path $csvFilePath -ErrorAction Stop
}
catch
{
$Context.LogMessage("An error occurred when importing CSV. Error: " + $_.Exception.Message, "Warning")
return
}
# Search record in CSV
$record = $records | Where {$_.$baseColumnName -eq $baseValue}
if ($record -eq $NULL)
{
$Context.LogMessage("Properties to update not specified for '$baseValue' value", "Warning")
return
}
elseif ($record -is [System.Array])
{
$Context.LogMessage("Found more than one record with value '$baseValue' in column '$baseColumnName'", "Warning")
return
}
# Update properties
$user = $Context.BindToObjectEx($Context.TargetObject.AdsPath , $True)
foreach ($property in $record.PsObject.Properties)
{
if ($property.Name -eq $baseColumnName)
{
continue
}
$user.Put($property.Name, $property.Value)
}
# Build department
$department = [System.String]::Format("{0}, {1} - {2}", @($record.l, $record.st, $baseValue)) # TODO: modify me
$user.Put("department", $department)
# Commit changes
try
{
$user.SetInfo()
}
catch
{
$Context.LogMessage("An error occurred when updating user. Error: " + $_.Exception.Message, "Warning")
}