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 (213k 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

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 (170 points)
0 votes
1 answer

Hello, is it possible to add computers to the basket, imported from a csv file? We get a list from our client team to disable computer accounts in bulk. regards Helmut

asked Feb 22, 2021 by a423385 (510 points)
0 votes
1 answer

We are working with an HR package that will send us a CSV file every 4 hours with a list of users that need to be created, modified or deleted from our environment. The CSV ... change, etc.) Is there a script that can manage all of that on a scheduled basis?

asked Sep 2, 2020 by RayBilyk (180 points)
0 votes
1 answer

Hi all, We're looking to buy but have some questions first. Our HRIS can spit out a csv every 5 mins. Can Adaxes import that csv and compare it to our AD, ... changes but produce a report detailing what WOULD happen if the import were to run normally? Thanks!

asked Oct 2, 2017 by gfreeman (120 points)
0 votes
1 answer

Hi, is it possible to use custom Powershell script to determine, who will be able to modify AD object property (for example managedBy)? So it is not manager of given object who can edit this property, but anyone who pass checks in Powershell script..

asked Apr 7, 2020 by KIT (520 points)
2,640 questions
2,375 answers
6,305 comments
986,043 users