Name
sn_cmdb_int_util.CmdbIntegrationOsUtil
Description
Utility class to cleanse and insert new os choice values into computer
Script
var CmdbIntegrationOsUtil = Class.create();
/*
cleanseAndInsertOs : cleanses os name and adds to cmdb_ci_computer.os choice list. makes assumption that os label and value are the same.
*/
CmdbIntegrationOsUtil.prototype = {
initialize: function() {
},
cleanseAndInsertOs: function(osName) {
if (!osName)
return null;
var osNameOut;
if (osName.toLowerCase().contains("microsoft") || osName.toLowerCase().contains("windows")){
osNameOut = this._windowsOsCleanupName(osName);
}
else{
osNameOut = this._fixCasing(osName);
}
//replace Ios with iOS if exists
osNameOut = osNameOut.replace(/\bios/gi, "iOS");
var result = new global.OSChoice().reconcile(osNameOut);
return result;
},
_fixCasing: function(osNameTemp) {
if (!osNameTemp)
return null;
return osNameTemp.split(' ').map(function(word,index){
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}).join(' ');
},
// taken from the SCCM2016 WindowsOSNameHelper
_windowsOsCleanupName: function(name) {
var value = name;
var replaceArr = [];
replaceArr.push({'®': ""}); // For fixing Windows 2008 DC (ie. Windows Server® 2008 Datacenter)
replaceArr.push({'®': ""});
replaceArr.push({"\\(R\\)" : ""});
replaceArr.push({"Windowsr" : "Windows"});
replaceArr.push({"Serverr" : "Server"});
replaceArr.push({"WINDOWS" : "Windows"});
replaceArr.push({"â¢" : ""}); // For fixing Vista (ie. "Vistaâ¢")
replaceArr.push({"VistaT" : "Vista"});
replaceArr.push({" Edition" : ""});
replaceArr.push({"microsoft" : ""});
replaceArr.push({"64 bit" : ""});
replaceArr.push({"64-bit" : ""});
replaceArr.push({"32 bit" : ""});
replaceArr.push({"32-bit" : ""});
replaceArr.push({"ms" : "" });
replaceArr.push({"," : ""}); // Replace anything like "2003, Standard" with just "2003 Standard"
for(var i=0; i<replaceArr.length; i++) {
replaceObj = replaceArr[i];
for(var elem in replaceObj) {
var re = new RegExp(elem, "gi");
value = value.replace(re, replaceObj[elem]);
}
}
var servicePackIndex = value.toUpperCase().indexOf(" SP");
if (!servicePackIndex || servicePackIndex < 0)
servicePackIndex = value.toUpperCase().indexOf("SERVICE PACK");
if (servicePackIndex > -1)
value = value.substring(0, servicePackIndex);
//Remove any spaces more than one (ie. "Windows 2008" should be "Windows 2008")
value = value.replace(/\s\s+/g, " ");
// Remove any leading and trailing spaces
value = value.trim();
value = this._fixCasing(value);
// special all upper cases
replaceArr = [];
replaceArr.push({" xp": " XP"});
replaceArr.push({" me": " ME"});
for(var j=0; j<replaceArr.length; j++) {
replaceObj = replaceArr[j];
for(var elem2 in replaceObj) {
var re2 = new RegExp(elem2, "gi");
value = value.replace(re2, replaceObj[elem2]);
}
}
return value;
},
type: 'CmdbIntegrationOsUtil'
};
Sys ID
7439e39e732100102b6265a751ab9eda