VMware Cloud Community
zenmike
Contributor
Contributor
Jump to solution

power-on network adapters script

Hello everyone I've got a problem I'm using view planner to deploy some linked clones and my desktops are windows xp 32bit for some reason the

Network adapter e1000 does not tick start up connected and connect on power on when the golden image has these options selected before cloning.

So I have to manually tick these options on the desktops "there is 300 of them" anyone have a script I can run to tick these options so I don't manually

have to do it ?

Thanks hopefully someone has something they can share with me.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-Cluster -Name MyCluster | Get-VM rdclient* | Get-NetworkAdapter | 
where {!$_.ConnectionState.StartConnected} |
Set-NetworkAdapter -Connected:$true -StartConnected:$true -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
18 Replies
LucD
Leadership
Leadership
Jump to solution

Are you sure there are enough free ports on the vSwitch to connect these 300 VM ?

If not, they will not be "connected".


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
zenmike
Contributor
Contributor
Jump to solution

Hi they are distributed across 3 hosts cluster with  248 ports per vSwitch.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I see.

The following can set the Connected and StartConnected tickbox.

Get-VM | Get-NetworkAdapter | 
where {!$_.ConnectionState.StartConnected} |
Set-NetworkAdapter -Connected:$true -StartConnected:$true -Confirm:$false

You should of course further specify which VMs need to be changed by adapting the Get-VM or by inserting a Where-clause


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

zenmike
Contributor
Contributor
Jump to solution

Hey thanks the VM are named rdclient1-300  how would I put that into the script.

So I guess the best way to do this would be to connect to the cluster and find names with

rdclient1-300 and change those tick boxes. can the script do that ?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

Get-Cluster -Name MyCluster | Get-VM rdclient* | Get-NetworkAdapter | 
where {!$_.ConnectionState.StartConnected} |
Set-NetworkAdapter -Connected:$true -StartConnected:$true -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
zenmike
Contributor
Contributor
Jump to solution

That did it I just added the login creds to get connected to the script like this.

$cred = Get-Credential

Connect-VIServer vcenter -Credential $cred

Get-Cluster -Name viewcluster | Get-VM rdclient* | Get-NetworkAdapter |

where {!$_.ConnectionState.StartConnected} |

Set-NetworkAdapter -Connected:$true -StartConnected:$true -Confirm:$false

Reply
0 Kudos
nileshjais
Enthusiast
Enthusiast
Jump to solution

Dear all,

Do we have equivalent util in perl SDK

Thanks.

Reply
0 Kudos
ryanperc77
Contributor
Contributor
Jump to solution

I ran into this problem today.. here is the functioning perl code.  It can be made alot simpler if you want to do it in bulk.

my $vnic_name = "Network adapter 1";

my $vmname = "my_machine";

my $vm_view = Vim::find_entity_view( (view_type => 'VirtualMachine'), filter => { name => $vmname } );

&start_connected($vm_view,$vnic_name);

sub start_connected {

  my $vm_view = shift;

  my $vnic_name = shift;

  my $devices = $vm_view->config->hardware->device;

  my $device;

  foreach my $dev (@$devices) {

    if ($dev->deviceInfo->label eq $vnic_name){

      $device = $dev;

    }

  }

  if($device) {

    my $operation = VirtualDeviceConfigSpecOperation->new('edit');

    if($device->connectable->startConnected == 0) {

      $device->connectable->startConnected(1);

    }

    my $devspec = VirtualDeviceConfigSpec->new(operation => $operation, device => $device);

    my $vmspec = VirtualMachineConfigSpec->new(deviceChange => [ $devspec ]);

    eval {

      $vm_view->ReconfigVM( spec => $vmspec );

    };

    if($@) {

      print "$@\n";

    }

  }

}

Reply
0 Kudos
nileshjais
Enthusiast
Enthusiast
Jump to solution

Thank you, Does this script assumes that VM is powered on but waiting to get connect, so when you run the above script will make VM ready ready to use and check the unchecked box of network adapter.

