scheduled_jobs_scripts
Differences
This shows you the differences between two versions of the page.
scheduled_jobs_scripts [12/30/2024 05:40] โ created johnsonjohn | scheduled_jobs_scripts [12/30/2024 06:45] (current) โ johnsonjohn | ||
---|---|---|---|
Line 1: | Line 1: | ||
======Scheduled Scripts====== | ======Scheduled Scripts====== | ||
---- | ---- | ||
+ | |||
+ | ====Application Update Checker==== | ||
+ | < | ||
+ | // JWJ03.14.24 | ||
+ | //run in xplore | ||
+ | ClearUserLog(); | ||
+ | var apps = | ||
+ | new sn_appclient.AppsDataAPI() | ||
+ | .appsData | ||
+ | .getAllAppsWithVersions(false, | ||
+ | |||
+ | apps.forEach(function(app) { | ||
+ | var hasUpdate = | ||
+ | app.version && | ||
+ | app.active && | ||
+ | app.latest_version != app.version && | ||
+ | app.can_install_or_upgrade && | ||
+ | app.isInstalledAndUpdateAvailable; | ||
+ | |||
+ | if (hasUpdate) { | ||
+ | gs.info(app); | ||
+ | // | ||
+ | } | ||
+ | }); | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ====Classify Aging Tickets 2-3-6 Months ==== | ||
+ | < | ||
+ | // JWJ0215 2024 | ||
+ | // view aging tickets 2, 3, and 6 months. | ||
+ | ClearUserLog(); | ||
+ | u_updateIncidentAging(); | ||
+ | |||
+ | function u_updateIncidentAging() { | ||
+ | var elapsedTime = 0; | ||
+ | var aging = ''; | ||
+ | var currentTimeNow = gs.nowDateTime(); | ||
+ | var twoMonthsAgoStart = gs.monthsAgoStart(2); | ||
+ | var threeMonthsAgoStart = gs.monthsAgoStart(3); | ||
+ | var sixMonthsAgoStart = gs.monthsAgoStart(6); | ||
+ | |||
+ | var gr = new GlideRecord(' | ||
+ | gr.addEncodedQuery(' | ||
+ | gr.setLimit(1000); | ||
+ | gr.query(); | ||
+ | while(gr.next()) { | ||
+ | elapsedTime = (gs.dateDiff(gr.opened_at, | ||
+ | |||
+ | // Calculate days ago and assign aging choice | ||
+ | if (elapsedTime <= 14) { | ||
+ | aging = '1-2 Weeks'; | ||
+ | } else if ((elapsedTime > 14) && (elapsedTime <= 28)) { | ||
+ | aging = '3-4 Weeks'; | ||
+ | } else if ((elapsedTime > 28) && (gr.opened_at >= twoMonthsAgoStart)) { | ||
+ | aging = '2 Months'; | ||
+ | } else if ((gr.opened_at < twoMonthsAgoStart) && (gr.opened_at >= threeMonthsAgoStart)) { | ||
+ | aging = '3 Months'; | ||
+ | } else if ((gr.opened_at < threeMonthsAgoStart) && (gr.opened_at >= sixMonthsAgoStart)) { | ||
+ | aging = '4-6 Months'; | ||
+ | } else if (gr.opened_at < sixMonthsAgoStart) { | ||
+ | aging = '6 Months'; | ||
+ | } | ||
+ | |||
+ | gr.setWorkflow(false); | ||
+ | gr.autoSysFields(false); | ||
+ | gr.u_aging_category = aging; | ||
+ | // | ||
+ | UserLog(' | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ====Close Req if RITM is closed ==== | ||
+ | < | ||
+ | // JWJ0215 | ||
+ | // This will close any request that is open ONLY If all RITMs are closed. | ||
+ | // If RITMs are open, the Request will be left open. | ||
+ | closeREQ(); | ||
+ | |||
+ | function closeREQ() { | ||
+ | ClearUserLog(); | ||
+ | var req = new GlideRecord(' | ||
+ | req.addQuery(' | ||
+ | req.query(); | ||
+ | |||
+ | while (req.next()) { | ||
+ | var ritm = new GlideRecord(' | ||
+ | ritm.addQuery(' | ||
+ | ritm.addQuery(' | ||
+ | ritm.query(); | ||
+ | |||
+ | if (ritm.hasNext()) { | ||
+ | //if not closed abort the action | ||
+ | current.setAbortAction(true); | ||
+ | gs.addErrorMessage(" | ||
+ | |||
+ | } else { | ||
+ | |||
+ | req.state = ' | ||
+ | // | ||
+ | UserLog(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ====CMDB Knowledge Related Lists ==== | ||
+ | < | ||
+ | // JWJ0215 4.2.24 | ||
+ | // get all the tables in the cmdb class | ||
+ | ClearUserLog(); | ||
+ | var table = new GlideRecord(" | ||
+ | table.addQuery(" | ||
+ | table.query(); | ||
+ | UserLog(table.name); | ||
+ | |||
+ | while(table.next()){ | ||
+ | //Create a glide record for creating new list entry' | ||
+ | var list_entry = new GlideRecord(" | ||
+ | UserLog(list_entry.number); | ||
+ | |||
+ | // GlideRecord to find the list | ||
+ | var list = new GlideRecord(" | ||
+ | |||
+ | // Find the list | ||
+ | list.addQuery(" | ||
+ | list.query(); | ||
+ | // if there is a list add a new entry | ||
+ | if(list.next()){ | ||
+ | // Create the list entry | ||
+ | list_entry.initialize(); | ||
+ | list_entry.related_list = " | ||
+ | list_entry.list_id = list.sys_id; | ||
+ | list_entry.position = 100; | ||
+ | list_entry.insert(); | ||
+ | UserLog(list_entry.related_list + list_entry.list_id); | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ====Find Record with Given SYS_ID ==== | ||
+ | < | ||
+ | // JWJ0215 2022 | ||
+ | //This script searches all tables for the record identified by a given sys_id. | ||
+ | //It returns the table name where the sys_id was found, and links to the List View and Form View where the record may be found. | ||
+ | |||
+ | //Specify the sys_id you are searching for. | ||
+ | var sysIdToFind = ' | ||
+ | |||
+ | //Invoke the search function. | ||
+ | FindRecGivenSysId(sysIdToFind); | ||
+ | |||
+ | function FindRecGivenSysId(sys_id) { | ||
+ | //Exclude any tables which are causing a problem for our search. | ||
+ | var tablesToExcludeFromSearch = [ | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | ]; | ||
+ | var baseTablesToSearch = new GlideRecord(' | ||
+ | var instanceURI = gs.getProperty(' | ||
+ | var recordFound = false; | ||
+ | var current; | ||
+ | var dict; | ||
+ | |||
+ | // | ||
+ | |||
+ | //Grab base tables, excluding text-indexing, | ||
+ | //Important to know: Records from Child tables will still be found in a search of the base table! | ||
+ | //The function getRecordClassName() can then be used to tell you the specific (child) table the record was found in. | ||
+ | baseTablesToSearch.addNullQuery(" | ||
+ | baseTablesToSearch.addQuery(" | ||
+ | baseTablesToSearch.addQuery(" | ||
+ | baseTablesToSearch.addQuery(" | ||
+ | baseTablesToSearch.query(); | ||
+ | |||
+ | while (baseTablesToSearch.next()) { | ||
+ | // | ||
+ | |||
+ | current = new GlideRecord(baseTablesToSearch.name); | ||
+ | |||
+ | //Find out if there is even a " | ||
+ | dict = new GlideRecord(' | ||
+ | dict.addQuery(' | ||
+ | dict.addQuery(' | ||
+ | dict.queryNoDomain(); | ||
+ | if (!dict.next()) continue; | ||
+ | |||
+ | //Now search for the actual sys_id in the current table. | ||
+ | current.addQuery(' | ||
+ | //Prevent Query Rules from running, if allowed, as these may limit our results. | ||
+ | current.setWorkflow(false); | ||
+ | current.queryNoDomain(); | ||
+ | |||
+ | if (current._next()) { | ||
+ | //We found the actual record by its sys_id value! | ||
+ | recordFound = true; | ||
+ | // | ||
+ | // | ||
+ | |||
+ | var listViewURL = instanceURI + "/ | ||
+ | listViewURL = listViewURL.replace(" | ||
+ | listViewURL = listViewURL.replace(" | ||
+ | var listViewHTML = '<a href="' | ||
+ | |||
+ | var formViewURL = instanceURI + "/ | ||
+ | formViewURL = formViewURL.replace(" | ||
+ | formViewURL = formViewURL.replace(" | ||
+ | var directLinkHTML = '<a href="' | ||
+ | |||
+ | OutputToAll(" | ||
+ | OutputToAll(" | ||
+ | OutputToForm(listViewHTML); | ||
+ | OutputToForm(directLinkHTML); | ||
+ | OutputToLog(" | ||
+ | OutputToLog(" | ||
+ | |||
+ | //We found what we came for. No need to keep searching. | ||
+ | break; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if (!recordFound) { | ||
+ | OutputToAll(" | ||
+ | } | ||
+ | |||
+ | // | ||
+ | } | ||
+ | |||
+ | function OutputToAll(outputString) { | ||
+ | OutputToForm(outputString); | ||
+ | OutputToLog(outputString); | ||
+ | } | ||
+ | |||
+ | function OutputToForm(outputString) { | ||
+ | gs.addInfoMessage(outputString); | ||
+ | } | ||
+ | |||
+ | function OutputToLog(outputString) { | ||
+ | //Log Prefix makes it much easier to find the statements we care about in the system log (table syslog). | ||
+ | var logPrefix = " | ||
+ | gs.print(logPrefix + outputString); | ||
+ | gs.log(logPrefix + outputString); | ||
+ | } | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ====Inactive Accounts ==== | ||
+ | < | ||
+ | // JWJ This will disable inactive accounts and send an email to admins of the group is-applications-servicenow | ||
+ | // line 8 sets the time of inactivity. Also, this only finds active users. | ||
+ | disable_users(); | ||
+ | |||
+ | function disable_users() { | ||
+ | ClearUserLog(); | ||
+ | var gr = new GlideRecord(" | ||
+ | gr.addQuery(' | ||
+ | gr.addQuery(' | ||
+ | gr.addQuery(' | ||
+ | gr.query(); | ||
+ | var DeactiveCount = gr.getRowCount(); | ||
+ | gs.log(" | ||
+ | UserLog(" | ||
+ | //limit the deactivation to 500 users per day | ||
+ | if (DeactiveCount <1600) { | ||
+ | while (gr.next()) { | ||
+ | gr.active = false; | ||
+ | gs.log(" | ||
+ | // | ||
+ | } | ||
+ | gs.log(" | ||
+ | } else { | ||
+ | var ga = new GlideRecord(' | ||
+ | ga.addQuery(' | ||
+ | ga.query(); | ||
+ | while (ga.next()) { | ||
+ | var gadminuser = new GlideRecord(" | ||
+ | gadminuser.addQuery(" | ||
+ | gadminuser.query(); | ||
+ | while (gadminuser.next()) { | ||
+ | var cmdbci = new GlideRecord(" | ||
+ | cmdbci.addQuery(" | ||
+ | cmdbci.query(); | ||
+ | while (cmdbci.next()) { | ||
+ | var gi = new GlideRecord(" | ||
+ | gi.initialize(); | ||
+ | gi.caller_id = gadminuser.sys_id; | ||
+ | gi.short_description = "Error in Scheduled Job: ' | ||
+ | gi.description = "There are more than 500 users discovered to be marked inactive. Please investigate and confirm the LDAP sync is working properly. If there are 500 records either increase the deactivation limit or manually run the script without the limit."; | ||
+ | gi.impact = ' | ||
+ | gi.urgency = ' | ||
+ | gi.category = "IS Processes"; | ||
+ | gi.subcategory = " | ||
+ | gi.assignment_group = ga.sys_id; | ||
+ | gi.cmdb_ci = cmdbci.sys_id; | ||
+ | gi.insert(); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ====Knowledge in Draft Mode ==== | ||
+ | < | ||
+ | // JWJ0215 2022 | ||
+ | ClearUserLog(); | ||
+ | var gr = new GlideRecord(' | ||
+ | gr.addQuery(' | ||
+ | gr.query(); | ||
+ | while(gr.next()) { | ||
+ | | ||
+ | // | ||
+ | UserLog(gr.number); | ||
+ | } | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ====MHCure Close REQ-RITM ==== | ||
+ | < | ||
+ | // JWJ0215 06/07/22 | ||
+ | // var SOURCE_REQUESTS = [' | ||
+ | // This will close the request, requested item, and the task of MH Cure Requests. | ||
+ | // put all req number in lone 5 | ||
+ | ClearUserLog(); | ||
+ | var SOURCE_REQUESTS = [' | ||
+ | |||
+ | var requestGR = new GlideRecord(' | ||
+ | requestGR.addQuery(' | ||
+ | requestGR.setValue(' | ||
+ | requestGR.setValue(' | ||
+ | requestGR.setValue(' | ||
+ | requestGR.setValue(' | ||
+ | requestGR.setWorkflow(false); | ||
+ | requestGR.updateMultiple(); | ||
+ | |||
+ | var requestItemGR = new GlideRecord(' | ||
+ | requestItemGR.addQuery(' | ||
+ | requestItemGR.addActiveQuery(); | ||
+ | requestItemGR.setValue(' | ||
+ | requestItemGR.setValue(' | ||
+ | requestItemGR.setWorkflow(false); | ||
+ | requestItemGR.updateMultiple(); | ||
+ | |||
+ | var taskGR = new GlideRecord(' | ||
+ | taskGR.addQuery(' | ||
+ | taskGR.addActiveQuery(); | ||
+ | taskGR.setValue(' | ||
+ | taskGR.setWorkflow(false); | ||
+ | taskGR.updateMultiple(); | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ====Scheduled Data Collector Killer ==== | ||
+ | < | ||
+ | //Cancel all Scheduled Data Collectors -Set to On Demand | ||
+ | // JWJ0215 07.02.24 | ||
+ | ClearUserLog(); | ||
+ | gs.setSession.setStrictQuery(true); | ||
+ | var dcollector = new GlideRecord (' | ||
+ | dcollector.addEncodedQuery(active=true); | ||
+ | dcollector.addEncodedQuery(run=' | ||
+ | dcollector.query(); | ||
+ | UserLog(" | ||
+ | while (dcollector.next()){ | ||
+ | UserLog(" | ||
+ | dcollector.active = ' | ||
+ | dcollector.run = 'On Demand'; | ||
+ | dcollector.update(); | ||
+ | UserLog(' | ||
+ | } | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ====Time and Date Update ==== | ||
+ | < | ||
+ | // JWJ0215 12.5.24 | ||
+ | // Set the time and date properties | ||
+ | ClearUserLog(); | ||
+ | gs.setProperty(" | ||
+ | gs.setProperty(" | ||
+ | UserLog(" | ||
+ | UserLog(" | ||
+ | </ | ||
+ | ---- | ||
+ | |||
+ | ====Update Knowledge to Published ==== | ||
+ | < | ||
+ | // JWJ This will find knowledge articles in the internal knowledgebase that are in draft mode | ||
+ | // and set them to published. | ||
+ | |||
+ | ClearUserLog(); | ||
+ | var updateWorkflow = new GlideRecord(' | ||
+ | updateWorkflow.addQuery(' | ||
+ | updateWorkflow.addEncodedQuery(' | ||
+ | updateWorkflow.query(); | ||
+ | while (updateWorkflow.next()) { | ||
+ | updateWorkflow.workflow_state = ' | ||
+ | UserLog(updateWorkflow.number); | ||
+ | // | ||
+ | } | ||
+ | </ | ||
+ | ---- | ||
+ | |||
=====Project Insight Update===== | =====Project Insight Update===== |
scheduled_jobs_scripts.1735566021.txt.gz ยท Last modified: 12/30/2024 05:40 by johnsonjohn