Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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.
These are the two MID Servers that will be used to perform the discovery.
You need to replace 'mid_server_1' and 'mid_server_2' with real MID Server names or sys_ids.
This gives you a backup option if the first one fails.
The script tries to get the IP address from the current record (current.ip_address).
If that’s not available, it tries to use inputs.ip_address (passed into the script).
If no IP address is found, it logs an error and stops.
This is a helper function that tells ServiceNow to start Quick Discovery for a specific IP address using a specific MID Server.
It first tries to start discovery using the first MID server.
If it works, it logs a success message and updates the record to show that discovery was triggered using MID Server 1.
If the first attempt fails, it logs a warning and tries again with the second MID server.
If the second attempt also fails, it logs an error and updates the record to reflect that both attempts failed.
The script updates the current record with the result of the discovery attempt (success or failure and which MID server was used).
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//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);



Loading