2012-03-05

Set-UpdateToolsPolicy–For your VM’s

In continuation to William Lam’s post on how you can Automating VMware Tools Upgrade Policy and thanks to a comment left on the post – I wanted to perform the same functionality in PowerCLI.

I present to you the Set-UpdateToolsPolicy Function

Function Set-UpdateToolsPolicy {
	<#
		.SYNOPSIS
			A function to change the update policy one or more VM's 
		.DESCRIPTION
			This script will change the VMware Tools Update policy 
			on one or many virtual machines
		.PARAMETER  VM
			The name of one or more virtual machines, this can be 
			passed from the pipeline
		.PARAMETER  Policy
			The policy setting - has to be be either manual (do not try 
			and upgrade tools on each boot cycle) or upgradeAtPowerCycle 
			(tools will be checked for upgrade at each power cycle)
		.EXAMPLE
			PS C:\> Get-VM foo | Set-UpdateToolsPolicy -Policy manual
		.EXAMPLE
			PS C:\> Set-UpdateToolsPolicy -VM foo -Policy upgradeAtPowerCycle
		.NOTES
			Author: Maish Saidel-Keesing
		.LINK
			http://technodrone.blogspot.com/2012/03/set-updatetoolspolicyfor-your-vms.html

	#>
	[CmdletBinding()]
	Param(
	[Parameter(Position=0,Mandatory=$True,ValueFromPipeline=$True)]
	[String]
	$VM,

	[ValidateSet("manual","upgradeAtPowerCycle")]
	[String]
	$Policy
	)
    
	begin {
	$config = New-Object VMware.Vim.VirtualMachineConfigSpec
	$config.Tools = New-Object VMware.Vim.ToolsConfigInfo
	$config.Tools.ToolsUpgradePolicy = $Policy
	}	
    Process
    {
        foreach ($vmobject in (Get-VM $VM)) {
			$vmobject.ExtensionData.ReconfigVM($config)
		}
    } 
}
Line 31: Using [ValidateSet("manual","upgradeAtPowerCycle")] as part of the definition of the parameter means that is can only be one of these two options – this is much easier than doing a switch on the variable.

Line 36-40: This part is the same for all of the VM’s that will be processed – that is why it is part of the processed at the beginning of the script.

I would also like to thank Damian Karlson for his post on this subject which was helpful.

The reason why I wrote this small function .. - well that is for another post.

As usual your comments are always welcome.