Update VMware Tools on Windows at powercycle in Powershell

jmachado81
jmachado81 used Ask the Experts™
on
Is there a way to change all windows VMs to upgrade VMware Tools at power cycle?  This is a great site that shows how to change it for ALL VMs, but i just want to do the Windows from guestID or guestFamily.  We have alot of appliances and non-windows systems where the guest manages the tools.

http://www.baylyparker.com/2017/01/powercli-script-for-time-vmtools/ 

#Auto Update VMware Tools on power cycle on an individual VM
$VM = Get-VM $VMName | Get-View
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
$VM.ReconfigVM($vmConfigSpec)
======================================================================
#Auto Update VMware Tools on power cycle for ALL VMs
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
Get-View -ViewType VirtualMachine | % {$_.ReconfigVM($vmConfigSpec) }

#or
Foreach ($v in (get-vm)) {
$vm = $v | Get-View
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
$vm.ReconfigVM($vmConfigSpec)
}
=======================================================================
#To get the current policy
Get-View -ViewType virtualmachine | select name,@{N='ToolsUpgradePolicy';E={$_.Config.Tools.ToolsUpgradePolicy } }
=======================================================================

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014
Commented:
There's the difference between what is reported by vmtools and what was configured when the VM was created (which doesn't have to be accurate).  So you would probably have to choose which criteria you want to go by.  The guestId from vmtools should be accurate, but isn't always present.

Example showing how the guestId may be different.
Get-VM | % { $vm = $_; $vm | Get-View | Select name,@{n="guestid";e={$_.guest.guestid}},@{n="guestfamily";e={$_.guest.guestfamily}},@{n="guestid2";e={$vm.guestid}} }

Open in new window


I believe either of the following should work (not tested).

Using the vmtools info:
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
Get-View -ViewType VirtualMachine |
 Where { $_.guest.guestid -like "windows*"} | 
 % {$_.ReconfigVM($vmConfigSpec) }

Open in new window


Using the config info:
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
Get-VM |
 Where { $_.guestid -like "windows*" } |
 % {$_.ReconfigVM($vmConfigSpec) }

Open in new window

Author

Commented:
I tested against my lab with appliances and window VMs and the VMTools info code worked.  I received errors on the config info code.

Thank you for this footech.

Author

Commented:
#To get the current policy of all VMs in the vCenter
Get-VM | % { $vm = $_; $vm | Get-View | Select name,@{N='ToolsUpgradePolicy';E={$_.Config.Tools.ToolsUpgradePolicy} },@{n="guestfamily";e={$_.guest.guestfamily}},@{n="guestid";e={$_.guest.guestid}} }

Open in new window

Top Expert 2014

Commented:
You may not need it, but here's a correction to the config info code that should work:
Get-VM |
 Where { $_.guestid -like "windows*" } | Get-View |
 % {$_.ReconfigVM($vmConfigSpec) }

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial