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

ccajohnson
Kilo Sage

It is unclear how the value of servers is defined. Is this a field on a form, or something else? The sys_class_name of a CI record is the table that it resides on, not the Label of the table. try using 'cmdb_ci_win_server' instead of 'Windows Server' in your if condition.

Thanks Johnson. Above replacement working but how can stop executing the If loop if there is at least one server class found to be different ex. Linux?

It is unclear what you are trying to accomplish with your if/else combination. Are you trying to determine if any of the selected CI servers are windows? If that is the case, you could set a variable as true when it meets the condition. Taking what you have as a sample, here is the inclusion of a Boolean variable:

var chk = chkWinServer(servers);
gs.info(chk);

function chkWinServer(servers) {
    var ciSysIds = servers //user selects multiple servers
    var gr = new GlideRecord('cmdb_ci_server');
    gr.addQuery('sys_id','IN',ciSysIds);
    gr.query();
    var returnChk = false;
    while(gr.next()){
        if(gr.sys_class_name == 'cmdb_ci_win_server'){
            returnChk = true;
        }
    }
    return returnChk;
}

My goal here is..If all of selected servers are Windows or Linux or mixed

If I found all of them as a Windows server then I will have to insert some data into 'Windows Cluster'

else if I found all of them as a Linux server then I will have to insert some data into 'Linux Cluster'

else I found them as mixed servers then I will have to insert some records to common 'Cluster'

Please let me know if you have any script ideas.

Thanks!