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 (233k 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 by jbahou (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 (320 points)
0 votes
1 answer

I am using this script modified for my testing. Import-Module Adaxes $csvFilePath = "D:\TestFeed\ImportNewUsers.csv" # Path to pick up feed file $userIdColumn = "Employee Number" # TODO: ... initial. I would like to add a 2 for now if the sam isn't unique.

asked Oct 17 by mightycabal (250 points)
0 votes
1 answer

So I need to export a list of all user's Line URI's to a CSV file. Running Adaxes 2021 Version 3.14.18804.0 (64 bit) and Teams Powershell 4.1.0 ... a Microsoft 365 account } finally { # Close the connection and release resources Disconnect-MicrosoftTeams }

asked Aug 4 by TheLexicon (180 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)
2,887 questions
2,606 answers
6,774 comments
121,372 users