VMware {code} Community
rlaborius
Contributor
Contributor

Change Nic state (connect/disconnect) for VM in perl

Hi,

can anyonone give some hints, how to change following nic states of a vm.

- connect / disconnect

- connect on start

I have written following but have no success

sub net_state {
  my $vnic_device;
  my $state = Opts::get_option('state');
  my $vmname = Opts::get_option('vmname');
  my $vnic = Opts::get_option('vnic');
  my $vm_view  = Vim::find_entity_view (view_type => 'VirtualMachine', filter =>{ 'name'=> $vmname});
  if ($vm_view) {
    my $devices = $vm_view->config->hardware->device;
    my $vnic_name = "Network adapter $vnic";
    foreach my $device (@$devices) {
                if ($device->deviceInfo->label eq $vnic_name){
                        $vnic_device=$device;
                }
        }
    my $config_spec_operation = VirtualDeviceConfigSpecOperation->new('edit');
    my $devspec = VirtualDeviceConfigSpec->new(operation => $config_spec_operation, device => $vnic_name);
    my $device_config_specs = [$devspec];
    my $vmspec = VirtualMachineConfigSpec->new(deviceChange => $device_config_specs);
      $vm_view->ReconfigVM( spec => $vmspec );
   print Dumper($vnic_device);
  }

Thanks

Ronny

0 Kudos
1 Reply
lamw
Community Manager
Community Manager

0 Kudos