Issue
Run DiscoverNow from a script using multiple IP addresses
\r\n\r\n
Background
\r\n\r\n
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.
\r\nWhen 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.
\r\nWhen 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.
\r\nThe next section provides an example for customers who may require the same functionality.
\r\n\r\n
Example Script
\r\n\r\n
\r\n\r\n
function RunMyDiscoverNow() {\r\n\r\n//Enter the IP Adresses in the Array IPAdresses, and comma seperate\r\n//Example: "x.x.x.1, x.x.x.2, x.x.x.3"\r\n\r\nvar IPAdresses = "10.200.218.27, 10.200.218.99, 10.200.218.191";\r\n\r\nvar iparray = IPAdresses.split(",");\r\n\r\nfor (var i=0; i < iparray.length; i++) {\r\n var ipaddr = iparray[i]\r\n \r\n //Ensure that a valid MID server name is used as the \r\n //2nd parameter to d.discoveryFromIP\r\n \r\n var d = new Discovery();\r\n var statusID = d.discoveryFromIP(ipaddr,'MIDSERVER1');\r\n gs.print("This is the address that was scanned " +ipaddr);\r\n } \r\n}\r\n\r\nRunMyDiscoverNow();\r\n\r\n
\r\n