jcouch
Enthusiast
Enthusiast

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

Jump to solution

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

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

0 Kudos
59 Replies
LucD
Leadership
Leadership

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

0 Kudos
jcouch
Enthusiast
Enthusiast

You are the Master LucD! This works great.

0 Kudos
elitekato
Contributor
Contributor

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.

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
elitekato
Contributor
Contributor

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.

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
elitekato
Contributor
Contributor

Got it. Make sense to me now.

Thank you very much LucD.

Warm regard,

 

 

0 Kudos
jcouch
Enthusiast
Enthusiast

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!

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos
jcouch
Enthusiast
Enthusiast

Thank you! Works great.

0 Kudos
vmkeef
Contributor
Contributor

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

0 Kudos
Troy_Clavell
Immortal
Immortal

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)
}

0 Kudos
jcouch
Enthusiast
Enthusiast

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

0 Kudos
vmkeef
Contributor
Contributor

Thanks Troy, that worked perfectly !!

0 Kudos
vmkeef
Contributor
Contributor

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

0 Kudos
kgilbert
Contributor
Contributor

luc: you rock!

saved my right finger from certain death clicking 1000x!

Smiley Happy

ken

0 Kudos
NolaNick
Contributor
Contributor

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!

0 Kudos
jhjkempen
Contributor
Contributor

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?         

0 Kudos
LucD
Leadership
Leadership

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

0 Kudos