How do I iterate through a list of hostnames so I can use each entry in a glide record query?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-20-2023 08:38 AM
Hello,
I'm new to ServiceNow and need some help figuring out how to iterate through a list so I can use each device name in the list in a glide record query. I would like to see scan information pertaining to each machine in the list as the end result. Please allow me to provide some background. My team receives requests to scan specific machines on the network to identify vulnerabilities on each device. The number of machines varies depending on the customer request, so I need a way to use each hostname separately in a glide record query. I created the function below and discovered that if I declare the same number of parameters in the function that gets inputted into the function call at the end of the code, I can view unique scan data from each entry by changing from "dnsName1" to "dnsName2" or "dnsName3" in line 6 and line 14. Should i be using something like the rest parameter in a function like "function scanInfo(...args)" or something like a for loop for(i=0;i<array.length;i++)? I noticed how i can use that iteration technique in a while loop, but get confused with how to code the while(gliderecord.next()) and something like the following: while(i<=5) { gs.print(i); i++; }. Any help would be much appreciated and thank you for your time!
Thanks,
Mike
function scanInfo(dnsName1,dnsName2,dnsName3) {
var dnsNameList = [];
var parkerGlide = new GlideRecord('sn_vul_detection');
parkerGlide.addEncodedQuery('status=0^last_foundRELATIVEGT@dayofweek@ago@60^source=Tenable.sc^vulnerability=0135e7de1b2981d03bdf859ce54bcb45');
parkerGlide.addQuery('dns','IN', dnsName3);
parkerGlide.query();
while(parkerGlide.next()) {
var scanName = parkerGlide.proof.split('\n').slice(9,10);
var scanStartDate = parkerGlide.proof.split('\n').slice(34,35);
var credentialedChecks = parkerGlide.proof.split('\n').slice(22,23);
dnsNameList.push(dnsName1,dnsName2,dnsName3);
gs.print('\n' + dnsNameList);
return '\n' + 'DNS Name : ' + dnsName3 + '\n' + scanName + '\n' + scanStartDate + '\n' + credentialedChecks;
}
}
var name = scanInfo("computer1.domain.com","computer2.domain.com","computer3.domain.com");
gs.print(name);