Reset local password using background script

annmoleapen
Giga Contributor

Hi there

I am checking if I can reset the local password of a scoped app user. I don't have access to the scoped app, hence it doesn't allow me to reset it manually and that is expected. I tried to run a background script and it also, gave me the same error. Then I set up a scheduled job, which executes the same script, but it runs as system user, though I can see the script is executing as expected (I logged something to check this as well) and user record doesn't get updated.

The next thing I tried was to reset password of a user who doesn't have scoped app privileges. The background script allows me to reset password, however that password doesn't work when I try to login to the user's account.

I used the below script

var x = new GlideRecord('sys_user');
x.addEncodedQuery('nameSTARTSWITHbenjack');
x.query();
while(x.next()){
x.user_password = "test";
x.password_needs_reset = true;
x.update();
gs.log("Password reset");
}

I checked this old community post https://community.servicenow.com/community?id=community_question&sys_id=38df76addb58dbc01dcaf3231f9619be; and looks like it had worked for them. Has anything changed since in terms of encryption on password field?

Now in short my problems are:

1. Password of a normal user which works when I set manually on the user record, doesn't work when I reset through background script

2. Scheduled job, doesn't update the user password or password needs reset field, though the job gets executed

3. Scope app user password, how can I reset their password in case there is an adfs issue without having access to the scoped app?

Please can you help.

Thanks

Ann

1 REPLY 1

sachin_namjoshi
Kilo Patron
Kilo Patron

use below to reset password and update query as per your requirement

 

resetPassword();
function resetPassword() {
var gr = new GlideRecord('sys_user');
gr.addActiveQuery();
gr.query();
var count = gr.getRowCount();
if (count > 0) {
while (gr.next()) {
gr.setDisplayValue("user_password","Password123456");
gr.update();
}
}
}

 

Regards,

Sachin