Reply
0 Kudos
ryanperc77
Contributor
Contributor
Jump to solution

This method can only be used when the machine is powered off.

$device->connectable->startConnected(1); triggers the "Connect at power on"

If you have a guest that is booted and you want to connect a device you would change the code to :

startConnected to connected.

Reply
0 Kudos
nileshjais
Enthusiast
Enthusiast
Jump to solution

Appreciated for quick response.

my $vnic_name = "Network adapter 1";

my $vmname = "my_machine";

my $vm_view = Vim::find_entity_view( (view_type => 'VirtualMachine'), filter => { name => $vmname } );

&start_connected($vm_view,$vnic_name);

sub start_connected {

  my $vm_view = shift;

  my $vnic_name = shift;

  my $devices = $vm_view->config->hardware->device;

  my $device;

  foreach my $dev (@$devices) {

    if ($dev->deviceInfo->label eq $vnic_name){

      $device = $dev;

    }

  }

  if($device) {

    my $operation = VirtualDeviceConfigSpecOperation->new('edit');

   if($device->connectable->connected == 0) {

      $device->connectable->connected(1);

    }

    my $devspec = VirtualDeviceConfigSpec->new(operation => $operation, device => $device);

    my $vmspec = VirtualMachineConfigSpec->new(deviceChange => [ $devspec ]);

    eval {

      $vm_view->ReconfigVM( spec => $vmspec );

    };

    if($@) {

      print "$@\n";

    }

  }

}

Please let me know if the changes above is what you meant.

Reply
0 Kudos
nileshjais
Enthusiast
Enthusiast
Jump to solution

To be more specific, can i do this such while creation of linked VM so as to avoid this multiple firing of two script.

Reply
0 Kudos
ryanperc77
Contributor
Contributor
Jump to solution

That looks to be correct, that is if you have a network device already in the guest that is not connected.

If you want to loop all guests and check if the device is not connected and connect it.. you would done something like this.

my $vnic_name = "Network adapter 1";

my $vm_views = Vim::find_entity_views(view_type => 'VirtualMachine');

foreach my $vm_view (@$vm_views) {

    &start_connected($vm_view,$vnic_name);

}

Now this is very simple and only looks for $vnic_name static.  You can get a lot more complex if you have multiple network interfaces on different port groups.

Reply
0 Kudos
nileshjais
Enthusiast
Enthusiast
Jump to solution

I tried running your script, but it exited silently without any message or i havn't seen any activity of reconfig on vcenter server. Can you please let me know what could be the reason.

I am pasting complete script.

#!/usr/bin/perl -w

use strict;

use warnings;

use FindBin;

use lib "$FindBin::Bin/../";

use lib "/usr/lib/vmware-vcli/apps";

use lib "/opt/vmware-vsphere-cli-distrib/apps";

use VMware::VIRuntime;

use XML::LibXML;

use AppUtil::VMUtil;

use AppUtil::HostUtil;

use AppUtil::XMLInputUtil;

$Util::script_version = "1.0";

sub check_missing_value;

my %opts = (

   vmname => {

      type => "=s",

      help => "The name of the Virtual Machine",

      required => 1,

   },

);

Opts::add_options(%opts);

Opts::parse();

#Opts::validate(\&validate);

Opts::validate();

my $vmname = Opts::get_option('vmname');

Util::connect();

start_connected();

Util::disconnect();

sub start_connected {

    my $vm_view = Vim::find_entity_view( (view_type => 'VirtualMachine'), filter => { 'name' => $vmname } );

    my $devices = $vm_view->config->hardware->device;

    my $vnic_name = "VirtualE1000";

    my $device;

    foreach my $dev (@$devices) {

        if ($dev->deviceInfo->label eq $vnic_name){

            $device = $dev;

        }

    }

    if($device) {

        my $operation = VirtualDeviceConfigSpecOperation->new('edit');

        if($device->connectable->startConnected == 0) {

            $device->connectable->startConnected(1);

        }

    my $devspec = VirtualDeviceConfigSpec->new(operation => $operation, device => $device);
    my $vmspec = VirtualMachineConfigSpec->new(deviceChange => [ $devspec ]);
    eval {
        $vm_view->ReconfigVM( spec => $vmspec );
        Util::trace(0,"Done with reconfig. \n");
    };
    if($@) {
        print "$@\n";
    }
}

}

