0 votes

Hello we want to renaming a user with changing the userhome directory and the exchange properties.

i adding the code we have:

Import-Module Adaxes

function DoesUserExist($username)
{
    $obj = Get-AdmUser $username -ErrorAction SilentlyContinue
    return $obj -ne $null
}

#first test of username
$username = "%firstname:lower,2%%lastname:lower,2%"

$newValuesAMAccountName = $Context.GetModifiedPropertyValue("sAMAccountName")
$newValuemailNickname = $Context.GetModifiedPropertyValue("mailNickname")

#no entry
$Context.LogMessage("Aktueller sAMAccountName = '$newValuesAMAccountName' und MailNickname = '$newValuemailNickname'.", "Information")

#Check whether an object with the same DN already exists.
if ((DoesUserExist $username))
{
    #second test
    $username = "%firstname:lower,2%%lastname:lower,3%"
    if ((DoesUserExist $username))
    {
        #third test
        $username = "%firstname:lower,3%%lastname:lower,3%"
        if ((DoesUserExist $username))
        {
           #it should work before, if not get this message
           $Context.LogMessage("Bitte Namenskürzel prüfen.", "Information")
           return
        }
        else
        {
            #$Context.LogMessage("Der sAMAccountName wurde auf '$username' gesetzt.", "Information") 
            $newValuesAMAccountName = $username
            $newValuemailNickname = $username
        }
    }
    else
    {
        #$Context.LogMessage("Der sAMAccountName wurde auf '$username' gesetzt.", "Information")
        $newValuesAMAccountName = $username
        $newValuemailNickname = $username  
    }
}
else
{
    #$Context.LogMessage("Der sAMAccountName wurde auf '$username' gesetzt.", "Information") 
    $newValuesAMAccountName = $username
    $newValuemailNickname = $username
}

#got the right entries
$Context.LogMessage("Neuer sAMAccountName = '$newValuesAMAccountName' und MailNickname = '$newValuemailNickname'.", "Information")

$Context.SetModifiedPropertyValue("sAMAccountName", $newValuesAMAccountName)
$Context.SetModifiedPropertyValue("mailNickname", $newValuemailNickname)


$newValue = $username
$divValue = "newObj"

#userhome path
$Path = "\\netplans.de\npdfs2\userhome\"

Rename-Item -Path $Path$divValue -NewName $Path$newValue

$Context.LogMessage("New Full Path is $Path$newValue.", "Information") 

after this Skript we try to set the new sAMAccountName for: image.png

But the sAMAccountName and Alias for Mail, is ever the old entry. How can we use the new sAMAccountName?

by (80 points)
0

Hello,

For troubleshooting purposes, please, clarify where the provided script is executed. Is it executed in a business rule triggering Before renaming a user? Additionally, please, provide us with a screenshot of the After renaming a user business rule in a higher resolution as currently, it is not readable. You can post the screenshot here, or send it to us at support@adaxes.com.

0

It is executed in a business rule triggering After renaming a user. In formular we only change the fullname, firstname and lastname.

image.png

Then we executed in a business rule triggering Before renaming a user.

Run little script: $oldValue= $Context.TargetObject.Get("sAMAccountName") $divValue="newObj" $Path = "\netplans.de\npdfs2\userhome\"

Rename-Item -Path $Path$oldValue -NewName $Path$divValue

$Context.LogMessage("Full Path is $Path$divValue.", "Information")

and after that we have the script which is triggering after renaming a user, i send at first.

image.png

image.png

But the sAMAccountName is ever the old sAMAccountName, i dont know why.

Hope you can understood.

1 Answer

+1 vote
by (12.6k points)

Hello,

Thank you for clarifying. The GetModifiedPropertyValue and SetModifiedPropertyValue methods can only be used in scripts executed in business rules that trigger Before certain operations (e.g. Before renaming a user, Before creating a user, etc.). In your case, to achieve the desired, you need to execute the script you provided in the original post in the business rule that triggers Before renaming a user.

When a script is executed in a business rule triggering After an operation (e.g. After renaming a user), to get property values you need to use the Get method. To update a property value, use the Put method. After setting a property value, you need to save the changes using the SetInfo method. Additionally, pay attention that all value references used in actions, conditions and scripts resolve before the conditions are checked and the actions are executed. It means that if you update a certain property in an action (it can be a built-in action or a script) and you need to update other properties based on the modified property value, it must be done in a script without using value references.

