VMware Cloud Community
sanjaygpt171
Enthusiast
Enthusiast
Jump to solution

checking box "Automating Upgrade of VMware Tools" for all windows VM created in last 7 days

I want to check the box  "Automating Upgrade of VMware Tools" for all windows VM created in last 7 days.  Currenlt , i have following script from which i was providing manual VM name but i need to automate it only for windows VM

foreach($vmlist in (Get-Content -Path C:\TEMP\vmliste.txt))

{

$vm = Get-VM -Name $vmlist

 

 # $VM = Get-VM # Filter here if needed

  $VMView = $VM | Get-View

    if($VMview.config.tools.toolsUpgradePolicy -ne "upgradeAtPowerCycle")

  {

    $spec = New-Object VMware.Vim.VirtualMachineConfigSpec

    $spec.changeVersion = $VM.ExtensionData.Config.ChangeVersion

    $spec.tools = New-Object VMware.Vim.ToolsConfigInfo

    $spec.tools.ToolsUpgradePolicy = "upgradeAtPowerCycle"

    $_this = Get-View -Id $VM.Id

    $_this.ReconfigVM_Task($spec)

  Write-Host "Update Tools Policy on $vm completed"

 }

  else

  {

  Write-Host "Update Tools Policy on $vm already esxists"

  }

   }

 

foreach($vmlist in (Get-Content -Path C:\TEMP\vmliste.txt))
{
$vm = Get-VM -Name $vmlist
 # $VM = Get-VM # Filter here if needed
  $VMView = $VM | Get-View
  if($VMview.config.tools.toolsUpgradePolicy -ne "upgradeAtPowerCycle")
  {
    $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $spec.changeVersion = $VM.ExtensionData.Config.ChangeVersion
    $spec.tools = New-Object VMware.Vim.ToolsConfigInfo
    $spec.tools.ToolsUpgradePolicy = "upgradeAtPowerCycle"
    $_this = Get-View -Id $VM.Id
    $_this.ReconfigVM_Task($spec)
Write-Host "Update Tools Policy on $vm completed"
  }
  
  else
  {
  Write-Host "Update Tools Policy on $vm failed"
  }
  foreach($vmlist in (Get-Content -Path C:\TEMP\vmliste.txt))
{
$vm = Get-VM -Name $vmlist
 
 # $VM = Get-VM # Filter here if needed
  $VMView = $VM | Get-View
    if($VMview.config.tools.toolsUpgradePolicy -ne "upgradeAtPowerCycle")
  {
    $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $spec.changeVersion = $VM.ExtensionData.Config.ChangeVersion
    $spec.tools = New-Object VMware.Vim.ToolsConfigInfo
    $spec.tools.ToolsUpgradePolicy = "upgradeAtPowerCycle"
    $_this = Get-View -Id $VM.Id
    $_this.ReconfigVM_Task($spec)
Write-Host "Update Tools Policy on $vm completed"
 
  }
  
  else
  {
  Write-Host "Update Tools Policy on $vm already esxists"
  }
  $VMview = ""
 }
23 Replies
sanjaygpt171
Enthusiast
Enthusiast
Jump to solution

  1. Hi LuCD,
  2. For some VM's , i am getting below errors and script fails in middle with below error. Do we need to adjust something in script to work ? or need to add something more, so that it can skip this error and move to next VM for performing changes.

VimException,Microsoft.PowerShell.Commands.ForEachObjectCommand
Exception calling "ReconfigVM_Task" with "1" argument(s): "The method is disabled by 'horizon.daas'"

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You didn't mention there were some Horizon View stations in there.
Horizon disables that method since it wants to control the stations it manages.
The best way to upgrade those is to use the Horizon functionality.
Make a new Master or a Snapshot with the correct HW version.


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

0 Kudos
sanjaygpt171
Enthusiast
Enthusiast
Jump to solution

Hi @LucD ,

 

Is there a way to filter out VM's Horizon View Station VM's in the code itself, so it won't execute on those VM's.

 

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you have the VMware.HV.Helper module, you can use that to retrieve all Horizon VMs.
With the help of such a list you can exclude those VMs.


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

0 Kudos