How to check If condition to determine all server classes are same?

Paul125
Kilo Guru

How do I check if the selected servers all are 'Windows Servers' by reading the CI classes? I built this script but doing something wrong in the If condition. If loop should execute when selected servers related to same 'Windows Server' class. If at least one server class is different then else should execute. Thanks

var ciSysIds = servers //user selects multiple servers
var gr = new GlideRecord('cmdb_ci_server');
gr.addQuery('sys_id','IN',ciSysIds);
gr.query();
while(gr.next()){
  if(gr.sys_class_name == 'Windows Server'){
    gs.info('its a Windows Server');
  }
  else{
    gs.info('its not a Windows Server');
  }
}
1 ACCEPTED SOLUTION

ccajohnson
Kilo Sage

My guess is that you need to determine the count of the different server classes selected, then do something based upon the results. Since we are iterating through the query, we can set a count of the number of iterations first, then add a count for each of the different classes found. One concern of mine is if it is neither Windows or Linux, what to do. I am guessing that it will be part of the common cluster. I have taken your code and made it so we are not using gr and added some counting logic:

var ciSysIds = servers //user selects multiple servers
var svrObj = new GlideRecord('cmdb_ci_server');
svrObj.addQuery('sys_id','IN',ciSysIds);
svrObj.query();
var svrCount = svrObj.getRowCount();
var winCount = 0;
var lnxCount = 0;
while(svrObj.next()){
    if(svrObj.sys_class_name == 'cmdb_ci_win_server'){
        winCount++;
    }
    if(svrObj.sys_class_name == 'cmdb_ci_linux_server'){
        lnxCount++;
    }
}
if (svrCount == winCount) {
    gs.info('Add to Windows Cluster');
}
if (svrCount == lnxCount) {
    gs.info('Add to Linux Cluster');
}
if (svrCount > winCount && svrCount > lnxCount) {
    gs.info('Add to Common Cluster');
}

View solution in original post

15 REPLIES 15

This is great! Thanks you so much for your efforts Johnson!