0 votes

How can I set that a password never expires for an AD account through SDK scripting?

eg:
[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

$userDN = "[DN]"
$user = $admService.OpenObject("Adaxes://$userDN", $NULL, $NULL, 0)

$user.Put ?
$user.SetInfo()

With regards,

Thnx Remco

by (780 points)

1 Answer

0 votes
by (216k points)

Hello Remco,

To set the Password never expires option for a user, you need to set the ADS_UF_DONT_EXPIRE_PASSWD flag in Account Options. Also, you need to disable the User must change password at next logon option (if it is set), which is the same as assigning the value of -1 to the Password Last Set property.

Here's an example on how to do this with the help of a script:

[Reflection.Assembly]::LoadWithPartialName("Softerra.Adaxes.Adsi")

$admNS = New-Object "Softerra.Adaxes.Adsi.AdmNamespace"
$admService = $admNS.GetServiceDirectly("localhost")

$userDN = "CN=John Smith,CN=Users,DC=example,DC=com"
$user = $admService.OpenObject("Adaxes://$userDN", $NULL, $NULL, 0)

[int]$passwordNeverExpires = [Softerra.Adaxes.Interop.Adsi.PersistentObjects.ADS_USER_FLAG_ENUM]::ADS_UF_DONT_EXPIRE_PASSWD

# Set the 'Password never expires' flag for the user
$user.Put("userAccountControl", $passwordNeverExpires)

# Set a bitmask for the Account Options property so that 
# only the 'Password never expires' flag gets updated
$user.PutPropertyItemMask("userAccountControl", $passwordNeverExpires)

# Remove the 'User must change password at next logon' flag if it is set
$passwordLastSet = $user.Get("pwdLastSet")
if ($passwordLastSet -eq 0)
{
    $user.Put("pwdLastSet", -1)
}

# Save changes
$user.SetInfo()

Related questions

0 votes
1 answer

I'd like the Password Never Expires to exclude certain users. Since it is script based is the only way to do so in the script? I have checked where I am aware and I do not see the possibility of doing this as it is currently configured. Thank you

asked 5 days ago by msheppard (240 points)
0 votes
1 answer

Hello, is there a way to remove "Password Never Expires" Check Box only from the "Reset Password" operation dialog? I see you can hide the whole Account Options section and ... . But I would like just to remove the "Password Never Expires" check box. Cheers

asked Feb 5, 2016 by jheisley (590 points)
0 votes
1 answer

I've got the script working as is and would like to add a column to display the number of days left before the password expires. I attempted to use adm-AccountExpiresDaysLeft ... error, probably because I don't know how to convert it to a displayable format.

asked Feb 12, 2021 by sandramnc (870 points)
0 votes
1 answer

We have a pretty generic installation of Adaxes 2013.1. We'd like the HelpDesk to be able to see the Password Expiration date. Currently they see "Password Expires ... 'Password Last Set' Property User Allow Write 'User Cannot Change Password' Property User

asked Aug 21, 2013 by theckel (520 points)
0 votes
1 answer

Hello, My "Never logged on users" report also shows users that recently logged on. My "Recently logged on users" report shows nobody. How can we change this? Best regards, Roel

asked Jan 30, 2013 by roel (40 points)
3,541 questions
3,232 answers
8,225 comments
547,798 users