VMware Cloud Community
jcouch
Enthusiast
Enthusiast
Jump to solution

Need script to enable "Check and upgradetools during power cycling" on all VMs

What is the best method for enabling this option on all VMs? Is there a good way to do it without either editing the VMX files on every machine or doing it manually though edit settings?

Tags (1)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I would do that this way

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"

Get-View -ViewType VirtualMachine | %{
   $_.ReconfigVM($vmConfigSpec)
} 

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

Reply
0 Kudos
61 Replies
LucD
Leadership
Leadership
Jump to solution

I would do that this way

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"

Get-View -ViewType VirtualMachine | %{
   $_.ReconfigVM($vmConfigSpec)
} 

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
jcouch
Enthusiast
Enthusiast
Jump to solution

You are the Master LucD! This works great.

Reply
0 Kudos
elitekato
Contributor
Contributor
Jump to solution

Please kindly explain how I can apply this script to all VMs then I would be very appreciated.

Never managed using script before.

Thank you.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The sample script in my previous answr is against all guests.

The line

Get-View -ViewType VirtualMachine

will retrieve all virtual machines in your vCenter or ESX(i) server

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
elitekato
Contributor
Contributor
Jump to solution

Dear LucD,

Thank you for your reply first of all.

I guess my question is where and how do I apply this script?

I have vCenter 4.1 and 11 ESX 4.1 servers in production cluster.

Putty into each ESX server and runnig this script is all I can think of, but doing it gave me an error message - Bash: Get-View: command not found.

Thank you for your help.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You run this script from a Windows machine where you have PowerShell and PowerCLI installed.

You use the Connect-VIServer cmdlet to set up the connection to your vCenter server and then you execute the PowerCLI script from above.

A PowerCLI script doesn't run in the COS of an ESX server but on any Windows client that can connect to your vCenter server.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
elitekato
Contributor
Contributor
Jump to solution

Got it. Make sense to me now.

Thank you very much LucD.

Warm regard,

 

 

Reply
0 Kudos
jcouch
Enthusiast
Enthusiast
Jump to solution

Luc,

Powercli Novice here...

I have played around with this script some and have another question for you... How would query for the status of this option?

I want to report on it so I can see which ones need to be change, then I would like to pipe the results of that query to the command so that it only updates the option on those VMs that do not meet the speficied option (manual or UpgradeAtPowercycle).

Thanks!

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Ok, to query the value you can do

Get-VM | Select Name,@{N="UpgradePolicy";E={$_.Extensiondata.Config.Tools.toolsUpgradePolicy}}

To filter only the guests that do not have manual or UpgradeAtPowercycle you can use the Where-Object cmdlet.

The conditions uses the -notcontains operator.

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"

Get-View -ViewType VirtualMachine | where{"manual","UpgradeAtPowercycle" -notcontains $_.Config.Tools.toolsUpgradePolicy} | %{
   $_.ReconfigVM($vmConfigSpec)
} 

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
jcouch
Enthusiast
Enthusiast
Jump to solution

Thank you! Works great.

Reply
0 Kudos
vmkeef
Contributor
Contributor
Jump to solution

Really nice script ! How could I apply this to a cluster at a time rather than a whole datacenter?

Reply
0 Kudos
Troy_Clavell
Immortal
Immortal
Jump to solution

How could I apply this to a cluster at a time rather than a whole datacenter?

here's what I use

$clusterName = "Cluster1"
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"

Get-Cluster -Name $clusterName | Get-VM | %{
	$_.Extensiondata.ReconfigVM($vmConfigSpec)
}

Reply
0 Kudos
jcouch
Enthusiast
Enthusiast
Jump to solution

Here is what I did with it after Luc gave me the base code. I needed a way to enable/disable this option for patching time. We noticed it got in the way of windows patching when multiple reboots were required. It will ask you for what cluster and if you want to enable or disable the VMTools options.

  1. ==============================================================================================

  2. NAME: ChangeVMToolsOptions.ps1

  3. AUTHOR: Jeff Couch

  4. DATE : 8/23/2010

  5. COMMENT: Base Code author LUCD

  6. ==============================================================================================

  1. Connect to Vcenter

$vCenter = Read-Host "Enter vCenter Server name"

Connect-VIServer $vCenter

  1. Determine Options

Write-Host "Name of Cluster: " -nonewline; $Clustername = Read-Host

Write-Host "Enable or Disable Auto Upgrade VM Tools?"

Write-Host "Answer: " -nonewline; $Option = Read-Host

if ($Option -match "Enable"){$status = "UpgradeAtPowerCycle"}

else {$status = "Manual"}

Write-Host "This will $status all vmtools Auto upgrade on the $Clustername."

Write-Host "Are you sure that you want to Procede? "

Write-Host "[Y] Yes " -nonewline; Write-Host "[N] No " -foregroundcolor yellow -nonewline; $continue = Read-Host "(default is "N")"

if ($continue -match "y")

{

  1. Set SDK Parms

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo

$vmConfigSpec.Tools.ToolsUpgradePolicy = "$status" #change to "manual" to disable auto upgrade of Tools

  1. Execute Command

get-vm -Location "$Clustername" | get-view | %{$_.ReconfigVM($vmConfigSpec)}

}

else

{

Write-Host "Script Ended Without Execution"

}

Jeff

Reply
0 Kudos
vmkeef
Contributor
Contributor
Jump to solution

Thanks Troy, that worked perfectly !!

Reply
0 Kudos
vmkeef
Contributor
Contributor
Jump to solution

jcouch, I'll give your script a go as well. That would really help when patching our clusters also. Many Thanks!!

Reply
0 Kudos
kgilbert
Contributor
Contributor
Jump to solution

luc: you rock!

saved my right finger from certain death clicking 1000x!

Smiley Happy

ken

Reply
0 Kudos
NolaNick
Contributor
Contributor
Jump to solution

This couldn't have come at a better time.

Many thanks to Luc and the OP...this is going to come in real handy next maintenance weekend.

Psyched!

Reply
0 Kudos
jhjkempen
Contributor
Contributor
Jump to solution

did some puzzling as i never used PowerCli / PowerShell,

when i run the script of LucD for every machine that is being set get this message:

Exception calling "ReconfigVM" with "1" argument(s): "fault.RestrictedVersion.s
ummary"
At C:\script.ps1:6 char:17
+    $_.ReconfigVM <<<< ($vmConfigSpec)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

My script looks like this:

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"

Get-View -ViewType VirtualMachine | %{
   $_.ReconfigVM($vmConfigSpec)
}

Any ideas?         

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

From the error message I guess you are trying to do this on a 'free' ESXi server.

I'm afraid you only can only read on those but not write, or change anything with PowerCLI


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

Reply
0 Kudos