After hearing about the bug with ESX 3.5 Update 3 where a vMotion would cause a tools upgrade, and consequentially a VM reboot, I wanted to check and verify that my VMs would not exhibit this behavior. Apparently the bug is present when the tools update policy is set to “Update at Power On”. (Yes, I admit this bug has been known for a while, I just kept forgetting to post this script.)
There are a multitude of other scripts (including the powershell cmdlet “update-tools”) out there to kickoff a tools update task, so I wasn’t interested in that, all I wanted was to know the update policy and be able to change it.
This perl is the result of that…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
#!/usr/bin/perl -w # # cfg-vmtools-upgrade.pl - written by Andrew Sullivan, 2008-12-30, http://www.get-admin.com # # Report bugs and request improvements at http://get-admin.com/blog/?p=279 # # This script does one of two things when executed: # 1) reports on the upgrade policy for the VMware tools in a VM # and reports on the tools status (up-to-date, out-of-date, etc). # 2) modifies the upgrade policy to one of the two valid values - manual # or upgradeAtPowerCycle if --update is specified # # I created this script in response to a bug that was discovered in ESX 3.5 U3 where a # vMotion will trigger a tools upgrade, causing the VM to reboot itself. See # http://kb.vmware.com/kb/1007501 for more information. # # Example usage: # Show policy and status for all VMs in vCenter # ./cfg-vmtools-upgrade.pl --server your.vc.server # # Show policy and status for only certain VMs # ./cfg-vmtools-upgrade.pl --server your.vc.server --vm vm1,vm2,vm3 # ./cfg-vmtools-upgrade.pl --server your.vc.server --vm vm4 # # Change the policy for all VMs to manual # ./cfg-vmtools-upgrade.pl --server your.vc.server --update # # Change the policy for one VM to powercycle # ./cfg-vmtools-upgrade.pl --server your.vc.server --update --vm vm5 --policy powercycle # use strict; use warnings; use FindBin; use lib "$FindBin::Bin/../"; use VMware::VIRuntime; # add some additional options my %opts = ( 'policy' => { type => ":s", help => "The upgrade policy for VMware Tools on the VM. Valid values are 'manual'" . "and 'powercycle'.", required => 0, default => "manual" }, 'vm' => { type => ":s", help => "The VM, or VMs, to apply the new setting to. If multiple VMs are to be " . "modified, separate them with commas. If this option is not specified," . " it will default to all VMs on the host.", required => 0, default => "all" }, 'update' => { type => ":s", help => "If provided, will update the current policy for each VM specified (or " . "all VMs). If not provided, will display the current policy and status.", required => 0, default => "false" # if --update is provided at the command line it is an empty string when retrieved # via Opts::get_option. This makes it easy to check and see if it was passed, cause if # it's not present at all, then it is set to the default above. } ); Opts::add_options(%opts); Opts::parse(); Opts::validate(&validate); Util::connect(); chomp( my $provided_vm_list = Opts::get_option('vm') ); my @VMs; if ($provided_vm_list =~ m/,/) { my @vmList = split(/,/, $provided_vm_list); foreach (@vmList) { my $virtualMachine = Vim::find_entity_view( view_type => 'VirtualMachine', filter => { 'name' => qr/($_)/i } ); if ($virtualMachine) { push(@VMs, $virtualMachine); } } } elsif ($provided_vm_list eq "all") { my $virtualMachines = Vim::find_entity_views( view_type => 'VirtualMachine' ); foreach (@$virtualMachines) { push(@VMs, $_); } } else { my $virtualMachine = Vim::find_entity_view( view_type => 'VirtualMachine', filter => { 'name' => qr/($provided_vm_list)/i } ); push(@VMs, $virtualMachine); } # expand the shorthand "powercycle" to the valid value. # "manual" is the correct value to provide to the operation, so we don't have to # worry about it. my $policy = Opts::get_option('policy'); if ($policy eq "powercycle") { $policy = "upgradeAtPowerCycle"; } foreach (@VMs) { if (Opts::get_option('update') eq "false") { print $_->name . ": " . "ntUpgrade Policy: " . $_->config->tools->toolsUpgradePolicy; if (defined($_->summary->guest->toolsStatus)) { print "ntTools Status: " . $_->summary->guest->toolsStatus->val . "n"; } else { print "ntTools Status: Unknownn"; } } else { eval { my $toolsConfigInfo = ToolsConfigInfo->new( 'toolsUpgradePolicy' => $policy ); my $configSpec = VirtualMachineConfigSpec->new( 'tools' => $toolsConfigInfo ); # blocking method, which should give us an error in the below section... $_->ReconfigVM( 'spec' => $configSpec ); }; # do some error checking if ($@) { Util::trace(0, "Error: " . $@ . "n"); } } } Util::disconnect(); sub validate { my $policy = Opts::get_option('policy'); my $valid = 0; if ($policy eq "manual" || $policy eq "powercycle") { $valid = 1; } return $valid; } |
cool thanks for helping me thanks! 😀
Hi,
I tried out your script but I’m getting this message at execution:
Can’t modify non-lvalue subroutine call in chomp at CheckVMwareToolsUpgradePolicy.pl line 78, near “))”
Found some perl threats about this issue, but could not really do something with the solutions…
Thx for your help.
Regards Oliver
Hi again,
after removing chomp and the () it works like a charm, thx a lot…
Regards
Oliver
Hello Oliver,
It looks like I borked the usage of
chomp
there. Chomp returns a the number of characters that it removed from the string, so it shouldn’t have worked at all. (For reference, the context I have above is how PHP’s trim command works.)By changing line 78 to look like the following, it should work:
Sorry for the confusion.
Andrew
Hi,
Could be possible can get virtual machine tools version.
Chan