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

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();


:facepalm:



.setDisplayValue() was exactly what was needed.   I'm appalled that it didn't occur to me to try that.



Thanks!


Hi,


          Did it worked for you?? I am trying the same and it is not working for me.   Any help on this??



THanks


Hello. As an admin, I went to the User Administration page, pulled up a user and tried to change his password, but the update failed with these messages:


Invalid update.


Match not found, reset to original  


Thoughts on this?