0

Now i have update the script with using GET, PUT and SetInfo(, it looks good. But the exchange Properties which i set afterwards are not correct. Here i got the old sAMAccountname.


Import-Module Adaxes

function DoesUserExist($username)
{
    $obj = Get-AdmUser $username -ErrorAction SilentlyContinue
    return $obj -ne $null
}


$ns = New-Object("Softerra.Adaxes.Adsi.AdmNamespace")
$service = $ns.GetServiceDirectly("npadx1.netplans.de")

# Bind to object
$userDN = "%distinguishedName%"
$user = $service.OpenObject("Adaxes://$userDN", $null, $null, 0)

# Get single-valued property
$oldValue = $user.Get("sAMAccountName")

$username = "%firstname:lower,2%%lastname:lower,2%"

#Check whether an object with the same DN already exists.
if ((DoesUserExist $username))
{
    $username = "%firstname:lower,2%%lastname:lower,3%"
    if ((DoesUserExist $username))
    {
        $username = "%firstname:lower,3%%lastname:lower,3%"
        if ((DoesUserExist $username))
        {
           $Context.LogMessage("Bitte Namenskürzel prüfen.", "Information")
           return
        }
    }

}

$user.Put("sAMAccountName", $username)

$newValue = $user.Get("sAMAccountName")

#Ausgabe im Skript
$Context.LogMessage("Alter sAMAccountName: '$oldValue'  Neuer sAMAccountName: '$newValue' ", "Information")

$user.SetInfo()

$divValue = "newObj"

# Pfad zum Ordner festlegen
$Path = "\\netplans.de\npdfs2\userhome\"


Rename-Item -Path $Path$divValue -NewName $Path$username

$Context.LogMessage("New Full Path is $Path$newValue.", "Information")

image.png

Alias is aafr, which is the old sAMAccountname. Do you have an idea what goes wrong?

image.png

0

Hello,

Now i have update the script with using GET, PUT and SetInfo(, it looks good. But the exchange Properties which i set afterwards are not correct. Here i got the old sAMAccountname.

As we mentioned in our previous reply, in your case, the best way to achieve the desired is to execute the original script you provided in a business rule that triggers Before renaming a user. Did you try this approach?

Alias is aafr, which is the old sAMAccountname. Do you have an idea what goes wrong?

Pay attention that all value references used in actions, conditions and scripts resolve before the conditions are checked and the actions are executed. It means that if you update a certain property in an action (it can be a built-in action or a script) and you need to update other properties based on the modified property value, it must be done in a script without using value references.

0

Hello

As we mentioned in our previous reply, in your case, the best way to achieve the desired is to execute the original script you provided in a business rule that triggers Before renaming a user. Did you try this approach?

=> Now I've tried it and it works, thank you. It can be so simple.

Related questions

0 votes
1 answer

This script description says it can find the manager via FullName Distinguished name or Display name. Wondering if we can change it to use employeeID or SamAccountName.

asked Oct 24, 2022 by mightycabal (1.0k points)
0 votes
1 answer

Hi All, I am currently using the 30 day free trial of Adaxes and seeing if we can use it to achieve our method of user provisioning. I am looking into server-side ... variable value within an SQL query Can this be achieved? Any help is much appreciated, Thanks

asked Feb 1 by Lewis (40 points)
0 votes
1 answer

Is the further clarification on how to join the Adaxes service to the configuration set during a multi-sever upgrade

asked Nov 16, 2022 by itsupport (20 points)
0 votes
1 answer

We have a 3rd party vendor that we are able to add users based on AD security groups. What I need to do is set a parameter for the number of available licenses and whenever ... the group is 495 I would like an email to trigger telling me to add more licenses.

asked Oct 12, 2022 by A_Pastor (70 points)
0 votes
1 answer

Hello, I'd like setup a new custom command on the Administrator dashboard that would run the following tasks against a disabled user account simultaneously. Enable their account ... the email when using the %unicodePwd% value. Is there a workaround for this?

asked Apr 23, 2020 by sirslimjim (480 points)
3,472 questions
3,165 answers
8,057 comments
547,017 users