|
| 1 | +//Place inside a Flow Script Step or a Workflow Script |
| 2 | +//Ensure current has a field like ip_address and discovery_status (create custom fields if needed) |
| 3 | +//You can also pass ip_address through inputs if not part of the current record |
| 4 | +//Replace mid_server_1 and mid_server_2 with actual MID Server names or sys_ids |
| 5 | +//You can customize status values or extend it to update logs/audit tables |
| 6 | + |
| 7 | +//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. |
| 8 | +//It adds reliability by using two MID servers, so if one fails, the other is used as a backup. |
| 9 | + |
| 10 | +(function execute(inputs, outputs) { |
| 11 | + //Define two MID servers for redundancy |
| 12 | + var midServer1 = 'mid_server_1'; // Replace with actual MID server name or sys_id |
| 13 | + var midServer2 = 'mid_server_2'; // Replace with actual MID server name or sys_id |
| 14 | + |
| 15 | + //Get IP address from current context (usually passed in workflow or flow) |
| 16 | + var ipAddress = current.ip_address || inputs.ip_address; |
| 17 | + |
| 18 | + if (!ipAddress) { |
| 19 | + gs.error('No IP address provided for discovery.'); |
| 20 | + current.discovery_status = 'Failed - No IP address'; |
| 21 | + current.update(); |
| 22 | + return; |
| 23 | + } |
| 24 | + |
| 25 | + gs.info('Starting Quick Discovery for IP: ' + ipAddress); |
| 26 | + |
| 27 | + //Function to trigger Quick Discovery |
| 28 | + function triggerQuickDiscovery(ip, mid) { |
| 29 | + var discovery = new Discovery(); |
| 30 | + return discovery.quickDiscovery(ip, mid); |
| 31 | + } |
| 32 | + |
| 33 | + //Attempt discovery with first MID server |
| 34 | + var status1 = triggerQuickDiscovery(ipAddress, midServer1); |
| 35 | + |
| 36 | + if (status1) { |
| 37 | + gs.info('Discovery triggered successfully using MID Server 1: ' + midServer1); |
| 38 | + current.discovery_status = 'Triggered via MID Server 1'; |
| 39 | + } else { |
| 40 | + gs.warn('Discovery failed with MID Server 1. Trying MID Server 2...'); |
| 41 | + |
| 42 | + var status2 = triggerQuickDiscovery(ipAddress, midServer2); |
| 43 | + |
| 44 | + if (status2) { |
| 45 | + gs.info('Discovery triggered successfully using MID Server 2: ' + midServer2); |
| 46 | + current.discovery_status = 'Triggered via MID Server 2'; |
| 47 | + } else { |
| 48 | + gs.error('Discovery failed with both MID servers.'); |
| 49 | + current.discovery_status = 'Failed - Both MID Servers'; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + //Update context with final discovery status |
| 54 | + current.update(); |
| 55 | + |
| 56 | +})(inputs, outputs); |
| 57 | + |
| 58 | + |
| 59 | + |
0 commit comments