Skip to content

Commit a2e8066

Browse files
TechTrekwithAJ - Check discovery status (#2407)
* Create TechTrekwithAJ-PopulateManufacturer.js This code will help to populate the Manufacturer if that empty on CMDB_CI table * Create TechTrekwithAJ-PopulateManufacturerReadME.md * Delete CMDB/TechTrekwithAJ-PopulateManufacturerReadME.md * Update TechTrekwithAJ-PopulateManufacturer.js 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. * Create TechTrekwithAJ-CheckDiscoveryStatus.js Processes a list of IPs, finds matching CIs in cmdb_ci_computer, checks their discovery history, retrieves the related Discovery Status number, and logs the results as JSON. Logs a message if no CI or history is found. * Create TechTrekwithAJ-CheckDiscoveryStatusReadME.md This script is a ServiceNow troubleshooting utility designed to help administrators quickly check if specific IP addresses were successfully discovered by the Discovery process. * Delete CMDB/TechTrekwithAJ-PopulateManufacturer.js * Rename TechTrekwithAJ-CheckDiscoveryStatusReadME.md to ReadME.md * Rename TechTrekwithAJ-CheckDiscoveryStatus.js to CheckDiscoveryStatus.js
1 parent 64d0268 commit a2e8066

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Configuration //
2+
// Define the list of IP addresses to check
3+
var ipList = [
4+
'192.168.1.35',
5+
'192.168.1.27',
6+
'192.168.1.15'
7+
];
8+
9+
// Main Script //
10+
var results = [];
11+
12+
ipList.forEach(function(ip) {
13+
var result = {
14+
ip_address: ip,
15+
discovery_status_number: ''
16+
};
17+
18+
//Find CI (Computer) with matching IP
19+
var ciGR = new GlideRecord('cmdb_ci_computer');
20+
ciGR.addQuery('ip_address', ip);
21+
ciGR.query();
22+
23+
if (ciGR.next()) {
24+
var ciSysId = ciGR.getUniqueValue();
25+
26+
//Check if CI has an entry in discovery_device_history
27+
var historyGR = new GlideRecord('discovery_device_history');
28+
historyGR.addQuery('ci', ciSysId);
29+
historyGR.orderByDesc('sys_created_on');
30+
historyGR.query();
31+
32+
if (historyGR.next()) {
33+
34+
//Get discovery status number (e.g., DIS123456)
35+
var statusGR = new GlideRecord('discovery_status');
36+
if (statusGR.get(historyGR.discovery_status.toString())) {
37+
result.discovery_status_number = statusGR.number.toString();
38+
} else {
39+
result.discovery_status_number = 'Discovery status record not found';
40+
}
41+
} else {
42+
result.discovery_status_number = 'No discovery record found';
43+
}
44+
} else {
45+
result.discovery_status_number = 'No CI found with this IP';
46+
}
47+
48+
results.push(result);
49+
});
50+
51+
// Output results to system log in JSON format
52+
53+
gs.info('IP to Discovery Status Mapping:\n' + JSON.stringify(results, null, 2));
54+
55+
56+
57+
58+
//Output//
59+
60+
[
61+
{
62+
"ip_address": "192.168.1.35",
63+
"discovery_status_number": "DIS123145"
64+
},
65+
{
66+
"ip_address": "192.168.1.27",
67+
"discovery_status_number": "DIS123189"
68+
},
69+
{
70+
"ip_address": "192.168.1.15",
71+
"discovery_status_number": "No discovery record found"
72+
}
73+
]
74+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Defines a list of IP addresses to check (hardcoded in the script).
2+
Example IPs: 192.168.1.35, 192.168.1.27, 192.168.1.15.
3+
For each IP address in the list:
4+
It looks up the CI (Configuration Item) in the cmdb_ci_computer table with a matching IP.
5+
If a CI is found:
6+
It checks the discovery_device_history table to see if that CI was discovered by ServiceNow Discovery.
7+
If discovery history exists, it finds the related Discovery Status record (discovery_status table) and gets its number (like DIS123456).
8+
If no CI or discovery record is found, it notes the reason in the result.
9+
Compiles all results (IP + discovery status or error message) into a list.
10+
Prints the results in a clear JSON format in the system logs, making it easy to read and review.

0 commit comments

Comments
 (0)