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

Jaspal Singh
Mega Patron
Mega Patron

Replace 

 

if(gr.sys_class_name == 'Windows Server')

with 

if(gr.sys_class_name == 'cmdb_ci_win_server')

 

Thanks,
Jaspal Singh

Hit Helpful or Correct on the impact of response.

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

Not sure what you mean but for those cases the Else part of the script would work.

 

Thanks,

Jaspal Singh

 

Hit Helpful or Correct on the impact of response.

My goal here is..

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!

Then use below snippet.

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 == 'cmdb_ci_windows_server'){
    gs.info('its a Windows Server');
//code whatever you want to udpate
  }
  else if(gr.sys_class_name == 'cmdb_ci_linux_server'){
    gs.info('its a Linux Server');
//code whatever you want to udpate
  }
  else{
    gs.info('its not a Windows or Linux Server');
//code whatever you want to udpate
  }
}

 

Thanks,

Jaspal Singh

 

Hit Helpful or Correct on the impact of response.