Skip to content

Commit f2f0a2c

Browse files
Create discovery_ip_status.js
This script maps a list of IP addresses to their corresponding Discovery Status in ServiceNow. It checks each IP in the CMDB for a matching CI record, retrieves the related discovery status from the discovery_device_history table, and returns a structured list showing which discovery status each IP belongs to.
1 parent e977bed commit f2f0a2c

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Array of IP addresses to check
2+
var ipAddressLists = ['192.168.1.35', '192.168.1.27', '192.168.1.15'];
3+
4+
// CMDB table where CI records are stored(Could be Linux/Unix,AIX server,etc)
5+
var ciTableName = 'cmdb_ci_computer';
6+
7+
// Array to hold the final output objects
8+
var ipDiscoveryMapping = [];
9+
10+
// Loop through each IP address in the list
11+
for (var i = 0; i < ipAddressLists.length; i++) {
12+
var ip = ipAddressLists[i];
13+
14+
// Query the CMDB to find the CI record with the given IP address
15+
var ciRecord = new GlideRecord(ciTableName);
16+
ciRecord.addEncodedQuery('ip_address=' + ip);
17+
ciRecord.query();
18+
19+
// Proceed only if a CI record is found for this IP
20+
if (ciRecord.next()) {
21+
22+
// Query discovery_device_history to find the related discovery status
23+
var deviceHistoryGr = new GlideRecord('discovery_device_history');
24+
deviceHistoryGr.addEncodedQuery(
25+
'cmdb_ci=' + ciRecord.getUniqueValue() +
26+
'^last_state=Created CI^ORlast_state=Updated CI'
27+
);
28+
deviceHistoryGr.query();
29+
30+
// If discovery history exists, capture its status number
31+
if (deviceHistoryGr.next()) {
32+
// Create an object with IP and discovery status number
33+
var recordObj = {
34+
ip_address: ip,
35+
discovery_status_number: deviceHistoryGr.getDisplayValue('status')
36+
};
37+
ipDiscoveryMapping.push(recordObj);
38+
} else {
39+
// Optional: handle case where no discovery status found
40+
var noStatusObj = {
41+
ip_address: ip,
42+
discovery_status_number: 'No discovery record found'
43+
};
44+
ipDiscoveryMapping.push(noStatusObj);
45+
}
46+
} else {
47+
// Optional: handle case where no CI record found
48+
var noCIObj = {
49+
ip_address: ip,
50+
discovery_status_number: 'No CI found for this IP'
51+
};
52+
ipDiscoveryMapping.push(noCIObj);
53+
}
54+
}
55+
56+
// Log the final array of IP–Discovery Status mappings
57+
gs.info('IP to Discovery Status mapping: ' + JSON.stringify(ipDiscoveryMapping));

0 commit comments

Comments
 (0)