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

Thanks for your efforts on this code. Issue with the script is when there is 3 Windows servers, 1 Linux server else loop should execute but with the above code we are entering to 'else if' loop and 'else' loop result of that I am ending up with create records even in the Linux cluster which is not the right way. Thanks!

If that is the case try using below

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') || (gr.sys_class_name == 'cmdb_ci_linux_server'))
  {
    if(gr.sys_class_name == 'cmdb_ci_linux_server')
      {
         //This will insert in linux table
         gs.info('its a Linux Server');
       }
    else
       {
           gs.info('its a Windows Server');
          //This will insert in windows table
        }
   }

  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.

Jaspal, code still entering into other loops when I select multiple servers. Code should enter into if, else if loop when all of the selected servers are Windows or Linux not when user select multiple servers. Thank you!

You can build an object or array to store the classes you find, like so:

var foundClass = [];

// We will stop looping when we have more than one class in the array, or when we run out of records to process
while (gr.next() && foundClass.length < 1){

	// If we do not already have the class in the array, add it.
  if (foundClass.indexOf(gr.getValue('sys_class_name')) < 0){
	  foundClass.push(gr.getValue('sys_class_name'));
  }
  
}

// If we have more than one class, add to common
if (foundClass.length > 1){
	// do something
        processCommon();
} else if (foundClass.length == 1){
	
	var cls = foundClass[0];
	// Here you can check which class you have and do what you need to do
	switch (cls){
		case 'cmdb_ci_win_server':
			processWin();
			break;
		case 'cmdb_ci_linux_server':
			processLin();
			break;
		default:
			doSomethingElse();
	}
}

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');
}