Skip to content

Commit 968626b

Browse files
authored
CMDB/IRE - CI deduplication task generator (#1673)
* Create CI Deduplication Tasks Generation.js This script finds duplicate hardware CIs by their serial number and creates remediation tasks. * Create README.md readme details
1 parent 55f3cb9 commit 968626b

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* This script re-checks all duplicate hardware CIs by serial number and creates
3+
* a de-duplication task for any group that does not already have an open task.
4+
* It leverages the global.CMDBDuplicateTaskUtils Script Include.
5+
*/
6+
7+
(function() {
8+
9+
var ciTable = 'cmdb_ci_hardware'; //Change to any CI table of your choosing
10+
11+
var groupsFound = 0;
12+
var tasksCreated = 0;
13+
var groupsSkipped = 0;
14+
15+
16+
var taskUtil = new global.CMDBDuplicateTaskUtils();
17+
18+
// Find all serial numbers that have duplicates using GlideAggregate.
19+
var ga = new GlideAggregate(ciTable);
20+
ga.addNotNullQuery('serial_number');
21+
ga.addQuery('serial_number', '!=', '');
22+
ga.addAggregate('COUNT', 'serial_number');
23+
ga.groupBy('serial_number');
24+
ga.addHaving('COUNT', '>', 1);
25+
ga.query();
26+
27+
gs.info('Starting check for duplicate CIs by serial number...');
28+
29+
while (ga.next()) {
30+
groupsFound++;
31+
var serialNumber = ga.getValue('serial_number');
32+
var sysIdArray = [];
33+
var taskExists = false;
34+
35+
// For each duplicate serial number, get all associated CI sys_ids.
36+
var ciGr = new GlideRecord('cmdb_ci_hardware');
37+
ciGr.addQuery('serial_number', serialNumber);
38+
ciGr.query();
39+
while (ciGr.next()) {
40+
sysIdArray.push(ciGr.getUniqueValue());
41+
}
42+
43+
// Check if ANY of the CIs in the group are already in an open task.
44+
for (var i = 0; i < sysIdArray.length; i++) {
45+
if (!taskUtil.hasNoOpenDuplicateTasks(sysIdArray[i])) {
46+
taskExists = true;
47+
break; // Found an open task, no need to check the others.
48+
}
49+
}
50+
51+
// If no open task exists for this group, create one.
52+
if (taskExists) {
53+
groupsSkipped++;
54+
gs.info('--> Skipping Serial Number "' + serialNumber + '". It is already part of an open task.');
55+
} else {
56+
var sysIdString = sysIdArray.join(',');
57+
var newTaskId = taskUtil.createDuplicateTask(sysIdString);
58+
if (newTaskId) {
59+
tasksCreated++;
60+
gs.info('==> Successfully created task ' + newTaskId + ' for Serial Number "' + serialNumber + '".');
61+
} else {
62+
gs.error('==> FAILED to create task for Serial Number "' + serialNumber + '".');
63+
}
64+
}
65+
}
66+
67+
// --- Final Summary ---
68+
gs.info('--- Re-check Complete ---');
69+
gs.info('Total Duplicate Groups Found: ' + groupsFound);
70+
gs.info('New Remediation Tasks Created: ' + tasksCreated);
71+
gs.info('Groups Skipped (Already in an open task): ' + groupsSkipped);
72+
gs.info('--------------------------');
73+
74+
})();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# CI Deduplication Task Generator
2+
3+
This script rechecks the cmdb_ci_hardware table for duplicates by serial number and creates a De-Duplication Task if needed (for records that didn't run through the IRE).
4+
5+
### How It Works
6+
7+
1. Finds all serial numbers that are used on more than one hardware CI.
8+
9+
2. For each group of duplicates, it checks if any of the CIs are already part of an open de-duplication task.
10+
11+
3. If no open task exists, it creates a new one linking all CIs in the group.
12+
13+
4. Logs a summary of actions taken (tasks created, groups skipped).
14+
15+
### Dependencies
16+
17+
This script requires the `global.CMDBDuplicateTaskUtils` Script Include to be active in your instance.
18+
19+
### Configuration & Use
20+
21+
This script is meant to be run as a **Scheduled Job** or as a **Background Script**.
22+
23+
Before you run it, you must set the target table.
24+
25+
```
26+
// Change this line in the script!
27+
var ciTable = "cmdb_ci_hardware"
28+
29+
30+
```
31+
32+
Change `"cmdb_ci_hardware"` to the table you want to run the script against.
33+
34+
### Example Log Output
35+
36+
```
37+
Starting check for duplicate CIs by serial number...
38+
==> Successfully created task RITM0010123 for Serial Number "VMW-50-81-7A-C9-23-44".
39+
--> Skipping Serial Number "SGH814X025". It is already part of an open task.
40+
--- Re-check Complete ---
41+
Total Duplicate Groups Found: 2
42+
New Remediation Tasks Created: 1
43+
Groups Skipped (Already in an open task): 1
44+
--------------------------
45+
46+
47+
```

0 commit comments

Comments
 (0)