0 votes

Would it be possible to update this script to modify three or more attributes based off one?

ex. when updating departmentnumber "0666" we want the following attributes to update automatically: City, State and department (we build our department from city, state and dept number).

http://www.adaxes.com/script-repository ... e-s379.htm

by (3.2k points)
0

Hello,

we build our department from city, state and dept number

What exactly do you mean?

Also, could you specify how City, State and department depend on the departmentnumber?

0

our example would be as follows: we update a department number to "0001" and the following attributes should change with it: city state and department.
ex. dept number = 0001 city= syracuse state = NY department = syracuse, ny - 0001

1 Answer

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

Hello,

Thank you for clarifying. Here you are:

$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)
}

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

Is it possible to pull this from a CSV file? We have 700 locations we would have to type out in the script. We could try a SQL table/database if needed but a CSV file would be better.

0

Hello,

Sure, find the script below. The Scheduled Task must be configured for the Domain-DNS object type. The domain here will be used only to trigger the script and does not define the scope of users it will executed on.

Import-Module Adaxes

$csvFilePath = "\\server\share\ImportedUsers.csv" # TODO: modify me
$userIdColumn = "DepartmentNumber" # TODO: modify me
$userIdProperty = "departmentNumber" # TODO: modify me

$domainName = $Context.GetObjectDomain("%distinguishedName%")
$importedUsers  = Import-Csv -Path $csvFilePath

foreach ($userFromCSV in $importedUsers)
{
    $userObject = @{}
    foreach ($property in $userFromCSV.PSObject.Properties)
    {
        $columnName = $property.Name
        $value = $property.Value

        # Parse special columns
        if ($columnName -ieq $userIdColumn)
        {
            $propertyName = $userIdProperty
        }
        else
        {
            $propertyName = $columnName
        }

        $userObject.Add($propertyName, $value)
    }

    # Build department
    $department = [System.String]::Format("{0}, {1} - {2}", @($userObject["l"], $userObject["st"], $userObject.$userIdProperty)) # TODO: modify me
    $userObject.Add("department", $department)

    # Check whether the user exists
    $valueForSearch = $userObject.$userIdProperty
    $userExists = Get-AdmUser -LdapFilter "($userIdProperty=$valueForSearch)" `
        -AdaxesService localhost -Server $domainName

    if ($userExists -eq $NULL)
    {
        $Context.LogMessage("User with value '$valueForSearch' in property '$userIdProperty' does not exists", "Warning")
        continue
    }

    if ($userExists -is [System.Array])
    {
        $Context.LogMessage("Found more than one user with value '$valueForSearch' in property '$userIdProperty'", "Warning")
        continue
    }

    # If user exists, update account
    try
    {
        Set-AdmUser -Identity $userExists.DistinguishedName -Replace $userObject `
            -AdaxesService localhost -Server $domainName -ErrorAction Stop
    }
    catch
    {
        $Context.LogMessage("An error occurred when updating user '$($userExists.Name)'. Error: " + $_.Exception.Message, "Warning")
    }
}
0

thank you this worked just as we wanted. Is there a way to change an attribute across many different attributes.

ex. when using this script and changing the department number we also want the Display Name and Description to modify the department number as well.
before change: (displayName) DA 0666 Jay Klinkowsky (description) DA 0666
after change: (displayName) DA 1000 Jay Klinkowsky (description) DA 1000

The existing information should be retained and only the department Number should be modified.

0

Hello,

This can be achieved using Property Patterns. You will need to update the default values for displayName and description properties. To do so:

  1. Launch Adaxes Administration Console.
  2. Navigate to Configuration\Property Patterns\Builtin and select User Pattern.
  3. In the Result Pane, double-click Display Name property.
  4. Enter the following into the Generate default value field: %st% %departmentNumber% %firstname% %lastname%.
  5. Select on creating and editing objects, click OK and save the changes.

For description property you can use the following template: %st% %departmentNumber%.

0

We cannot use the standard property pattern solution, as we have different data for each jobCode in the company.

Ideally we need to just be able to change the departmentNumber and leave the existing data.

0

Hello,

If you edit a user, only the values that were updated will change in the Display Name and Description fields according to the Property Pattern.

For example: The script we made for you updates City, State and Department properties if Department Number has changed. If the script does not change State property, it will not change in the Display Name and Description fields. However Department Number will be updated in both fields.

0

