User Tools

Site Tools


cleanup_scripts

Cleanup Scripts

Post Clone Cleanup Script

//JWJ0215 6.14.24
//Upload to PROD instance by navigating to system definition>upload file
//This file will LIVE in PROD so it will copy down when cloned.  Do NOT RUN THIS IN PROD !!
// DO NOT MAKE THIS ACTIVE IN PRODUCTION
// *****    DEV begins on line 34       TEST begins on line 195       SAND begins on line 337   *****
//define the instance names here
TimestampLog("START POST CLONE Cleanup Script - Post_CLONE_Scripts");
var devInstance = "<instance>dev.service-now.com";
var devEmailRedirect = "<instance>DEV@thedacare.org";
var testInstance = "<instance>test.service-now.com";
var testEmailRedirect = "<instance>TEST@thedacare.org";
var sandInstance = "<instance>sand.service-now.com";
var sandEmailRedirect = "<instance>NowSAND@thedacare.org";
var prodInstance = "<instance>.service-now.com";
 
//get this instance's name
var thisInstance = gs.getProperty("instance_name");
switch (thisInstance) {
 case devInstance:
  devConfig();
  break;
 case testInstance: 
  testConfig();
  break;
  case sandInstance:
  sandConfig();
  break;
 case prodInstance: 
  gs.print("**** You're running this script in production, are you asking for trouble?");
  break;
 default: 
  gs.print("**** I don't understand what this instance is for: " + thisInstance);
}
 // ********************************************************************************************************* START DEV
