Run DiscoverNow from a script using multiple IP addresses 


In ServiceNow Product Documentation, the following article, Run DiscoverNow from a Script, describes the basic process of running DiscoverNow from a script. However, customers who are required to run this script and also require the ability to enter multiple IP addresses, are unable to get the process to work correctly.

When reviewing the code, we noticed that all of the variables were defined globally, so we suggest that the code be encapsulated in a function. This resolves the issue. 

When using short variables such as d (as seen in the following script example), and defining the variable globally, it is very easy for the variable to be overwritten if another script is also using a globally defined variable called d, elsewhere on the system. Encapsulating the code within a function fences off the variables, which are defined locally and can not be re-defined outside of that function.

The next section provides an example for customers who may require the same functionality. 


Example Script


function RunMyDiscoverNow() {

//Enter the IP Adresses in the Array IPAdresses, and comma seperate
//Example: "x.x.x.1, x.x.x.2, x.x.x.3"

var IPAdresses = ",,";

var iparray = IPAdresses.split(",");

for (var i=0; i < iparray.length; i++) {
  var ipaddr = iparray[i]
  //Ensure that a valid MID server name is used as the 
  //2nd parameter to d.discoveryFromIP
  var d = new Discovery();
  var statusID = d.discoveryFromIP(ipaddr,'MIDSERVER1');
  gs.print("This is the address that was scanned " +ipaddr);



Article Information

Last Updated:2019-08-02 21:27:57