Thank you but the property pattern value is not he "st" = state that we populate it is a abbreviation of the job title. DA = Dental Assistant. So making that property pattern will not achieve the goal we are attempting. We need a way to just change the requested attribute within the description or display name field without altering the other data.

0

Hello,

The following version of the script will do the job:

Import-Module Adaxes

$csvFilePath = "\\server\share\ImportedUsers.csv" # TODO: modify me
$userIdColumn = "DepartmentNumber" # TODO: modify me
$userIdProperty = "departmentNumber" # TODO: modify me

function UpdateDepartmentNumberInProperty($string, $pattern, $departmentNumber, $userObject, $propertyName, $userName)
{
    $result = $string | Select-String -Pattern $pattern
    if ($result -eq $NULL)
    {
        $Context.LogMessage("The department number was not found in the $propertyName '$string' for user '$userName'. ", "Warning")
        return $NULL
    }

    $oldDeprtmentNumber = $result.Matches[0].Value

    $string = $string.Replace($oldDeprtmentNumber, $departmentNumber)
    $userObject.Add($propertyName, $string)
}

$domainName = $Context.GetObjectDomain("%distinguishedName%")
$importedUsers = Import-Csv -Path $csvFilePath

foreach ($userFromCSV in $importedUsers)
{
    $userObject = @{}
    foreach ($property in $userFromCSV.PSObject.Properties)
    {
        $columnName = $property.Name
        $value = $property.Value

        # Parse special columns
        if ($columnName -ieq $userIdColumn)
        {
            $propertyName = $userIdProperty
        }
        else
        {
            $propertyName = $columnName
        }

        $userObject.Add($propertyName, $value)
    }

    # Build department
    $department = [System.String]::Format("{0}, {1} - {2}", @($userObject["l"], $userObject["st"], $userObject.$userIdProperty)) # TODO: modify me
    $userObject.Add("department", $department)

    # Check whether the user exists
    $valueForSearch = $userObject.$userIdProperty
    $userExists = Get-AdmUser -LdapFilter "($userIdProperty=$valueForSearch)" `
        -AdaxesService localhost -Server $domainName -Properties "displayName", "description"

    if ($userExists -eq $NULL)
    {
        $Context.LogMessage("User with value '$valueForSearch' in property '$userIdProperty' does not exist", "Warning")
        continue
    }

    if ($userExists -is [System.Array])
    {
        $Context.LogMessage("Found more than one user with value '$valueForSearch' in property '$userIdProperty'", "Warning")
        continue
    }

    # Update Display name
    UpdateDepartmentNumberInProperty $userExists.DisplayName "(?<=.+\s)[0-9]+(?=\s.+)" $userObject.$userIdProperty $userObject "displayName" $userExists.Name

    # Update Description
    UpdateDepartmentNumberInProperty $userExists.Description "(?<=.+\s)[0-9]+$" $userObject.$userIdProperty $userObject "description" $userExists.Name

    # Update account
    try
    {
        Set-AdmUser -Identity $userExists.DistinguishedName -Replace $userObject `
            -AdaxesService localhost -Server $domainName -ErrorAction Stop
    }
    catch
    {
        $Context.LogMessage("An error occurred when updating user '$($userExists.Name)'. Error: " + $_.Exception.Message, "Warning")
    }
}

Related questions

0 votes
1 answer

Hey Support, i hope you can help me with this and how to build it in Adaxes. I want to have a one click action, like a custom command, in the web interface that ... (static) group B Update the members dynamic groups C &amp; D Thanks for your assistance, Markus

asked Nov 29, 2021 by roppiffm (130 points)
0 votes
1 answer

Hello, I need to modify e-mails for all users in an OU. Does anyone know how to do it quickly? Thanks

asked Sep 24, 2009 by tjohns (20 points)
0 votes
1 answer

I'm trying to combine these two scripts to effectively store a user's group memberships in customattributebinary5 and then be able to copy and paste those memberships to a ... ) $Context.LogMessage("Added the user to group '$groupName'", "Information") }

asked Jan 24, 2020 by yourpp (540 points)
0 votes
1 answer

Update group membership based on one property values. I am trying to find a script that resembles "Update group membership based on two property value" but just for one value.

asked Apr 7, 2022 by lee_thomas (20 points)
0 votes
1 answer

We are looking to update all AD user records with one CSV file. Can you point us in the correct direction?

asked Oct 19, 2020 by willy-wally (3.2k points)
3,588 questions
3,277 answers
8,303 comments
548,102 users