We use cookies to improve your experience.
By your continued use of this site you accept such use.
For more details please see our privacy policy and cookies policy.

Script Repository

Update attributes based on value of another attribute

September 17, 2019 Views: 2211

The script updates attributes based on the value of another attribute. In the example below, attributes City (LDAP name title) and State (LDAP name st) are updated based on the value of the Department Number attribute (LDAP name departmentNumber).

Parameter:

  • $basePropertyName - Specifies the LDAP name of the property whose values will be used as base for the update.
  • $valueMap - Maps values of the property specified in the $basePropertyName variable with the properties to be updated and their values.
Edit Remove
PowerShell
$basePropertyName = "departmentNumber" # TODO: modify me
$valueMap = @{
    "01234" = @{
        "l" = "City1";
        "st" = "State1";
    };
    "57890" = @{
        "l" = "City2";
        "st" = "State2"
    };
} # 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)
}

# Save changes
try
{
    $user.SetInfo()
}
catch
{
    $Context.LogMessage("An error occurred when updating user. Error: " + $_.Exception.Message, "Warning")
}
Comments 0
Leave a comment
Loading...

Got questions?

Support Questions & Answers