Skip to content

Commit 165b621

Browse files
Create Discovery: Automated Device Discovery in ServiceNow Using Quick Discovery
This code snippet automates the discovery of devices in ServiceNow using MID (Management, Instrumentation, and Discovery) servers by triggering Quick Discovery through a workflow. The code can also be modified for different workflows/ flows as needed. Key features include: MID Server Setup: It defines two MID servers to ensure redundancy, which improves the chances of successfully finding devices. IP Address Retrieval: The code retrieves the IP address to be scanned from the current workflow context. Discovery Process: It creates a Discovery object to initiate the process. The script first attempts to find the device using the first MID server. If that fails, it logs a message and tries again with the second MID server. Logging and Status Update: After the discovery attempts, the script logs the results and updates the current context with the discovery status. This code enhances device discovery in ServiceNow, making the process more reliable by utilizing multiple MID servers.
1 parent 690dba9 commit 165b621

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The JavaScript code is part of a ServiceNow workflow that helps find a device on the network using a specific IP address. It starts by defining two MID (Management, Instrumentation, and Discovery) servers and retrieves the target IP address from the current workflow. The code creates a Discovery object to handle the discovery process. First, it tries to find the device using the first MID server and saves the result. If that doesn’t work (if the result is null), it logs a message and tries again with the second MID server. Finally, it logs the discovery result and the IP address, updating the current context with the discovery status. This method increases the chances of successfully finding the device by trying two different servers.
2+
3+
var midServer1 = 'SNOWMIDPRD01'; // Rename/input your MID server as per your organization
4+
var midServer2 = 'SNOWMIDPRD02'; // Rename/input your second MID server accordingly
5+
var ipAddressScan = current.variables.ip_address; // IP address to scan
6+
var discovery = new Discovery(); // Create a new Discovery object
7+
8+
// Attempt discovery using the first MID Server
9+
workflow.scratchpad.statusID = discovery.discoveryFromIP(ipAddressScan, midServer1);
10+
11+
// If the first attempt was unsuccessful, try the second MID Server
12+
if (workflow.scratchpad.statusID == null) {
13+
gs.info('Discovery using ' + midServer1 + ' failed. Trying ' + midServer2);
14+
workflow.scratchpad.statusID = discovery.discoveryFromIP(ipAddressScan, midServer2);
15+
}
16+
17+
// Log the results
18+
gs.log('DiscoveryStatusId: ' + workflow.scratchpad.statusID);
19+
gs.log('QuickDiscoveryIPAddress: ' + ipAddressScan);
20+
current.variables.discovery_status = workflow.scratchpad.statusID;

0 commit comments

Comments
 (0)