VMware {code} Community
peterc4270
Contributor
Contributor
Jump to solution

How to modify a VM's network connection name via Perl Toolkit

We have a number of private networks which may be assigned to VMs, depending on certain requirements.

I can extract the network name from the vm_view->network array, but I cannot find a corresponding method to update the value.

Any help/pointers would be very much appreciated.

Cheers .

Tags (1)
Reply
0 Kudos
1 Solution

Accepted Solutions
StefanPahrmann
Jump to solution

Hi,

remember to set the first 2 variables:

   my $vSwitchName = "";
   my $vmname="";
   my $vm_ref = Vim::find_entity_view((view_type => 'VirtualMachine'), 
                                      filter => { name => '^'.$vmname.'$' } );
   
   my $net_name="Network Adapter 1";
   my $new_net_name=$vSwitchName;
   my $net_device="";
   $config_spec_operation = VirtualDeviceConfigSpecOperation->new('edit');
   my $devices = $vm_ref->config->hardware->device;

   foreach my $device (@$devices) {
      if ($device->deviceInfo->label eq $net_name){
          $net_device=$device;
       }
   }
   if($net_device){
       $net_device->deviceInfo->summary( $new_net_name ) ;
       $net_device->backing->deviceName( $new_net_name ) ;
   }
   my $vm_dev_spec = VirtualDeviceConfigSpec->new(
         device => $net_device,
         operation => $config_spec_operation
   );

   my $vmSwitchspec = VirtualMachineConfigSpec->new(deviceChange => [ $vm_dev_spec ] );

   eval{
      $vm_ref->ReconfigVM(spec => $vmSwitchspec);
   };

View solution in original post

Reply
0 Kudos
4 Replies
StefanPahrmann
Jump to solution

Hi,

remember to set the first 2 variables:

   my $vSwitchName = "";
   my $vmname="";
   my $vm_ref = Vim::find_entity_view((view_type => 'VirtualMachine'), 
                                      filter => { name => '^'.$vmname.'$' } );
   
   my $net_name="Network Adapter 1";
   my $new_net_name=$vSwitchName;
   my $net_device="";
   $config_spec_operation = VirtualDeviceConfigSpecOperation->new('edit');
   my $devices = $vm_ref->config->hardware->device;

   foreach my $device (@$devices) {
      if ($device->deviceInfo->label eq $net_name){
          $net_device=$device;
       }
   }
   if($net_device){
       $net_device->deviceInfo->summary( $new_net_name ) ;
       $net_device->backing->deviceName( $new_net_name ) ;
   }
   my $vm_dev_spec = VirtualDeviceConfigSpec->new(
         device => $net_device,
         operation => $config_spec_operation
   );

   my $vmSwitchspec = VirtualMachineConfigSpec->new(deviceChange => [ $vm_dev_spec ] );

   eval{
      $vm_ref->ReconfigVM(spec => $vmSwitchspec);
   };

Reply
0 Kudos
peterc4270
Contributor
Contributor
Jump to solution

Hi,

This looks great!

Many thanks for the fast response.

Cheers.

Reply
0 Kudos
peterc4270
Contributor
Contributor
Jump to solution

Hi,

I think something's not quite correct.

The script runs just fine, but it actually puts the vSwitch name into the network setting, rather than the network name.

Should I be using a different property?

Many thanks for all of your help.

Cheers.

Reply
0 Kudos
peterc4270
Contributor
Contributor
Jump to solution

Hi,

I think I have it ... the vSwitchName variable is the network name, not vSwitch0, 1 etc.

Looks to be working fine now.

Cheers.

Reply
0 Kudos