From ad4324f630d36dd730ed5b1ed0700428314dfc26 Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 15:40:38 +0530 Subject: [PATCH 1/8] Create TechTrekwithAJ-PopulateManufacturer.js This code will help to populate the Manufacturer if that empty on CMDB_CI table --- CMDB/TechTrekwithAJ-PopulateManufacturer.js | 46 +++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 CMDB/TechTrekwithAJ-PopulateManufacturer.js diff --git a/CMDB/TechTrekwithAJ-PopulateManufacturer.js b/CMDB/TechTrekwithAJ-PopulateManufacturer.js new file mode 100644 index 0000000000..5fbb598dff --- /dev/null +++ b/CMDB/TechTrekwithAJ-PopulateManufacturer.js @@ -0,0 +1,46 @@ +// Predefined mapping of model names to manufacturer sys_ids or names +var modelManufacturerMap = { + 'ThinkPad T14': 'Lenovo', + 'EliteBook 840': 'HP', + 'Latitude 7420': 'Dell', + 'MacBook Pro 16': 'Apple', + 'Surface Pro 7': 'Microsoft' +}; + +// Function to get the manufacturer sys_id from the manufacturer name +function getManufacturerSysId(name) { + var mfgGR = new GlideRecord('core_company'); + mfgGR.addQuery('name', name); + mfgGR.query(); + if (mfgGR.next()) { + return mfgGR.getUniqueValue(); + } + return null; +} + +// GlideRecord to loop through Configuration Items where manufacturer is empty +var ciGR = new GlideRecord('cmdb_ci'); +ciGR.addNullQuery('manufacturer'); // Manufacturer is empty +ciGR.query(); + +var updatedCount = 0; + +while (ciGR.next()) { + var model = ciGR.model.name.toString(); // Get model name + + if (model && modelManufacturerMap.hasOwnProperty(model)) { + var manufacturerName = modelManufacturerMap[model]; + var manufacturerSysId = getManufacturerSysId(manufacturerName); + + if (manufacturerSysId) { + ciGR.manufacturer = manufacturerSysId; + ciGR.update(); + updatedCount++; + gs.info('Updated CI: ' + ciGR.name + ' | Model: ' + model + ' | Manufacturer: ' + manufacturerName); + } else { + gs.warn('Manufacturer "' + manufacturerName + '" not found in core_company table.'); + } + } +} + +gs.info('Auto-populate complete. Total CIs updated: ' + updatedCount); From 88951259d69802e97cf6e00950b247cc734bbc4e Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 15:43:15 +0530 Subject: [PATCH 2/8] Create TechTrekwithAJ-PopulateManufacturerReadME.md --- ...chTrekwithAJ-PopulateManufacturerReadME.md | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md diff --git a/CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md b/CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md new file mode 100644 index 0000000000..a02a97889d --- /dev/null +++ b/CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md @@ -0,0 +1,23 @@ +This script is used in ServiceNow to automatically fill in the missing manufacturer information for Configuration Items (CIs) in the CMDB (Configuration Management Database). + +1. Predefined Mapping: +The script starts with a list of known model names and their corresponding manufacturer names. +For example, a model called ThinkPad T14 is made by Lenovo, and MacBook Pro 16 is made by Apple + +2. Look Up Manufacturer: + * It defines a function that looks up the manufacturer’s record in the core_company table (based on the name) and gets its unique ID (sys_id). + +3. Find CIs Missing a Manufacturer: + * The script goes through all CIs in the cmdb_ci table where the manufacturer field is empty. + +4. Update Missing Manufacturer: + * For each of those CIs: + * It checks the model name. + * If the model is in the predefined mapping: + * It looks up the correct manufacturer in the core_company table. + * It updates the CI record by setting the manufacturer field with the correct sys_id. + * It also logs that the update was successful. + * If the manufacturer is not found in the system, it logs a warning. + +5. Final Log: + * After going through all matching CIs, it logs how many records were successfully updated. From 176e80dceacd9800398956fd3ce3b3f50e61519e Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 15:52:23 +0530 Subject: [PATCH 3/8] Delete CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md --- ...chTrekwithAJ-PopulateManufacturerReadME.md | 23 ------------------- 1 file changed, 23 deletions(-) delete mode 100644 CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md diff --git a/CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md b/CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md deleted file mode 100644 index a02a97889d..0000000000 --- a/CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md +++ /dev/null @@ -1,23 +0,0 @@ -This script is used in ServiceNow to automatically fill in the missing manufacturer information for Configuration Items (CIs) in the CMDB (Configuration Management Database). - -1. Predefined Mapping: -The script starts with a list of known model names and their corresponding manufacturer names. -For example, a model called ThinkPad T14 is made by Lenovo, and MacBook Pro 16 is made by Apple - -2. Look Up Manufacturer: - * It defines a function that looks up the manufacturer’s record in the core_company table (based on the name) and gets its unique ID (sys_id). - -3. Find CIs Missing a Manufacturer: - * The script goes through all CIs in the cmdb_ci table where the manufacturer field is empty. - -4. Update Missing Manufacturer: - * For each of those CIs: - * It checks the model name. - * If the model is in the predefined mapping: - * It looks up the correct manufacturer in the core_company table. - * It updates the CI record by setting the manufacturer field with the correct sys_id. - * It also logs that the update was successful. - * If the manufacturer is not found in the system, it logs a warning. - -5. Final Log: - * After going through all matching CIs, it logs how many records were successfully updated. From ef7e37e6c416df2e4383da1b6ee615aa05b4d476 Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 16:37:15 +0530 Subject: [PATCH 4/8] Update TechTrekwithAJ-PopulateManufacturer.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This script is used in ServiceNow to automatically fill in the missing manufacturer information for Configuration Items (CIs) in the CMDB (Configuration Management Database). 1. Predefined Mapping: The script starts with a list of known model names and their corresponding manufacturer names.For example, a model called ThinkPad T14 is made by Lenovo, and MacBook Pro 16 is made by Apple 2. Look Up Manufacturer: * It defines a function that looks up the manufacturer’s record in the core_company table (based on the name) and gets its unique ID (sys_id). 3. Find CIs Missing a Manufacturer: * The script goes through all CIs in the cmdb_ci table where the manufacturer field is empty. 4. Update Missing Manufacturer: * For each of those CIs: * It checks the model name. * If the model is in the predefined mapping: * It looks up the correct manufacturer in the core_company table. * It updates the CI record by setting the manufacturer field with the correct sys_id. * It also logs that the update was successful. * If the manufacturer is not found in the system, it logs a warning. 5. Final Log: * After going through all matching CIs, it logs how many records were successfully updated. --- CMDB/TechTrekwithAJ-PopulateManufacturer.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CMDB/TechTrekwithAJ-PopulateManufacturer.js b/CMDB/TechTrekwithAJ-PopulateManufacturer.js index 5fbb598dff..4da3c5d987 100644 --- a/CMDB/TechTrekwithAJ-PopulateManufacturer.js +++ b/CMDB/TechTrekwithAJ-PopulateManufacturer.js @@ -44,3 +44,7 @@ while (ciGR.next()) { } gs.info('Auto-populate complete. Total CIs updated: ' + updatedCount); + + + +//This script is used in ServiceNow to automatically fill in the missing manufacturer information for Configuration Items (CIs) in the CMDB (Configuration Management Database). From 32e947c54d8bb4a6404380d91367b8947d88b9a7 Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 19:21:02 +0530 Subject: [PATCH 5/8] Remove Inactive and locked out users from All Groups and Roles.js MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Removing roles and group access from dormant users is a critical security and compliance best practice — it reduces the attack surface and prevents unauthorized access. --- ...ked out users from All Groups and Roles.js | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Server-Side Components/Scheduled Jobs/Remove Inactive and locked out users from All Groups and Roles.js diff --git a/Server-Side Components/Scheduled Jobs/Remove Inactive and locked out users from All Groups and Roles.js b/Server-Side Components/Scheduled Jobs/Remove Inactive and locked out users from All Groups and Roles.js new file mode 100644 index 0000000000..e5a68320af --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/Remove Inactive and locked out users from All Groups and Roles.js @@ -0,0 +1,44 @@ +// Number of days threshold +var daysThreshold = 90; +var cutoffDate = new GlideDateTime(); +cutoffDate.addDaysUTC(-daysThreshold); + +gs.info('🔍 Starting cleanup of inactive and locked out users since: ' + cutoffDate); + +// Query users inactive, locked out, and not updated in last 90 days +var userGR = new GlideRecord('sys_user'); +userGR.addQuery('active', false); +userGR.addQuery('locked_out', true); +userGR.addQuery('sys_updated_on', '<=', cutoffDate); +userGR.query(); + +var userCount = 0; + +while (userGR.next()) { + var userSysId = userGR.getUniqueValue(); + var userName = userGR.name; + + gs.info('🧹 Cleaning up user: ' + userName + ' (' + userSysId + ')'); + + // Remove from all groups + var groupMemberGR = new GlideRecord('sys_user_grmember'); + groupMemberGR.addQuery('user', userSysId); + groupMemberGR.query(); + while (groupMemberGR.next()) { + gs.info('❌ Removing user from group: ' + groupMemberGR.group.name); + groupMemberGR.deleteRecord(); + } + + // Remove all roles + var roleGR = new GlideRecord('sys_user_has_role'); + roleGR.addQuery('user', userSysId); + roleGR.query(); + while (roleGR.next()) { + gs.info('❌ Removing role: ' + roleGR.role.name); + roleGR.deleteRecord(); + } + + userCount++; +} + +gs.info('✅ Cleanup complete. Total users processed: ' + userCount); From a6c0f70f5a8463441e5384ce7e7469f3bb2190ea Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 19:22:40 +0530 Subject: [PATCH 6/8] Create ReadME.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This script automatically finds and cleans up user accounts that meet all of the following conditions: * The user is inactive * The user is locked out * They haven’t been updated in the last 90 days --- .../Scheduled Jobs/ReadME.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Server-Side Components/Scheduled Jobs/ReadME.md diff --git a/Server-Side Components/Scheduled Jobs/ReadME.md b/Server-Side Components/Scheduled Jobs/ReadME.md new file mode 100644 index 0000000000..2f7b8b172c --- /dev/null +++ b/Server-Side Components/Scheduled Jobs/ReadME.md @@ -0,0 +1,28 @@ +Purpose of the Script: +This script automatically finds and cleans up user accounts that meet all of the following conditions: +* The user is inactive +* The user is locked out +* They haven’t been updated in the last 90 days + +For such users, the script: +* Removes them from all groups +* Deletes all their roles +* Logs everything in the system logs + +Explanation: +1. Set a time threshold (90 days): + * It calculates the date 90 days ago from today. +2. Find target users: + * Searches the sys_user table for users who: + * Are marked as inactive + * Are locked out + * Have not been updated since 90 days ago +3. Loop through each matching user: + * Logs the username being cleaned up +4. Remove user from all groups: + * Searches the sys_user_grmember table (group memberships) + * Deletes all group entries related to the user +5. Remove all roles: + * Searches the sys_user_has_role table + * Deletes all roles assigned to the user +6. Logs the total number of users processed. From b7913711b6fd6700bd92c4cd106a6cf0373e9cca Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 19:23:46 +0530 Subject: [PATCH 7/8] Delete CMDB/TechTrekwithAJ-PopulateManufacturer.js --- CMDB/TechTrekwithAJ-PopulateManufacturer.js | 50 --------------------- 1 file changed, 50 deletions(-) delete mode 100644 CMDB/TechTrekwithAJ-PopulateManufacturer.js diff --git a/CMDB/TechTrekwithAJ-PopulateManufacturer.js b/CMDB/TechTrekwithAJ-PopulateManufacturer.js deleted file mode 100644 index 4da3c5d987..0000000000 --- a/CMDB/TechTrekwithAJ-PopulateManufacturer.js +++ /dev/null @@ -1,50 +0,0 @@ -// Predefined mapping of model names to manufacturer sys_ids or names -var modelManufacturerMap = { - 'ThinkPad T14': 'Lenovo', - 'EliteBook 840': 'HP', - 'Latitude 7420': 'Dell', - 'MacBook Pro 16': 'Apple', - 'Surface Pro 7': 'Microsoft' -}; - -// Function to get the manufacturer sys_id from the manufacturer name -function getManufacturerSysId(name) { - var mfgGR = new GlideRecord('core_company'); - mfgGR.addQuery('name', name); - mfgGR.query(); - if (mfgGR.next()) { - return mfgGR.getUniqueValue(); - } - return null; -} - -// GlideRecord to loop through Configuration Items where manufacturer is empty -var ciGR = new GlideRecord('cmdb_ci'); -ciGR.addNullQuery('manufacturer'); // Manufacturer is empty -ciGR.query(); - -var updatedCount = 0; - -while (ciGR.next()) { - var model = ciGR.model.name.toString(); // Get model name - - if (model && modelManufacturerMap.hasOwnProperty(model)) { - var manufacturerName = modelManufacturerMap[model]; - var manufacturerSysId = getManufacturerSysId(manufacturerName); - - if (manufacturerSysId) { - ciGR.manufacturer = manufacturerSysId; - ciGR.update(); - updatedCount++; - gs.info('Updated CI: ' + ciGR.name + ' | Model: ' + model + ' | Manufacturer: ' + manufacturerName); - } else { - gs.warn('Manufacturer "' + manufacturerName + '" not found in core_company table.'); - } - } -} - -gs.info('Auto-populate complete. Total CIs updated: ' + updatedCount); - - - -//This script is used in ServiceNow to automatically fill in the missing manufacturer information for Configuration Items (CIs) in the CMDB (Configuration Management Database). From 3b370f47a215082690feacf3725482d63f63c820 Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 19:27:43 +0530 Subject: [PATCH 8/8] Update ReadME.md --- Server-Side Components/Scheduled Jobs/ReadME.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Server-Side Components/Scheduled Jobs/ReadME.md b/Server-Side Components/Scheduled Jobs/ReadME.md index 2f7b8b172c..134b6fad28 100644 --- a/Server-Side Components/Scheduled Jobs/ReadME.md +++ b/Server-Side Components/Scheduled Jobs/ReadME.md @@ -26,3 +26,4 @@ Explanation: * Searches the sys_user_has_role table * Deletes all roles assigned to the user 6. Logs the total number of users processed. +7.