The CreatorCon Call for Content is officially open! Get started here.

Writing a script to print duplicate servers

JaganR
Tera Contributor

// Define the CI classes to search for duplicates

var ciClasses = ['cmdb_ci_win_server', 'cmdb_ci_linux_server'];

 

// Function to find and print duplicate CIs

function findDuplicates(ciClass) {

    var gr = new GlideRecord(ciClass);

    gr.addQuery('install_status', '1'); // Active CIs only (modify if needed)

    gr.query();

    

    var ciMap = {}; // Object to store unique CIs based on key attributes

 

    while (gr.next()) {

        // Combine fields to create a unique key for each CI

        var uniqueKey = gr.name.toString() + '-' + gr.serial_number.toString() + '-' + gr.ip_address.toString();

 

        if (ciMap[uniqueKey]) {

            // Duplicate found, print the CI details

            gs.print('Duplicate CI found:');

            gs.print('CI Name: ' + gr.name);

            gs.print('Class: ' + ciClass);

            gs.print('Operational Status: ' + gr.operational_status.getDisplayValue());

            gs.print('Sys ID: ' + gr.sys_id);

            gs.print('Discovery Source: ' + gr.discovery_source.getDisplayValue());

            gs.print('---');

        } else {

            // Store the unique key to check against future CIs

            ciMap[uniqueKey] = true;

        }

    }

}

 

// Loop through each CI class and check for duplicates

for (var i = 0; i < ciClasses.length; i++) {

    findDuplicates(ciClasses[i]);

}

6 REPLIES 6

Hi @JaganR ,

If I understood your code and question correctly, you would like to get the details of duplicate servers based on names across 3 cmdb classes. I mean may be same CI name can be in all 3 cmdb classes as well.

 

I have written a code for you as below, but I could not get chance to run in my PDI, You may try this if it works for you and let me know. If there is any syntactical error or silly mistake please excuse me.

 

If this address your question, please mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

========================

// Define the server classes to filter
var classes = ['cmdb_ci_server', 'cmdb_ci_win_server', 'cmdb_ci_linux_server'];
var dupArray = [];
var dupObj = {};
// Use GlideAggregate to count occurrences of each name
var agg = new GlideAggregate('cmdb_ci');
agg.addQuery('sys_class_name', 'IN', classes.join(',')); // Filter by specific classes
agg.addNotNullQuery('name');
agg.groupBy('name');
agg.addAggregate('COUNT');
agg.addHaving('COUNT', '>', 1);
agg.query();
gs.print('--- Duplicate Servers ---');
while (agg.next()) {
    dupObj.name = agg.name.toString();
    dupObj.count = agg.getAggregate('COUNT');
    dupArray.push(dupObj);
    gs.print('Duplicate Server: ' + dupObj.name + ' (Count: ' + dupObj.count + ')');
}
// Query to get details for each duplicate record
if (dupArray.length > 0) {
    for (var i = 0; i < dupArray.length; i++) {
        var gr = new GlideRecord('cmdb_ci');
        gr.addQuery('name', dupObj[i].name);
        gr.addQuery('sys_class_name', 'IN', classes.join(','));
        gr.query();
        while (gr.next()) {
            gs.print('Class: ' + gr.sys_class_name.getDisplayValue());
            gs.print('Name: ' + gr.name);
            gs.print('SysID: ' + gr.sys_id);
            gs.print('Discovery Source: ' + gr.discovery_source);
            gs.print('Last Discovery Date: ' + gr.last_discovered);
            gs.print('Operational Status: ' + gr.operational_status.getDisplayValue());
            gs.print('---------------------------------------');
        }
    }
}
=============================

If this address your question, please mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

Thanks, 

Animesh Das

Hi @JaganR ,

If you got chance to check my solution/code please let me know if that worked for you.

 

If this address your question, please mark this response correct by clicking on Accept as Solution and/or Kudos.

You may mark this helpful as well if it helps you.

Thanks, 

Animesh Das