Is there a way to set user_password via script?

rushputin
Mega Expert

Something that's come up a couple of times, now, is the need to set a default password for a set of users (generally because we're about to kick off UAT).   I've tried scripting it before, and have never had any success and inevitably have to just do it by hand... which bothers me.

 

I've tried doing it the straightforward way:

 

var gr = new GlideRecord('sys_user');

 

if (gr.get(userId)) {

  gr.user_password = newPassword;

  gr.update();

}

 

Having seen GlideEncrypter using 2-way encrypted fields, I've tried that:

 

var gr = new GlideRecord('sys_user');

 

if (gr.get(userId)) {

    var encrypter = new GlideEncrypter();


  gr.user_password = encrypter.encrypt(newPassword);

  gr.update();

}

 

I've also looked at the business rule that encrypts password & password2 system properties, which gets me this (which also doesn't work):

 

var gr = new GlideRecord('sys_user');

 

if (gr.get(userId)) {

  var cryptoService = GlideCryptoService.getInstance();

  gr.user_password = cryptoService.encryptWithAnyCKP(newPassword);

  gr.update();

}

 

None of these actually set a password that can actually be authenticated against.

 

What am I missing?

1 ACCEPTED SOLUTION

TrevorK
Kilo Sage

I was just installing the self-service password reset feature, and here is the code they use in the script include. There is one distinct difference compared to what you have tried, it sets the display value for the password. I hope this helps!



          var usr = new GlideRecord('sys_user');


          usr.addQuery('active', 'true');


          usr.addQuery('user_name', userid);


          usr.query();


...


                      newpw = "";


                      var availablechars = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";


                      for(var x = 0; x < 8; x++) {


                            randomNumber = Math.floor(Math.random() * availablechars.length);


                            newpw += availablechars[randomNumber];


                      }


                      usr.user_password.setDisplayValue(newpw);


                      usr.password_needs_reset = true;


                      usr.update();


View solution in original post

9 REPLIES 9

Trevor


Excellent - usr.user_password.setDisplayValue(newpw); solved my issue with encryption.


Thanks!


Will this reset for all the users?  What if I only want to do like 50 users?

Kostya
Tera Guru

Hi, you can try the utility "Password Generator" on ServiceNow Share


It is designed to reset password (one default or random) of multiple users. You can define a filter to find users and setup password and notification options. Can be used for UAT, during release change, within ITOM etc. Tested with >65k users without any problems because of async procedure.


Hit the Thumb Icon and/or mark as Correct, if my answer was correct. So you help others to see correct responses and I get fame 🙂

Cheers,
Kostya

adri
Giga Contributor

Hi!

Do you how it can be installed in our test environment?

I download it and it is just an XML

thanks

balsu333
Mega Expert

Oh great. It worked. Thanks