Skip to content

Instantly share code, notes, and snippets.

@jbirley
Last active March 8, 2018 00:13
Show Gist options
  • Save jbirley/7529a1ae3f01cacff2975dc143a35096 to your computer and use it in GitHub Desktop.
Save jbirley/7529a1ae3f01cacff2975dc143a35096 to your computer and use it in GitHub Desktop.
A PowerShell function that sets the VM Tools Upgrade Policy on a VM. This function assumes you are using PowerCLI modules and are connected to a vCenter.
#Requires -Version 3.0
function Set-VMToolsUpgradePolicy {
<#
.SYNOPSIS
Updates the Tools Upgrade Policy on VMware VM.
.DESCRIPTION
This function uses get-view to get a managed object reference to a VM, and updates its 'ToolsUpgradePolicy'
.PARAMETER VM
One or more valid vSphere VM objects.
.PARAMETER Setting
This parameter can be either "manual", "upgradeAtPowerCycle"
.EXAMPLE
get-vm | where name -like "myvm" | Set-VMToolsUpgradePolicy -Setting manual -verbose
.NOTES
Author: Jim Birley
#>
[CmdletBinding()]
[OutputType('PSCustomObject')]
param (
[Parameter(Mandatory,
ValueFromPipeline)]
[PSCustomObject[]]$VM,
[Parameter(Mandatory)]
[ValidateSet("manual", "upgradeAtPowerCycle")]
[string]$Setting
)
BEGIN { }
PROCESS {
foreach ($MyVM in $VM) {
Write-Verbose "Setting Tools Upgrade Policy on $($MyVM.Name) to $Setting"
$MoRef = $MyVM | Get-View -verbose:$false
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = $Setting
$MoRef.ReconfigVM($vmConfigSpec)
$MyVM | Select-Object Name, @{N='ToolsUpgradePolicy';E={($_ | get-view -Verbose:$false).Config.Tools.ToolsUpgradePolicy}}
}
}
END { }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment