0 votes

We are using the following the script and would like to have it updated to use a CSV file instead of the list option.
Thank you in advance for your assistance.

$basePropertyName = "departmentNumber" # TODO: modify me
$valueMap = @{
"0005" = @{ "l" = "Farmington"; "st" = "CT"; };
"0019" = @{ "l" = "Seekonk"; "st" = "MA"; };
"0021" = @{ "l" = "Fall River"; "st" = "MA"; };
"0030" = @{ "l" = "Fairfield"; "st" = "CT"; };
"0033" = @{ "l" = "Stratford"; "st" = "CT"; };
"9999" = @{ "l" = "DoNotRemove"; "st" = "NY"; };

} # TODO: modify me. Example @{<base property value> = <Property map>}. 
# $valueMap = @{"0001" = @{"l" = "City"; "department" = "Dep1"; "st" = "State"}}

# Get base property value
try
{
    $baseValue = $Context.TargetObject.Get($basePropertyName)
}
catch
{
    return # Value not specified
}

# Update properties
$propertyMap = $valueMap[$baseValue]
if ([System.String]::IsNullOrEmpty($propertyMap))
{
    $Context.LogMessage("Properties to update not specified for '$baseValue' value", "Warning")
    return
}

$user = $Context.BindToObjectEx($Context.TargetObject.AdsPath , $True)
foreach ($item in $propertyMap.GetEnumerator())
{
    $user.Put($item.Key, $item.Value)
}

# Build department
$department = [System.String]::Format("{0}, {1} - {2}", @($propertyMap["l"], $propertyMap["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")
}
by (3.2k points)

1 Answer

0 votes
by (257k points)
selected by
Best answer

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")
}
0

Perfect, thank you as always.

Related questions

0 votes
1 answer

Receive "Index operation failed; the array index evaluated to null. Stack trace: at &lt;ScriptBlock&gt;, &lt;No file&gt;: line 104&gt;" and "Index operation failed; the ... $GroupName, $GroupDN." } } #foreach write-output "" Write-Output "" Stop-Transcript

asked Apr 14, 2022 by jbahou (20 points)
0 votes
1 answer

hello i'm new with Adaxes i'm try to creat schuadle task to import a spefice user list by thier username id after that just update City for them by bulk updating . kinly advise

asked Aug 29 by sudox (20 points)
0 votes
1 answer

Hallo Everyone I'm attempting to import a CSV list to Update Current users. I Used the Script 2 from Repostitory https://www.adaxes.com/script-repository/ ... "employeeNumber") # TODO: modify me Test file: sAMAccountName,employeeNumber, Peter.Muster,AB052

asked Jul 13, 2021 by Sandberg94 (340 points)
0 votes
1 answer

Hi, we have replaced our local Exchange server with installation of Exchange Management Tools (EMT) installed directly on Adaxes server. And my question is: How can I force ... this is how 'Set External Senders' option looks in Adaxes config Thanks in advance

asked Apr 1 by KIT (910 points)
0 votes
1 answer

Hello, Similar to exporting the members of a group to a csv file: https://www.adaxes.com/script-repository/export-group-members-to-csv-file-s184.htm I am looking to ... would like to include the memberof csv report in the email as well. Thanks in advance!

asked Feb 7 by JonnyBGood (20 points)
3,175 questions
2,878 answers
7,369 comments
507,162 users