Rerouting of VM CI's based on Naming Convtention

Karthikeyan03
Tera Contributor

Hello team,

 

As an ITOM developer, I want to reroute the VM CI's based on naming convention, under mid server script includes the following "VMWAREvCenterVMsProbe" creates virtual machines which then goes into cmdb_ci_vmware_instance upon discovery of vCenter's. If possible any virtual machine that begin with "USX" or "USZ we would like to have routed to the cmdb_ci_virtual_desktop table upon discovery, all others would remain in the same table. All CI's that starts with USX or USZ would be automatically moved to cmdb_ci_virtual_desktop upon discovery.

 

 

Should I enable plugin for above requirement?

 

Would you please correct the steps and scripts below, if it is wrong. I am grateful to your response.

 

var vmName = current.name.toString();

if (vmName.startsWith("USX") || vmName.startsWith("USZ")) {

   // Create a new record in cmdb_ci_virtual_desktop table and copy over relevant information

   var newDesktop = new GlideRecord('cmdb_ci_virtual_desktop');

   newDesktop.initialize();

   newDesktop.name = current.name;

   newDesktop.description = current.description;

   newDesktop.os = current.os;

   newDesktop.ip_address = current.ip_address;

   newDesktop.mac_address = current.mac_address;

   // ... copy over other relevant fields

   newDesktop.insert();

  

   }

 

to modify the "process" function in the "VMWarevCenterVMsProbe" script include to include the logic I described earlier:

process: function(vm) {

   // Call the parent process function to do the standard processing

   this.parent.process(vm);

   // Check if the VM name starts with "USX" or "USZ"

   var vmName = vm.name.toString();

   if (vmName.startsWith("USX") || vmName.startsWith("USZ")) {

      // Create a new record in cmdb_ci_virtual_desktop table and copy over relevant information

      var newDesktop = new GlideRecord('cmdb_ci_virtual_desktop');

      newDesktop.initialize();

      newDesktop.name = vm.name;

      newDesktop.description = vm.description;

      newDesktop.os = vm.os;

      newDesktop.ip_address = vm.ip_address;

      newDesktop.mac_address = vm.mac_address;

      // ... copy over other relevant fields

      newDesktop.insert();

     

        }

}

1 ACCEPTED SOLUTION

Hi,

Looks good to me but please test it out in a non-prod environment to be sure.

Regards,
Niklas

View solution in original post

8 REPLIES 8

OK, so your issue is something like what is described here? https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0954440

Both cmdb_ci_virtual_desktop and cmdb_ci_vmware_instance extends Virtual Machine Object (cmdb_ci_vm_object) which means most of the attributes are the same. You should be able to reclassify by changing the sys_class_name attribute from cmdb_ci_vmware_instance to cmdb_ci_virtual_desktop with a Business Rule. 

 

Regards,

Niklas

Hi Niklas,

 

Thanks a ton for checking this out and providing relevant information, I got your point and below is business rule to reclassify CIs by changing attribute

(function exexcuteRule(current, previous) {

//check if the CI is a virtual deskop

if(current.sys_class_name == 'cmdb_ci_vmware_instance' && (current.name.startsWith('USZ') || current.name.startsWith('USX')))

{

//change the sys_class_name to cmdb_ci_virtual_desktop

current.sys_class_name = 'cmdb_ci_virtual_desktop':

 

gs.info('reclassified virtual desktop CI: ' + current.name + ' to cmdb_ci_virtual_desktop');

}

})(current, previous);

Please correct me if I am wrong. I really appreciate your health.

Hi,

Looks good to me but please test it out in a non-prod environment to be sure.

Regards,
Niklas

Looks like it worked upon discovery, thanks a million for your kind help.

 

Regards

Karthikeyan Palaniyappan