Can't manually change user's password via script

Oliver Anderson
Kilo Sage

I'm trying to "disable" local logon for all accounts imported from AD (so that admins and other local accounts can still use local logon) by setting users' passwords to a random string. Here's the script (partially provided by this ServiceNow doc😞

 

var r = new Packages.java.util.Random();
var str1 = Packages.java.lang.Long.toString(Packages.java.lang.Math.abs(r.nextLong()),36); 
var str2 = Packages.java.lang.Long.toString(Packages.java.lang.Math.abs(r.nextLong()),36);
var newPass = str1 + str2;

var gr = new GlideRecord('sys_user');
gr.get('b8b936b31b4ef05023a02173604bcb67');
gr.user_password.setDisplayValue(newPass);
gr.update();

 

I'm testing on my own non-admin account which is synced from AD. Script output shows successful update, but I am still able to log in with my old password synced from AD.

OliverAnderson_0-1709919333909.png

Any ideas?

1 ACCEPTED SOLUTION

Oliver Anderson
Kilo Sage

The problem was LDAP authentication was enabled lol...

There were no local passwords because users with authenticating to our LDAP server. Once I disabled this, the script worked fine and I was able to log in with the local scrambled password.

View solution in original post

4 REPLIES 4

swathisarang98
Giga Sage
Giga Sage

Hi @Oliver Anderson ,

 

You can refer below article here user have tried something similar to your requirement,

https://www.servicenow.com/community/itom-forum/is-there-a-way-to-set-user-password-via-script/m-p/9... 

 

https://www.servicenow.com/community/developer-forum/generate-random-user-password-in-onafter-transf... 

 

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Swathi Sarang

Amit Pandey
Kilo Sage

Hi @Oliver Anderson 

 

Pls try this-

 

// Assuming 'userid' is the username of the user requesting the password reset
var usr = new GlideRecord('sys_user');
usr.addQuery('active', 'true');
usr.addQuery('user_name', userid);
usr.query();

if (usr.next()) {
    var newpw = generatePassword(8); // Generate a new password
    usr.user_password.setDisplayValue(newpw); // Set the display value for the password
    usr.password_needs_reset = true;
    usr.update();
}

// Function to generate a random password
function generatePassword(length) {
    var availablechars = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    var newPassword = "";
    for (var i = 0; i < length; i++) {
        var randomNumber = Math.floor(Math.random() * availablechars.length);
        newPassword += availablechars[randomNumber];
    }
    return newPassword;
}

 

 

Make sure to replace 'userid' with the actual username of the user requesting the password reset.

 

Regards,

Amit

Leonardoalves14
Tera Contributor

Its my question to, thanks for sharing.

Oliver Anderson
Kilo Sage

The problem was LDAP authentication was enabled lol...

There were no local passwords because users with authenticating to our LDAP server. Once I disabled this, the script worked fine and I was able to log in with the local scrambled password.