|
| 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 | + |
0 commit comments