Unlock the locked user accounts automatically

Abdul13
Tera Contributor

Hi,

The user tries to log in and types the wrong password several times then the user account is locked.
We need to unlock those user accounts automatically after 1 or 2 days if the user is active in the snow.



Regards,
Abdul

1 ACCEPTED SOLUTION

Saurav11
Kilo Patron
Kilo Patron

Hello 

Write a scheduled runs with runs daily and set a specific time. then use the below code:-

var gr = new GlideRecord("sys_user");

gr.addQuery("active", "true");

gr.addQuery("locked_out", "true");

gr.query();

while (gr.next()) {

gr.locked_out=false;

gr.update();

}

Please mark answer correct/helpful based on Impact

View solution in original post

4 REPLIES 4

VigneshMC
Mega Sage

You can write a scheduled job to do this .

Run a scheduled job daily or periodically which checks for locked out and active user accounts based on last bad password attempt field .

Thanks

Saurav11
Kilo Patron
Kilo Patron

Hello 

Write a scheduled runs with runs daily and set a specific time. then use the below code:-

var gr = new GlideRecord("sys_user");

gr.addQuery("active", "true");

gr.addQuery("locked_out", "true");

gr.query();

while (gr.next()) {

gr.locked_out=false;

gr.update();

}

Please mark answer correct/helpful based on Impact

Annay Das
Tera Contributor

Hello,

There is a similar OOTB functionality. Please check this link: Specify lockout for failed login attempts | ServiceNow Docs. It lets you define a time period after which the account will get unlocked automatically, but only if the account was locked out due to failed login attempts.

If this does not meet your requirements, you need to create scheduled job as the other members have commented above.

Going along with Annay, the specific System Policy => Events => Script Action you would look for is "SNC User Lockout Check with Auto Unlock". 

Convert the 1 or 2 days (depending on your decision) to minutes. Then update the first line within "triggerUnlock" function (line getting the glide.user.unlock_timeout_in_mins property).

function triggerUnlock(userSysID) {
	var unlockIn = gs.getProperty("glide.user.unlock_timeout_in_mins", 15);
	var trigger = new GlideRecord("sys_trigger");
	trigger.name = "Unlock the user after "+ unlockIn + " mins";
	trigger.next_action = getTriggerTime(unlockIn);
	trigger.job_id.setDisplayValue('RunScriptJob');
	trigger.script = getTriggerScript(userSysID, gs.nowNoTZ());
	trigger.document = 'sys_user';
	trigger.document_key = userSysID;
	trigger.state = 0;
	trigger.trigger_type = 0;
	trigger.insert();
}