Must check the Network card type too. If your esxi host is 5.5 den you must change all those network card to "vmxnet3" Type
Here is an perl script which can help you out.
You need to set the startConnected as well as connected in the connectable property for the device to handle both poweredOn and poweredOff states of a VM.
- #!/usr/bin/perl -w
- use strict;
- use warnings;
- use VMware::VIRuntime;
- my %opts = (
- network => {
- type => "=s",
- help => "Network name",
- required =>0,
- default => "Network adapter 1",
- },
- operation => {
- type => "=s",
- help => "connect or disconnect",
- required => 0,
- default => "connect",
- },
- vmname => {
- type => "=s",
- help => "Name of virtual machine",
- required => 1
- }
- );
- Opts::add_options(%opts);
- Opts::parse();
- Opts::validate();
- Util::connect();
- my ($vm_view, $network, $operation, @vmnics, $vmnic, $vm_spec, $dev_spec);
- $vm_view = Vim::find_entity_view(view_type => 'VirtualMachine',
- filter => {'name' => Opts::get_option('vmname') },
- properties => ['name', 'config.hardware.device'] );
- die "failed to locate virtual machine '" . Opts::get_option('vmname') . "'" unless $vm_view;
-
- $network = Opts::get_option('network');
- $operation = Opts::get_option ('operation');
- # Get list of vmnics from the virtual machine config.hardware.device array
- @vmnics = grep { $_->isa("VirtualEthernetCard") } @{$vm_view->{'config.hardware.device'} || []};
- # Find the vmnic by device label
- ($vmnic) = grep { $_->{'deviceInfo'}{'label'} } @vmnics;
- die "failed to locate virtual machine nic by label '$network'" unless $vmnic;
- # Set the connectable flag for the vmnic
- if ($operation =~ m/^connect$/) {
- $vmnic->{'connectable'}{'connected'} = 1;
- $vmnic->{'connectable'}{'startConnected'} = 1;
- } elsif ($operation =~ m/^disconnect$/) {
- $vmnic->{'connectable'}{'connected'} = 0;
- $vmnic->{'connectable'}{'startConnected'} = 0;
- } else {
- die "operation must be 'connect' or 'disconnect'";
- }
- # Create reconfigure spec
- $vm_spec = new VirtualMachineConfigSpec();
- # Create deviceChange spec
- $dev_spec = new VirtualDeviceConfigSpec( device => $vmnic );
- $dev_spec->{'operation'} = new VirtualDeviceConfigSpecOperation('edit');
- $vm_spec->{'deviceChange'} = [ $dev_spec ];
- # Call ReconfigVM. Use synchronous version
- $vm_view->ReconfigVM(spec => $vm_spec);
- print "Virtual machine reconfigured\n";
- Util::disconnect();
$ perl eran.pl --username=administrator --password=VMware1! --server=172.16.254.10 --vmname=TestUUID --network="Network adapter 1" --operation=connectVirtual machine reconfigure
Please consider marking this answer "correct" or "helpful" if you found it useful.
Anjani Kumar | VMware vExpert 2014-2015-2016 | Infrastructure Specialist
Twitter : @anjaniyadav85
Website : http://www.Vmwareminds.com