forked from ServiceNowDevProgram/code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTechTrekwithAJ-Automate Discovery using flow Script Step.js
More file actions
59 lines (44 loc) · 2.38 KB
/
TechTrekwithAJ-Automate Discovery using flow Script Step.js
File metadata and controls
59 lines (44 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//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);