From ad4324f630d36dd730ed5b1ed0700428314dfc26 Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 15:40:38 +0530 Subject: [PATCH 1/6] 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/6] 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/6] 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 20e3d3178fcb15f0fb714183537406a0edf90879 Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 16:10:31 +0530 Subject: [PATCH 4/6] Create TechTrekwithAJ-Automate Discovery using flow Script Step.js This script is used in ServiceNow to automate device discovery on a network by triggering Quick Discovery using a MID Server. It is typically used within a workflow or flow, where you want to scan a device using its IP address. It adds reliability by using two MID servers, so if one fails, the other is used as a backup. --- ...tomate Discovery using flow Script Step.js | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 ITOM/Discovery/TechTrekwithAJ-Automate Discovery using flow Script Step.js diff --git a/ITOM/Discovery/TechTrekwithAJ-Automate Discovery using flow Script Step.js b/ITOM/Discovery/TechTrekwithAJ-Automate Discovery using flow Script Step.js new file mode 100644 index 0000000000..d28dbf12ec --- /dev/null +++ b/ITOM/Discovery/TechTrekwithAJ-Automate Discovery using flow Script Step.js @@ -0,0 +1,59 @@ +//Place inside a Flow Script Step or a Workflow Script +//Ensure current has a field like ip_address and discovery_status (create custom fields if needed) +//You can also pass ip_address through inputs if not part of the current record +//Replace mid_server_1 and mid_server_2 with actual MID Server names or sys_ids +//You can customize status values or extend it to update logs/audit tables + +//This script is used in ServiceNow to automate device discovery on a network by triggering Quick Discovery using a MID Server. It is typically used within a workflow or flow, where you want to scan a device using its IP address. +//It adds reliability by using two MID servers, so if one fails, the other is used as a backup. + +(function execute(inputs, outputs) { + //Define two MID servers for redundancy + var midServer1 = 'mid_server_1'; // Replace with actual MID server name or sys_id + var midServer2 = 'mid_server_2'; // Replace with actual MID server name or sys_id + + //Get IP address from current context (usually passed in workflow or flow) + var ipAddress = current.ip_address || inputs.ip_address; + + if (!ipAddress) { + gs.error('No IP address provided for discovery.'); + current.discovery_status = 'Failed - No IP address'; + current.update(); + return; + } + + gs.info('Starting Quick Discovery for IP: ' + ipAddress); + + //Function to trigger Quick Discovery + function triggerQuickDiscovery(ip, mid) { + var discovery = new Discovery(); + return discovery.quickDiscovery(ip, mid); + } + + //Attempt discovery with first MID server + var status1 = triggerQuickDiscovery(ipAddress, midServer1); + + if (status1) { + gs.info('Discovery triggered successfully using MID Server 1: ' + midServer1); + current.discovery_status = 'Triggered via MID Server 1'; + } else { + gs.warn('Discovery failed with MID Server 1. Trying MID Server 2...'); + + var status2 = triggerQuickDiscovery(ipAddress, midServer2); + + if (status2) { + gs.info('Discovery triggered successfully using MID Server 2: ' + midServer2); + current.discovery_status = 'Triggered via MID Server 2'; + } else { + gs.error('Discovery failed with both MID servers.'); + current.discovery_status = 'Failed - Both MID Servers'; + } + } + + //Update context with final discovery status + current.update(); + +})(inputs, outputs); + + + From aac7a271b1591a73a33be31dde6382fe22c36be3 Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 16:16:59 +0530 Subject: [PATCH 5/6] Delete CMDB/TechTrekwithAJ-PopulateManufacturer.js --- CMDB/TechTrekwithAJ-PopulateManufacturer.js | 46 --------------------- 1 file changed, 46 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 5fbb598dff..0000000000 --- a/CMDB/TechTrekwithAJ-PopulateManufacturer.js +++ /dev/null @@ -1,46 +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); From 1a5869a672a9462f40b13750453c718ec63904be Mon Sep 17 00:00:00 2001 From: Ajay Kumar Date: Wed, 22 Oct 2025 16:19:52 +0530 Subject: [PATCH 6/6] Create TechTrekwithAJ-Automate Discovery using flow Script Step ReadME.md --- ...mate Discovery using flow Script Step ReadME.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 ITOM/Discovery/TechTrekwithAJ-Automate Discovery using flow Script Step ReadME.md diff --git a/ITOM/Discovery/TechTrekwithAJ-Automate Discovery using flow Script Step ReadME.md b/ITOM/Discovery/TechTrekwithAJ-Automate Discovery using flow Script Step ReadME.md new file mode 100644 index 0000000000..7fe091c680 --- /dev/null +++ b/ITOM/Discovery/TechTrekwithAJ-Automate Discovery using flow Script Step ReadME.md @@ -0,0 +1,14 @@ +This script is used in ServiceNow to automate device discovery on a network by triggering Quick Discovery using a MID Server. It is typically used within a workflow or flow, where you want to scan a device using its IP address. +It adds reliability by using two MID servers, so if one fails, the other is used as a backup. +These are the two MID Servers that will be used to perform the discovery. +You need to replace 'mid_server_1' and 'mid_server_2' with real MID Server names or sys_ids. +This gives you a backup option if the first one fails. +The script tries to get the IP address from the current record (current.ip_address). +If that’s not available, it tries to use inputs.ip_address (passed into the script). +If no IP address is found, it logs an error and stops. +This is a helper function that tells ServiceNow to start Quick Discovery for a specific IP address using a specific MID Server. +It first tries to start discovery using the first MID server. +If it works, it logs a success message and updates the record to show that discovery was triggered using MID Server 1. +If the first attempt fails, it logs a warning and tries again with the second MID server. +If the second attempt also fails, it logs an error and updates the record to reflect that both attempts failed. +The script updates the current record with the result of the discovery attempt (success or failure and which MID server was used).