0 votes

Hi I am trying to utilise the ADSI more and srver side scripting as an attempt to gain a wider knowledge and understanding of the Adaxes objects and interfaces.

I have created a scipt to run after a business rule is triggered but it never seems to execute the put() method.

I understand that ther is a cache and that I need to get() the properties that I want to execute on which I am doing in my script.

The script is intended to handle username and name changes upon approval after the user has changed "sn"

if ($Context.IsPropertyModified("sn"))
{
    $modifiedlastname = $Context.GetModifiedPropertyValue("sn")

    $newValue = $modifiedlastname.Trim()

    $Context.SetModifiedPropertyValue("sn", $newValue)




    Try 

    {
        $adspath =  $context.TargetObject.AdsPath
        $adspath | out-null 

        $user = $Context.BindToObject($adspath)
        $user | out-null

        $firstname = "%givenName%"
        $firstname | out-null
        $sn = $newvalue

        $fullname = "$firstname $sn"
        $Username = "$firstname.$sn"
        $fullname | out-null
        $username | out-null

        $propertynames = $Context.TargetObject.GetInfoEx(@("samaccountname","userprincipalname","mail","displayname","cn"), 0)
        $propertynames | Out-Null

        $context.targetobject.Put("samaccountname",$Username)
        $context.targetobject.Put("userprincipalname",$Username+'@domain.com')
        $context.targetobject.Put("mail",$fullname+'@domain.com')
        $context.targetobject.Put("displayname",$fullname)
        $context.targetobject.Put("cn",$fullname)

        # Commit to the directory
        $user.SetInfo()

    }
    catch
{
    $Context.LogException($_.Exception)
}

}
by (350 points)

1 Answer

0 votes
by (13.8k points)

Hello Will,

The changes are not applied because the Put method is called for the TargetObject property, but the SetInfo method is called for the $user variable. Despite the fact that the same user is referenced by TargetObject property and the $user variable, these are considered different objects in terms of PowerShell. Moreover, there is no need to additionally bind to the user as the TargetObject property of the $Context variable already represents an instance of the user object on which the script is executed.

As far as we understand, you need to change property values once the user last name has changed. If so, there is actually no need to use a PowerShell script. It can be done using a business rule triggering After updating a user. The rule should look like the following:

image.png

If you still need to update property values using a PowerShell script, you can use the approach like the following:

$fullName = "%firstname% %lastname%"
$userName = "%firstname%.%lastname%"

try 
{
    # Update property values
    $Context.TargetObject.Put("sAMAccountName", $userName)
    $Context.TargetObject.Put("userPrincipalName", $userName + "@domain.com")
    $Context.TargetObject.Put("mail", $userName + "@domain.com")
    $Context.TargetObject.Put("displayName", $fullName)
    $Context.TargetObject.Put("cn", $fullName)

    # Save the changes
    $Context.TargetObject.SetInfo()

}
catch
{
    $Context.LogMessage($_.Exception.Message, "Warning")
}

Related questions

0 votes
1 answer

Trying to create a custom command to run a powershell script, but I keep getting an error saying it can't find the file or path supplied for launching powershell. ... and I can launch powershell from this location using Command Prompt. Am I missing something?

asked Oct 24 by cstaub (100 points)
0 votes
1 answer

We have a rule setup that when a user requests membership into a group it will email approvers of the group for approval. I would like to create a report that sends out a list of ALL approvers for every group we have approvals setup for.

asked Dec 18, 2023 by jujones79 (20 points)
0 votes
1 answer

I've just started using Adaxes for the first time so there may be something obvious that I'm missing. I've created a business rule to create a home directory ... or does the user have to be created through the Adaxes Administration Console or Web Interface?

asked May 7, 2012 by bemho (520 points)
0 votes
1 answer

For instance to execute a powershell script that enable MFA for all member in that group?

asked Jan 27, 2023 by samuel.anim-addo (20 points)
0 votes
1 answer

Hi, we've been using this script for some time but after the upgrade to 2023 it's now erroring out as below: As you can see I've done some logging out of ... .BindToObject($groupPath) is not returning an object. Any suggestions of how to fix? Thanks, Allister

asked Dec 1, 2022 by Allister (20 points)
3,538 questions
3,229 answers
8,224 comments
547,747 users