Reply
0 Kudos
ryanperc77
Contributor
Contributor
Jump to solution

I don't see anything wrong with the script, I would put debug in the function.  IE:

    my $vm_view = Vim::find_entity_view( (view_type => 'VirtualMachine'), filter => { 'name' => $vmname } );

    my $devices = $vm_view->config->hardware->device;

    my $vnic_name = "VirtualE1000";

    my $device;

print "VM found " . $vm_view->name .  "\n";  # HERE

    foreach my $dev (@$devices) {

        if ($dev->deviceInfo->label eq $vnic_name){

print "VM found " . $dev->deviceInfo->label . "\n";   # HERE

            $device = $dev;

        }

    }

.....

This will tell you if you at least got what your looking for, the function definitely works with VC5.0 and the latest sdk.

Reply
0 Kudos
nileshjais
Enthusiast
Enthusiast
Jump to solution

    foreach my $dev (@$devices) {

        if ($dev->deviceInfo->label eq $vnic_name){

print "VM found " . $dev->deviceInfo->label . "\n";   # HERE

            $device = $dev;

             last >>>>>>> should we not do last once we got what we want.

        }

Just wanted to understand if the above should be the case.

Reply
0 Kudos
ryanperc77
Contributor
Contributor
Jump to solution

The script was general based on a single interface label.

the loop will set the last of the matching match labels . ( I don't know how your going to have multiple devices with the same label ??)

Anyway.. I hate to keep posting code but this is the "exact code" I use.

#!/usr/bin/perl -w

use strict;

use warnings;

use VMware::VILib;

use VMware::VIRuntime;

use VMware::VIExt;

Opts::parse();

Opts::validate();

Util::connect();

my $vnic_name = "NETWORK_LABLE";

my $vmname = "VMNAME";

my $vm_view = Vim::find_entity_view((view_type => 'VirtualMachine'), filter => { name => $vmname } );

#print Dumper($vm_view);

#exit;

&start_connected($vm_view,$vnic_name);

sub start_connected {

  my $vm_view = shift;

  my $vnic_name = shift;

  my $new_vm_name = $vm_view->name;

  my $devices = $vm_view->config->hardware->device;

  my $device;

  foreach my $dev (@$devices) {

    if ($dev->deviceInfo->label eq $vnic_name){

      $device = $dev;

    }

  }

  if($device) {

    debug("start_connected: host -> $new_vm_name checking device $vnic_name");

    my $operation = VirtualDeviceConfigSpecOperation->new('edit');

    if($device->connectable->connected == 0) {

      debug("start_connected: host -> $new_vm_name not started, starting");

      $device->connectable->connected(1);

      my $devspec = VirtualDeviceConfigSpec->new(operation => $operation, device => $device);

      my $vmspec = VirtualMachineConfigSpec->new(deviceChange => [ $devspec ]);

      eval {

        $vm_view->ReconfigVM( spec => $vmspec );

      };

      if($@) {

        debug("start_connected: error $@");

      }

    }

    else {

      debug("start_connected: host -> $new_vm_name already started");

    }

  }

}

sub debug {

  my $in = shift;

  print "$in\n";

}

You will need to set the

my $vnic_name = "NETWORK_LABLE";

my $vmname = "VMNAME";

You can get this from Dumper if you uncomment the lines below.

#print Dumper($vm_view);

#exit;

Reply
0 Kudos
nileshjais
Enthusiast
Enthusiast
Jump to solution

After doing the below changes

foreach my $dev (@$devices) {
    print " $dev->deviceInfo->label . \n";
    if ( ref $dev eq "$vnic_name" ){
        $device = $dev;
        last;
    }
}

I was able to do it, Really appreciated for your help and patiently responding my queries. You are fantastic.

Reply
0 Kudos