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

Raunak kapoor2
Tera Expert

This one is to only add new IP's and ignore the existing Ip addresses.

// Function to get the list of existing IP addresses in the discovery schedule
function getExistingIPAddresses(scheduleSysId) {
    var existingIPs = [];
    var ipRange = new GlideRecord('discovery_range_item');
    ipRange.addQuery('schedule', scheduleSysId);
    ipRange.query();
    while (ipRange.next()) {
        var ipRangeIP = new GlideRecord('discovery_range_item_ip');
        ipRangeIP.addQuery('item_parent', ipRange.sys_id);
        ipRangeIP.query();
        while (ipRangeIP.next()) {
            existingIPs.push(ipRangeIP.ip_address.toString());
        }
    }
    return existingIPs;
}

// 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 = 'New IP Range ' + new Date().toISOString(); // Unique name for the new 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('New 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 existing IP addresses in the discovery schedule
    var existingIPs = getExistingIPAddresses(scheduleSysId);
    
    // Get the list of new IP addresses from the encoded query
    var newIPAddresses = getIPAddressesFromQuery(encodedQuery);
    var newIPs = newIPAddresses.filter(function(ip) {
        return existingIPs.indexOf(ip) === -1;
    });
    
    if (newIPs.length > 0) {
        var ipList = newIPs.join(',');
        
        // Add new IP addresses to a single discovery range item in the discovery schedule
        addIPsToSingleDiscoveryRangeItem(ipList, scheduleSysId);
    } else {
        gs.info('No new IP addresses to add.');
    }
})(); 

Pratiksha
Mega Sage
Mega Sage

Hi, this may not be the best practice. As and when new win system added you need wait for the script to run to get the IP range. Plus if manually something is not added correctly will lead in not discoverying the device. 

 

1) try network discovery it will scan the infra and give you IP range.

2)Export the IP from the win server table. Put them in templet of IP ranges and do an import. 

3)Ask the infra team to share the IP ranges. (with this not only the old devices will get discover but any future device will also be discovered without any intervention.) Most of the time infra team have set of ranges defined and the devices were given the IP address from pool of predefined  range.  Hope it helps