function devConfig() {
 //set the base colors
 gs.setProperty("css.$nav-highlight-bar-active","#ffd600");
 gs.setProperty("css.$nav-highlight-bar-inactive","#ffd600");
 gs.setProperty("css.$nav-hr-color","#ffd600");
 gs.setProperty("css.$navpage-header-bg","#80bc00 ");
 gs.setProperty("css.$navpage-header-color","#fffff  ");
 gs.setProperty("css.$navpage-header-divider-color","");
 gs.setProperty("css.$navpage-nav-bg","#80bc00 ");
 gs.setProperty("css.$navpage-nav-bg-sub","#80bc00 ");
 gs.setProperty("css.$navpage-nav-border","#fffff  ");
 gs.setProperty("css.$navpage-nav-color-sub","#323e48");
 gs.setProperty("css.$navpage-nav-selected-bg","#ffd600");
 gs.setProperty("css.$navpage-nav-selected-color","#80bc00 ");
 gs.setProperty("css.$navpage-nav-unselected-color","#fffff  ");
 gs.setProperty("css.$subnav-background-color","#ffd600");

 ClearUserLog();
 
 // Query the Group Members [sys_user_grmember] table to find members of the specified group
var groupSysId = '9360165397e886103e1432890353af54'; // test_impersonator group  
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('group', groupSysId);
grMember.query();
// Log the user's Sys ID being removed - for audit purposes 
while (grMember.next()) {
    UserLog('Post Upgrade 1: Removing user with Sys ID: ' + grMember.user + ' from group: ' + groupSysId); // was gs.info
    // Delete the group member record
    grMember.deleteRecord();}
gs.info('Post Upgrade 1 complete:  -All members have been removed from the group, test_impersonator, with Sys ID: ' + groupSysId); // was gs.info
 
 
  
 //EMAIL OUTBOUND ON-Inbound OFF
 gs.setProperty("glide.email.read.active",false); //inbound
 gs.setProperty("glide.email.smtp.active",true);  //outbound
 

//Cancel all Scheduled Data Collectors -Set to On Demand
// JWJ0215 07.02.24

gs.setSession.setStrictQuery(true);
var dcollector = new GlideRecord ('sysauto_pa');
dcollector.addEncodedQuery(active=true);
dcollector.addEncodedQuery(run='On Demand');
dcollector.query();
UserLog("found " + dcollector.getRowCount() + " data collectors to update.");
while (dcollector.next()){
	UserLog("changing " + dcollector.sys_name + " records to active=false.");
	dcollector.active = 'false';
	dcollector.run = 'On Demand';
	dcollector.update();
	UserLog('Data Colectors set to Active=false ' + dcollector.sys_name);
}


//Cancel all Scheduled Reports
gs.setSession.setStrictQuery(true);
var schedreport = new GlideRecord ('sysauto_report');
schedreport.addEncodedQuery(active=true);
schedreport.query();
UserLog("found " + schedreport.getRowCount() + " reports to update.");
while (schedreport.next()){
	UserLog("changing " + schedreport + " records to active-false.");
	schedreport.active = 'false';
	schedreport.update();
	UserLog('Post Upgrade 3 complete' + schedreport);
}

//Cancel CMDB Schedules
gs.setSession.setStrictQuery(true);
var schedcmdb = new GlideRecord ('cert_task');
schedcmdb.addEncodedQuery(active=true);
schedcmdb.query();
UserLog("found " + schedcmdb.getRowCount() + " reports to update.");
while (schedcmdb.next()){
	UserLog("changing " + schedcmdb + " records to active-false.");
	schedcmdb.active = 'false';
	schedcmdb.update();
	UserLog('Post Upgrade 4 complete: ' + schedcmdb);
}

//Cancel CERT Schedules
gs.setSession.setStrictQuery(true);
var schedcert = new GlideRecord ('cert_schedule');
schedcert.addEncodedQuery('nameISNOTEMPTY^run_type!=on_demand');
schedcert.query();
UserLog("found " + schedcert.getRowCount() + " reports to update.");
while (schedcert.next()){
	UserLog("changing " + schedcert + " records to ON Demand.");
	schedcert.run_type = 'on_demand';
	schedcert.update();
	UserLog('Post Upgrade 5 complete: ' + schedcert);
}

//Cancel Scheduled Data Jobs
gs.setSession.setStrictQuery(true);
var schedreport = new GlideRecord ('sysauto_report');
schedreport.addEncodedQuery(active=true);
schedreport.query();
UserLog("found " + schedreport.getRowCount() + " reports to update.");
while (schedreport.next()){
	UserLog("changing " + schedreport + " records to active-false.");
	schedreport.active = 'false';
	schedreport.update();
	UserLog('Post Upgrade 6 complete: ' + schedreport);
}

//Cancel all Data Management Tasks
gs.setSession.setStrictQuery(true);
// --use a GlideRecord against the data management table  ****  our variable is 'grcmdbtask'
var grcmdbtask = new GlideRecord('cmdb_data_management_task');
// --create the query
grcmdbtask.addEncodedQuery('state=1^stateNOT IN3,4,7,8');
// --call the query
grcmdbtask.query();
UserLog("2. found " + grcmdbtask.getRowCount() + " records to update.", "DATA-MANAGEMENT");
while (grcmdbtask.next()) {
    // --flip the state over
    UserLog("3. changing: " + grcmdbtask.number + " to CANCELLED.", "DATA-MANAGEMENT");
     //grcmdbtask.getValue('cmdb_data_management_task');
     //grcmdbtask.getValue(grcmdbtask.sys_id);
    //UserLog(grcmdbtask.number);
    grcmdbtask.state = 'cancelled';
    grcmdbtask.update();
}

//Sam Killer
gs.setSession.setStrictQuery(true);
var skreport = new GlideRecord ('sys_trigger');      //name var
skreport.addEncodedQuery("nameLIKESAM -^trigger_typeISNOTEMPTY");     //add encoded filter
skreport.query();
UserLog("found " + skreport.getRowCount() + " records to update.");     //log messge
while (skreport.next()){     // set while with gr
	UserLog("changing " + skreport + " records to disabled.");     //log message
	skreport.trigger_type='2';  //set 
	skreport.update();  //update
	UserLog('Post Upgrade 8 complete: ' + skreport);  //log
}

// JWJ0215 12.5.24
// Set the time and date properties   ***********************************************************   added 12/5/24   **********************************
gs.setProperty("glide.sys.time_format","hh:mm a");
gs.setProperty("glide.sys.date_format","MM/dd/yyyy");
UserLog("Time has been set to hh:mm a.");
UserLog("Date has been set to MM/dd/yyyy.");

 //disable all LDAP servers
 var ldap = new GlideRecord("ldap_server_config");
 ldap.query();
 while (ldap.next()) {
  active = false;
  ldap.update();
 }

 //set header name
 gs.setProperty("glide.product.description","Service Management-DEVELOPMENT");
 gs.print("Applied Dev Configurations");
}
//************************************************************************************************************* TEST CONFIG
function testConfig() {
 //set the base colors
 gs.setProperty("css.$nav-highlight-bar-active","#ffa400");
 gs.setProperty("css.$nav-highlight-bar-inactive","#ffa400");
 gs.setProperty("css.$nav-hr-color","#ffa400");
 gs.setProperty("css.$navpage-header-bg","#c028b9");
 gs.setProperty("css.$navpage-header-color","#fffff");
 gs.setProperty("css.$navpage-header-divider-color","#ffa400");
 gs.setProperty("css.$navpage-nav-bg","#c028b9");
 gs.setProperty("css.$navpage-nav-bg-sub","#c028b9");
 gs.setProperty("css.$navpage-nav-border","#fffff");
 gs.setProperty("css.$navpage-nav-selected-bg","#f5333f");
 gs.setProperty("css.$navpage-nav-selected-color","");
 gs.setProperty("css.$navpage-nav-unselected-color","#fffff");
 gs.setProperty("css.$subnav-background-color","#f5333f");

 ClearUserLog();
 
 // Query the Group Members [sys_user_grmember] table to find members of the specified group
var groupSysId = '9360165397e886103e1432890353af54'; // test_impersonator group  
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('group', groupSysId);
grMember.query();
// Log the user's Sys ID being removed - for audit purposes 
while (grMember.next()) {
    UserLog('Post Upgrade 1: Removing user with Sys ID: ' + grMember.user + ' from group: ' + groupSysId); // was gs.info
    // Delete the group member record
    grMember.deleteRecord();}
gs.info('Post Upgrade 1 complete:  -All members have been removed from the group, test_impersonator, with Sys ID: ' + groupSysId); // was gs.info
 
 
  
 //EMAIL OUTBOUND ON-Inbound OFF
 gs.setProperty("glide.email.read.active",false); //inbound
 gs.setProperty("glide.email.smtp.active",true);  //outbound
 

//Cancel all Scheduled Reports
gs.setSession.setStrictQuery(true);
var schedreport = new GlideRecord ('sysauto_report');
schedreport.addEncodedQuery(active=true);
schedreport.query();
UserLog("found " + schedreport.getRowCount() + " reports to update.");
while (schedreport.next()){
	UserLog("changing " + schedreport + " records to active-false.");
	schedreport.active = 'false';
	schedreport.update();
	UserLog('Post Upgrade 3 complete' + schedreport);
}

//Cance CMDB Schedules
gs.setSession.setStrictQuery(true);
var schedcmdb = new GlideRecord ('cert_task');
schedcmdb.addEncodedQuery(active=true);
schedcmdb.query();
UserLog("found " + schedcmdb.getRowCount() + " reports to update.");
while (schedcmdb.next()){
	UserLog("changing " + schedcmdb + " records to active-false.");
	schedcmdb.active = 'false';
	schedcmdb.update();
	UserLog('Post Upgrade 4 complete: ' + schedcmdb);
}

//Cancel CERT Schedules
gs.setSession.setStrictQuery(true);
var schedcert = new GlideRecord ('cert_schedule');
schedcert.addEncodedQuery('nameISNOTEMPTY^run_type!=on_demand');
schedcert.query();
UserLog("found " + schedcert.getRowCount() + " reports to update.");
while (schedcert.next()){
	UserLog("changing " + schedcert + " records to ON Demand.");
	schedcert.run_type = 'on_demand';
	schedcert.update();
	UserLog('Post Upgrade 5 complete: ' + schedcert);
}

//Cancel Scheduled Data Jobs
gs.setSession.setStrictQuery(true);
var schedreport = new GlideRecord ('sysauto_report');
schedreport.addEncodedQuery(active=true);
schedreport.query();
UserLog("found " + schedreport.getRowCount() + " reports to update.");
while (schedreport.next()){
	UserLog("changing " + schedreport + " records to active-false.");
	schedreport.active = 'false';
	schedreport.update();
	UserLog('Post Upgrade 6 complete: ' + schedreport);
}

//Cancel all Data Management Tasks
gs.setSession.setStrictQuery(true);
// --use a GlideRecord against the data management table  ****  our variable is 'grcmdbtask'
var grcmdbtask = new GlideRecord('cmdb_data_management_task');
// --create the query
grcmdbtask.addEncodedQuery('state=1^stateNOT IN3,4,7,8');
// --call the query
grcmdbtask.query();
UserLog("2. found " + grcmdbtask.getRowCount() + " records to update.", "DATA-MANAGEMENT");
while (grcmdbtask.next()) {
    // --flip the state over
    UserLog("3. changing: " + grcmdbtask.number + " to CANCELLED.", "DATA-MANAGEMENT");
     //grcmdbtask.getValue('cmdb_data_management_task');
     //grcmdbtask.getValue(grcmdbtask.sys_id);
    //UserLog(grcmdbtask.number);
    grcmdbtask.state = 'cancelled';
    grcmdbtask.update();
}

//Sam Killer
gs.setSession.setStrictQuery(true);
var skreport = new GlideRecord ('sys_trigger');      //name var
skreport.addEncodedQuery("nameLIKESAM -^trigger_typeISNOTEMPTY");     //add encoded filter
skreport.query();
UserLog("found " + skreport.getRowCount() + " records to update.");     //log messge
while (skreport.next()){     // set while with gr
	UserLog("changing " + skreport + " records to disabled.");     //log message
	skreport.trigger_type='2';  //set 
	skreport.update();  //update
	UserLog('Post Upgrade 8 complete: ' + skreport);  //log
}


// JWJ0215 12.5.24
// Set the time and date properties   ***********************************************************   added 12/5/24   **********************************
gs.setProperty("glide.sys.time_format","hh:mm a");
gs.setProperty("glide.sys.date_format","MM/dd/yyyy");
UserLog("Time has been set to hh:mm a.");
UserLog("Date has been set to MM/dd/yyyy.");

 //disable all LDAP servers
 var ldap = new GlideRecord("ldap_server_config");
 ldap.query();
 while (ldap.next()) {
  active = false;
  ldap.update();
 }

 //set header name
 gs.setProperty("glide.product.description","Service Management-TEST");
 gs.print("Applied TEST Configurations");
}
//***********************************************************************************************************SAND CONFIG
function sandConfig() {
//set the base colors
 gs.setProperty("css.$nav-highlight-bar-active","#ffd600");
 gs.setProperty("css.$nav-highlight-bar-inactive","#ffd600");
 gs.setProperty("css.$nav-hr-color","#ffd600");
 gs.setProperty("css.$navpage-header-bg","#80bc00");
 gs.setProperty("css.$navpage-header-color","#fffff");
 gs.setProperty("css.$navpage-header-divider-color","#ffd600");
 gs.setProperty("css.$navpage-nav-bg","#80bc00");
 gs.setProperty("css.$navpage-nav-bg-sub","#80bc00");
 gs.setProperty("css.$navpage-nav-border","#fffff");
 gs.setProperty("css.$navpage-nav-color-sub","#323e48");
 gs.setProperty("css.$navpage-nav-selected-bg","#ffd600");
 gs.setProperty("css.$navpage-nav-selected-color","#80bc00");
 gs.setProperty("css.$navpage-nav-unselected-color","#fffff");
 gs.setProperty("css.$subnav-background-color","#ffd600");

 ClearUserLog();
 
 // Query the Group Members [sys_user_grmember] table to find members of the specified group
var groupSysId = '9360165397e886103e1432890353af54'; // test_impersonator group  
var grMember = new GlideRecord('sys_user_grmember');
grMember.addQuery('group', groupSysId);
grMember.query();
// Log the user's Sys ID being removed - for audit purposes 
while (grMember.next()) {
    UserLog('Post Upgrade 1: Removing user with Sys ID: ' + grMember.user + ' from group: ' + groupSysId); // was gs.info
    // Delete the group member record
    grMember.deleteRecord();}
gs.info('Post Upgrade 1 complete:  -All members have been removed from the group, test_impersonator, with Sys ID: ' + groupSysId); // was gs.info
 
 
  
 //EMAIL OUTBOUND ON-Inbound OFF
 gs.setProperty("glide.email.read.active",true); //inbound
 gs.setProperty("glide.email.smtp.active",false);  //outbound
 

//Cancel all Scheduled Reports
gs.setSession.setStrictQuery(true);
var schedreport = new GlideRecord ('sysauto_report');
schedreport.addEncodedQuery(active=true);
schedreport.query();
UserLog("found " + schedreport.getRowCount() + " reports to update.");
while (schedreport.next()){
	UserLog("changing " + schedreport + " records to active-false.");
	schedreport.active = 'false';
	schedreport.update();
	UserLog('Post Upgrade 3 complete' + schedreport);
}

//Cance CMDB Schedules
gs.setSession.setStrictQuery(true);
var schedcmdb = new GlideRecord ('cert_task');
schedcmdb.addEncodedQuery(active=true);
schedcmdb.query();
UserLog("found " + schedcmdb.getRowCount() + " reports to update.");
while (schedcmdb.next()){
	UserLog("changing " + schedcmdb + " records to active-false.");
	schedcmdb.active = 'false';
	schedcmdb.update();
	UserLog('Post Upgrade 4 complete: ' + schedcmdb);
}

//Cancel CERT Schedules
gs.setSession.setStrictQuery(true);
var schedcert = new GlideRecord ('cert_schedule');
schedcert.addEncodedQuery('nameISNOTEMPTY^run_type!=on_demand');
schedcert.query();
UserLog("found " + schedcert.getRowCount() + " reports to update.");
while (schedcert.next()){
	UserLog("changing " + schedcert + " records to ON Demand.");
	schedcert.run_type = 'on_demand';
	schedcert.update();
	UserLog('Post Upgrade 5 complete: ' + schedcert);
}

//Cancel Scheduled Data Jobs
gs.setSession.setStrictQuery(true);
var schedreport = new GlideRecord ('sysauto_report');
schedreport.addEncodedQuery(active=true);
schedreport.query();
UserLog("found " + schedreport.getRowCount() + " reports to update.");
while (schedreport.next()){
	UserLog("changing " + schedreport + " records to active-false.");
	schedreport.active = 'false';
	schedreport.update();
	UserLog('Post Upgrade 6 complete: ' + schedreport);
}

//Cancel all Data Management Tasks
gs.setSession.setStrictQuery(true);
// --use a GlideRecord against the data management table  ****  our variable is 'grcmdbtask'
var grcmdbtask = new GlideRecord('cmdb_data_management_task');
// --create the query
grcmdbtask.addEncodedQuery('state=1^stateNOT IN3,4,7,8');
// --call the query
grcmdbtask.query();
UserLog("2. found " + grcmdbtask.getRowCount() + " records to update.", "DATA-MANAGEMENT");
while (grcmdbtask.next()) {
    // --flip the state over
    UserLog("3. changing: " + grcmdbtask.number + " to CANCELLED.", "DATA-MANAGEMENT");
     //grcmdbtask.getValue('cmdb_data_management_task');
     //grcmdbtask.getValue(grcmdbtask.sys_id);
    //UserLog(grcmdbtask.number);
    grcmdbtask.state = 'cancelled';
    grcmdbtask.update();
}

//Sam Killer
gs.setSession.setStrictQuery(true);
var skreport = new GlideRecord ('sys_trigger');      //name var
skreport.addEncodedQuery("nameLIKESAM -^trigger_typeISNOTEMPTY");     //add encoded filter
skreport.query();
UserLog("found " + skreport.getRowCount() + " records to update.");     //log messge
while (skreport.next()){     // set while with gr
	UserLog("changing " + skreport + " records to disabled.");     //log message
	skreport.trigger_type='2';  //set 
	skreport.update();  //update
	UserLog('Post Upgrade 8 complete: ' + skreport);  //log
}


// JWJ0215 12.5.24
// Set the time and date properties   ***********************************************************   added 12/5/24   **********************************
gs.setProperty("glide.sys.time_format","hh:mm a");
gs.setProperty("glide.sys.date_format","MM/dd/yyyy");
UserLog("Time has been set to hh:mm a.");
UserLog("Date has been set to MM/dd/yyyy.");

 //disable all LDAP servers
 var ldap = new GlideRecord("ldap_server_config");
 ldap.query();
 while (ldap.next()) {
  active = false;
  ldap.update();
 }

 //set header name
 gs.setProperty("glide.product.description","Service Management-SAND");
 gs.print("Applied SAND Configurations");
}

cleanup_scripts.txt · Last modified: 2024/12/30 05:39 by johnsonjohn