Writing a script to print duplicate servers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2024 10:41 AM
// 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]);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-23-2024 11:34 AM
Hi Jagan,
Can you please explain what is your ask here for people to help you?
Thanks,
Animesh Das
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2024 05:21 AM - edited 09-24-2024 05:22 AM
Hi Animesh, I am just trying for the script to print all the duplicate servers in CMDB.
Please help me if you have any best approaches to identify and fix the duplicate servers issues
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2024 05:13 AM - edited 09-24-2024 05:27 AM
Removed--This
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-24-2024 05:26 AM
// Define the server classes to filter
var classes = ['cmdb_ci_server', 'cmdb_ci_win_server', 'cmdb_ci_linux_server'];
// Use GlideAggregate to count occurrences of each name
var agg = new GlideAggregate('cmdb_ci');
agg.addAggregate('COUNT', 'name');
agg.addQuery('sys_class_name', 'IN', classes.join(',')); // Filter by specific classes
agg.addHaving('COUNT', 'name', '>', 1); // Only fetch records with duplicate names
agg.groupBy('name');
agg.query();
// Loop through aggregated results and print duplicate names
gs.print('--- Duplicate Servers ---');
while (agg.next()) {
var serverName = agg.getValue('name');
var count = agg.getAggregate('COUNT', 'name');
gs.print('Duplicate Server: ' + serverName + ' (Count: ' + count + ')');
// Query to get details for each duplicate record
var gr = new GlideRecord('cmdb_ci');
gr.addQuery('name', serverName);
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('---------------------------------------');
}
}