Skip to main content

Set up a bunch of test users

 via fix script


Users – 10 in total

NAME

USER ID

test USER1

test_user1

test USER2

test_user2

test USER3

test_user3

test USER4

test_user4

test USER5

test_user5

test USER6

test_user6

test USER7

test_user7

test USER8

test_user8

test USER9

test_user9

test USER10

test_user10

Groups – 2 in total

 

TestGrp1

TestGrp2

 

Group memberships

All above users added to each group

 

Group roles – 2 in total

itil

agent_workspace_user

 

Stand alone user roles – 1 in total

 

Catalog (added to all above users)

 

Last login default

 

'2020-01-01';

Last login time default

'2020-01-01 15:47:31';

 

Fix script name “set up test users"

To be run once only

Script code:

 

var lastLogin = '2020-01-01';

var lastLoginTime = '2020-01-01 15:47:31';

setUpUsers(lastLogin, lastLoginTime);

setUpGroups();

AddRolesToGroups();

AddStandaloneRolesToUsers();

AddUsersToGroups();

 

function setUpUsers(lastLogin, lastLoginTime) {

    setUpUser('test', 'TEST1', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST2', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST3', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST4', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST5', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST6', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST7', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST8', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST9', lastLogin, lastLoginTime);

    setUpUser('test', 'TEST10', lastLogin, lastLoginTime);

}

 

function setUpUser(firstName, lastName, lastLogin, lastLoginTime) {

    var grUser = new GlideRecord('sys_user');

    grUser.newRecord();

    grUser.first_name = firstName;

    grUser.last_name = lastName;

    grUser.last_login_time = lastLoginTime;

    grUser.last_login = lastLogin;

//grUser.password.setDisplayValue('xxxxx');

    

var combinedNames = firstName + '_' + lastName;

    grUser.user_name = combinedNames.toLowerCase();

    grUser.insert();

}

 

function setUpGroups() {

    setUpGroup('TestGrp1');

    setUpGroup('TestGrp2');

}

 

function setUpGroup(groupName) {

    var grGroup = new GlideRecord('sys_user_group');

    grGroup.newRecord();

    grGroup.name = groupName;

    grGroup.insert();

}

 

function AddRolesToGroups() {

    AddRoleToGroup('TestGrp1', 'itil');

    AddRoleToGroup('TestGrp1', 'agent_workspace_user');

    AddRoleToGroup('TestGrp2', 'itil');

    AddRoleToGroup('TestGrp2', 'agent_workspace_user');

}

 

function AddRoleToGroup(groupName, groupRole) {

    var grGrpRole = new GlideRecord('sys_group_has_role');

    grGrpRole.newRecord();

    grGrpRole.setDisplayValue('group', groupName);

    grGrpRole.setDisplayValue('role', groupRole);

    grGrpRole.insert();

}

 

function AddStandaloneRolesToUsers() {

    var grUsers = new GlideRecord('sys_user');

    var sQuery = 'last_nameTEST';

    grUsers.addQuery(sQuery);

    grUsers.query();

    while (grUsers.next()) {

        AddStandaloneRoleToUser(grUsers.sys_id, 'catalog');

        AddStandaloneRoleToUser(grUsers.sys_id, 'catalog');

    }

}

 

function AddStandaloneRoleToUser(userSysID, roleName) {

    var grUserRole = new GlideRecord('sys_user_has_role');

    grUserRole.newRecord();

    grUserRole.user = userSysID;

    grUserRole.setDisplayValue('role', roleName);

    grUserRole.insert();

}

 

function AddUsersToGroups() {

    var grUsers = new GlideRecord('sys_user');

    var sQuery = 'last_nameLIKETEST';

    grUsers.addQuery(sQuery);

    grUsers.query();

    while (grUsers.next()) {

        AddUserToGroup(grUsers.sys_id, 'TestGrp1');

        AddUserToGroup(grUsers.sys_id, 'TestGrp2');

    }

}

 

function AddUserToGroup(userSysID, groupName) {

    var grGrpRole = new GlideRecord('sys_user_grmember');

    grGrpRole.newRecord();

    grGrpRole.user = userSysID;

    grGrpRole.setDisplayValue('group', groupName);

    grGrpRole.insert();

}


Comments

Popular posts from this blog

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){...

Code a pause/wait - gs.sleep or gs.wait alternative, pause script for specified seconds (timer)

Code a pause/wait - gs.sleep / gs.wait alternative, pause script for specified seconds (timer)  e.g. 10 seconds: do_sleep ( 10000 ); function do_sleep ( milliseconds ) { var start = new Date (). getTime (); for ( var i = 0 ; i < 1e7 ; i ++) { if (( new Date (). getTime () - start ) > milliseconds ){ gs . print ( 'waking up!' ); break ; } } }