Server name swap After connecting to a new SCCM Server

Brian Lancaster
Tera Sage

We have an integration with SCCM and SolarWinds using a Data Source of type JDBC. We pull the data from views and populate the CMDB. Recently we switch to a new SCCM database and after the integration ran at least two servers swapped names. I'm trying to figure out the extent of the issue to see how many of CI this happened to as we get other CIs from SCCM other then Servers. I would like to figure out a script to identify the ones this happened to and then switch them back once I figure out what caused this. I figured this would be a less work on fixing old incidents, problems, changes, etc. We identified there was an issue almost immediately when someone submitted a change with one CI but when they presented it at CAB the CI name was different. Example screenshot from the history of one of the CI. I had to blank host of it out but as you can see by the first two letters of the name before and after are different.

BrianLancaster_0-1735577822713.png

Is there a way I can script this so I can identify all the CI were it just swapped name?

Also any thoughts on how this could happen when we coalesce on the name field?

3 REPLIES 3

Harsh Vardhan
Giga Patron

@Brian Lancaster  Have you tried with "HistoryWalker" api, you can use this api to write a script to get old name .

 

eg: 

var grcd = new GlideRecord('cmdb_ci_linux_server');
grcd.get('sys_id', 'Record SysID');
var hw = new sn_hw.HistoryWalker(grcd.getTableName(), grcd.getUniqueValue(), true);
if (hw.walkTo(3)) 
    gs.info('Old Name was ' + hw.getWalkedRecord().name);

 

Doc link for further help : 

HistoryWalker  

 

Hope it will help you.

 

Thanks,

Harsh

This only allow me to do it on a CI where I specify the sys_id of the record. How can I run this against the entire CMDB and return all item where old name and new name do not match.

Brian Lancaster
Tera Sage

I got an enter scrip from Support. I still had to do it one table at a time so thankfully we are not getting a lot of Classes from the SolarWinds and SCCM. It turned out that both new DB caused the issue. Here is the code support gave me.

 

var targetMonth = '2024-12'; // Set the target month (format: YYYY-MM)
var changedCICount = 0; // Counter for number of CIs with changes
var updatedCICount = 0; // Counter for number of records updated
var dryRun = true; // Set to false to make actual updates

var gr = new GlideRecord('cmdb_ci_computer');
gr.addEncodedQuery('u_solarwinds_node_idISNOTEMPTY^ORu_sccm_node_idISNOTEMPTY');
gr.setLimit(100); //remove this when you want to get all the CI info and when you set dryRun above to false.
gr.query();

while (gr.next()) {
    var sysId = gr.getUniqueValue();
    var tableName = gr.getTableName();

    // Instantiate HistoryWalker with HISTORY walker
    var hw = new sn_hw.HistoryWalker(tableName, sysId, 'HISTORY');
    var previousName = null; // To store the previous name for comparison
    var hasChanges = false; // Track if any changes are detected

    while (hw.walkForward()) {
        var walkedGr = hw.getWalkedRecord(); // Get the current walked record

        // Ensure the 'name' field exists
        if (!walkedGr.isValidField('name')) {
            continue; // Skip iteration if field is invalid
        }

        var currentName = walkedGr.getValue('name'); // Get the current 'name' field value
        var updatedOn = walkedGr.getValue('sys_updated_on'); // Get the updated date/time

        // Extract the month and year part from 'sys_updated_on' (YYYY-MM)
        var updatedMonth = updatedOn.split(' ')[0].slice(0, 7);

        if (updatedMonth === targetMonth) {
            if (previousName !== null && previousName !== currentName) {
                hasChanges = true;

                // Print the detected change with the date
                gs.print(
                    'Change detected: Old Name = ' +
                    previousName +
                    ', New Name = ' +
                    currentName +
                    ', Date Changed = ' +
                    updatedOn
                );

                // Update the record to revert the name change
                if (!dryRun) {
                    gr.setWorkflow(false); // Disable business rules
                    gr.setValue('name', previousName); // Revert to old name
                    gr.update();
                    updatedCICount++; // Increment update count
                }
            }
        }

        // Update the previousName to the currentName for the next iteration
        previousName = currentName;
    }

    if (hasChanges) {
        changedCICount++; // Increment count if this CI had changes
    }
}

// Print the total count of CIs with changes and updates
gs.print('Total number of CIs changed in ' + targetMonth + ': ' + changedCICount);
gs.print('Total number of CIs updated: ' + updatedCICount);