Automatic addition of Ip address to discovery schedule

Raunak kapoor2
Tera Expert

I have a list of windows servers which i want to add to a discovery schedule

i want to use the encoded query on the windows server page and add the ip addresses to a specific discovery schedule via a fix script 
can someone help me with that please?

 

1 ACCEPTED SOLUTION

Raunak kapoor2
Tera Expert

Anyways I created it myself and it worked 😁

// Function to get the list of IP addresses from an encoded query
function getIPAddressesFromQuery(encodedQuery) {
    var ipAddresses = [];
    var gr = new GlideRecord('cmdb_ci_win_server');
    gr.addEncodedQuery(encodedQuery);
    gr.query();
    while (gr.next()) {
        ipAddresses.push(gr.ip_address.toString());
    }
    return ipAddresses;
}

// Function to add a comma-separated list of IP addresses to a single discovery range item
function addIPsToSingleDiscoveryRangeItem(ipList, scheduleSysId) {
    var ipArray = ipList.split(',');
    
    // Create a single discovery range item
    var ipRange = new GlideRecord('discovery_range_item');
    ipRange.initialize();
    ipRange.name = 'Consolidated IP Range'; // Name of the range item
    ipRange.active = true;
    ipRange.type = 'IP Address List';
    ipRange.schedule = scheduleSysId;
    var ipRangeSysId = ipRange.insert();
    
    // Add each IP address under the single range item
    ipArray.forEach(function(ip) {
        var ipRangeIP = new GlideRecord('discovery_range_item_ip');
        ipRangeIP.initialize();
        ipRangeIP.item_parent = ipRangeSysId;
        ipRangeIP.ip_address = ip.trim();
        ipRangeIP.insert();
    });
    
    gs.info('IP addresses added to a single discovery range item in the discovery schedule with sys_id: ' + scheduleSysId);
}

// Main script
(function() {
    var encodedQuery = 'operational_status=1^discovery_source=ServiceNow^last_discoveredONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^ORlast_discoveredISEMPTY';
    var scheduleSysId = '031284c79307021082c0ff118bba10a8'; // Discovery schedule sys_id
    
    // Get the list of IP addresses from the encoded query
    var ipAddresses = getIPAddressesFromQuery(encodedQuery);
    var ipList = ipAddresses.join(',');
    
    // Add IP addresses to a single discovery range item in the discovery schedule
    addIPsToSingleDiscoveryRangeItem(ipList, scheduleSysId);
})();
 

View solution in original post

6 REPLIES 6

Pratiksha
Mega Sage
Mega Sage

// Initialize GlideRecord to query the cmdb_ci_win_server table
var grWindowsServers = new GlideRecord('cmdb_ci_win_server');
grWindowsServers.query();

while (grWindowsServers.next()) {
// Get the IP address of the current Windows server
var ipAddress = grWindowsServers.ip_address;

if (ipAddress) {
// Initialize GlideRecord to create a new discovery_schedule_range record
var grDiscoveryScheduleRange = new GlideRecord('discovery_schedule_range');
grDiscoveryScheduleRange.initialize();
grDiscoveryScheduleRange.discovery_schedule = discoveryScheduleSysId;
grDiscoveryScheduleRange.ip_address = ipAddress;
grDiscoveryScheduleRange.range = ipAddress; // Assuming it's a single IP address range
grDiscoveryScheduleRange.insert();
}
}

gs.print('Windows servers have been successfully added to the discovery schedule.');

Answer from Chatgpt ?
Sorry but this doesnt help 

I have already tried that 

Raunak kapoor2
Tera Expert

Anyways I created it myself and it worked 😁

// Function to get the list of IP addresses from an encoded query
function getIPAddressesFromQuery(encodedQuery) {
    var ipAddresses = [];
    var gr = new GlideRecord('cmdb_ci_win_server');
    gr.addEncodedQuery(encodedQuery);
    gr.query();
    while (gr.next()) {
        ipAddresses.push(gr.ip_address.toString());
    }
    return ipAddresses;
}

// Function to add a comma-separated list of IP addresses to a single discovery range item
function addIPsToSingleDiscoveryRangeItem(ipList, scheduleSysId) {
    var ipArray = ipList.split(',');
    
    // Create a single discovery range item
    var ipRange = new GlideRecord('discovery_range_item');
    ipRange.initialize();
    ipRange.name = 'Consolidated IP Range'; // Name of the range item
    ipRange.active = true;
    ipRange.type = 'IP Address List';
    ipRange.schedule = scheduleSysId;
    var ipRangeSysId = ipRange.insert();
    
    // Add each IP address under the single range item
    ipArray.forEach(function(ip) {
        var ipRangeIP = new GlideRecord('discovery_range_item_ip');
        ipRangeIP.initialize();
        ipRangeIP.item_parent = ipRangeSysId;
        ipRangeIP.ip_address = ip.trim();
        ipRangeIP.insert();
    });
    
    gs.info('IP addresses added to a single discovery range item in the discovery schedule with sys_id: ' + scheduleSysId);
}

// Main script
(function() {
    var encodedQuery = 'operational_status=1^discovery_source=ServiceNow^last_discoveredONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()^ORlast_discoveredISEMPTY';
    var scheduleSysId = '031284c79307021082c0ff118bba10a8'; // Discovery schedule sys_id
    
    // Get the list of IP addresses from the encoded query
    var ipAddresses = getIPAddressesFromQuery(encodedQuery);
    var ipList = ipAddresses.join(',');
    
    // Add IP addresses to a single discovery range item in the discovery schedule
    addIPsToSingleDiscoveryRangeItem(ipList, scheduleSysId);
})();