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

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

Hello I'm trying to run a custom PowerShell script to request a Workspace ONE Access Sync when I change something in our users or groups. Here is the script: $ClientId = "api ... of having to create 6 independent rules with each of them a copy of the script)?

asked Sep 25, 2021 by ygini (220 points)
0 votes
1 answer

I am trying to send a $context.logmessage from a condition script in a Scheduled Task but I get nothing in the log. Is this not possible? Morten A. Steien

asked Jul 20, 2020 by Morten A. Steien (150 points)
0 votes
1 answer

All of our custom command use %adm-CustomAttributeText1% and we want to update that feild to department. Is there a script that we could run that will look trough out custom commands and charng from one to the other?

asked Feb 28, 2020 by hgletifer (1.3k points)
0 votes
1 answer

Hello. My goal is to run a custom command/script when a user is created and then when the user is deprovisoned. 1. Get the user name 2. Replace international chars 3. Sort ... me } The error I receive is 'Cannot send a content-body with this verb-type.'

asked Apr 8, 2015 by Klas (460 points)
2,807 questions
2,541 answers
6,615 comments
65,307 users