VMware Cloud Community
htwnrver
Enthusiast
Enthusiast
Jump to solution

Schedule VM Compatibility Upgrade

Does anyone know how to check this box via Powercli?  This is new to 5.1 web client.

Schedule VM Compatibility Upgrade

Thanks,

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Afaik there is no public method for that, and there definitely is no cmdlet for it.

Upgrading the virtual HW of VM is quite easy with the UpgradeVM_Task method.

And creating a scheduled task in vCenter that calls this method isn't too difficult either.

But scheduling the upgrade during the next restart of the VM is not so obvious I'm afraid.


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

View solution in original post

Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

Afaik there is no public method for that, and there definitely is no cmdlet for it.

Upgrading the virtual HW of VM is quite easy with the UpgradeVM_Task method.

And creating a scheduled task in vCenter that calls this method isn't too difficult either.

But scheduling the upgrade during the next restart of the VM is not so obvious I'm afraid.


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

Reply
0 Kudos
htwnrver
Enthusiast
Enthusiast
Jump to solution

Thanks!  my current scripts power down the VM and then upgrade but that seemed like a shortcut by just requiring a soft reboot.

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

You can schedule VM compatibility upgrade with PowerCLI. The following PowerCLI script will schedule a vm called MyVM to be compatible with "ESXi 5.1 and later" and it will enable "Only upgrade after normal guest OS shutdown".

$vm = Get-VM -Name MyVM
$vm.ExtensionData.Config.ScheduledHardwareUpgradeInfo
$spec = New-Object -TypeName VMware.Vim.VirtualMachineConfigSpec
$spec.ScheduledHardwareUpgradeInfo = New-Object -TypeName VMware.Vim.ScheduledHardwareUpgradeInfo
$spec.ScheduledHardwareUpgradeInfo.UpgradePolicy = "onSoftPowerOff"
$spec.ScheduledHardwareUpgradeInfo.VersionKey = "vmx-09"
$spec.ScheduledHardwareUpgradeInfo.ScheduledHardwareUpgradeStatus = "pending"
$vm.ExtensionData.ReconfigVM_Task($spec)

You can  check the scheduled VM compatibility upgrade for all your vm's with:

Get-VM |
Select-Object -Property Name,
@{N="UpgradePolicy";E={$_.ExtensionData.Config.ScheduledHardwareUpgradeInfo.UpgradePolicy}},
@{N="VersionKey";E={$_.ExtensionData.Config.ScheduledHardwareUpgradeInfo.VersionKey}},
@{N="ScheduledHardwareUpgradeStatus";E={$_.ExtensionData.Config.ScheduledHardwareUpgradeInfo.ScheduledHardwareUpgradeStatus}},
@{N="Fault";E={$_.ExtensionData.Config.ScheduledHardwareUpgradeInfo.Fault}},
@{N="DynamicType";E={$_.ExtensionData.Config.ScheduledHardwareUpgradeInfo.DynamicType}},
@{N="DynamicProperty";E={$_.ExtensionData.Config.ScheduledHardwareUpgradeInfo.DynamicProperty}}|
Format-Table

Message was edited by: RvdNieuwendijk Added the screenshot

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
LucD
Leadership
Leadership
Jump to solution

I stand corrected, but what is the purpose of the 2nd line in your first script ?

And are you sure you need to provide a value for the scheduledHardwareUpgradeStatus property ?


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

Reply
0 Kudos
htwnrver
Enthusiast
Enthusiast
Jump to solution

that works!  Thanks!

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The second line in the first script was a result of debugging and is not needed.

Update:

I did some more testing and you don't have to provide a value for the scheduledHardwareUpgradeStatus property.

So here is the new shorter version of the script:

$vm = Get-VM -Name MyVM
$spec = New-Object -TypeName VMware.Vim.VirtualMachineConfigSpec
$spec.ScheduledHardwareUpgradeInfo = New-Object -TypeName VMware.Vim.ScheduledHardwareUpgradeInfo
$spec.ScheduledHardwareUpgradeInfo.UpgradePolicy = "onSoftPowerOff"
$spec.ScheduledHardwareUpgradeInfo.VersionKey = "vmx-09"
$vm.ExtensionData.ReconfigVM_Task($spec)

Other possible values for UpgradePolicy are: "always" and "never".

Message was edited by: RvdNieuwendijk

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
markdjones82
Expert
Expert
Jump to solution

RVD,

  Just reading through this and I was wondering how you obtained all the code for this.  Did you just browse through the API's and do it that way or use smething like onyx?

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
Reply
0 Kudos
b1naryagent
Contributor
Contributor
Jump to solution

This method does not work. I tested the code and after two restarts the VM's would not upgrade. The VM must be powered down for the upgrade to occur.

The documentation clearly states, onSoftPowerOff - Run scheduled upgrades only on normal guest OS shutdown.

Reply
0 Kudos
b1naryagent
Contributor
Contributor
Jump to solution

Actually, with further testing we discovered that using the parameter "always" rather than "onSoftPowerOff" does cause the hardware to be upgraded on a reboot cycle and does not require a power down cycle. Here's the updated code:

$vm = Get-VM -Name MyVM
$spec = New-Object -TypeName VMware.Vim.VirtualMachineConfigSpec
$spec.ScheduledHardwareUpgradeInfo = New-Object -TypeName VMware.Vim.ScheduledHardwareUpgradeInfo
$spec.ScheduledHardwareUpgradeInfo.UpgradePolicy = "always"
$spec.ScheduledHardwareUpgradeInfo.VersionKey = "vmx-10"
$vm.ExtensionData.ReconfigVM_Task($spec)


Also, if you want to upgrade tools and hardware in the same reboot cycle here is some code that will do that but will only upgrade hardware on VM's that complete the tools upgrade successfully. I borrowed from LucD's task processing code to monitor the background async tasks:


$vms = get-content ./vm_upgrades.txt

function Upgd-Hdwr($vm){

  $spec = New-Object -TypeName VMware.Vim.VirtualMachineConfigSpec

  $spec.ScheduledHardwareUpgradeInfo = New-Object -TypeName VMware.Vim.ScheduledHardwareUpgradeInfo

  $spec.ScheduledHardwareUpgradeInfo.UpgradePolicy = "always"

  $spec.ScheduledHardwareUpgradeInfo.VersionKey = "vmx-10"

  $vm.ExtensionData.ReconfigVM_Task($spec)

}

$taskTab = @{}

foreach($vm in $vms){

  $taskTab[(Update-Tools -vm (Get-VM $vm) -NoReboot -RunAsync).Id] = $vm

}

$runningTasks = $taskTab.Count

while($runningTasks -gt 0){

  Get-Task | %{

  if($taskTab.ContainsKey($_.Id) -and $_.State -eq "Success"){

  Write-Host "VMtools on $($taskTab[$_.Id]) was upgraded successfully. Scheduling virtual hardware upgrade..."

  add-content ./tools_success.txt -value $($taskTab[$_.Id])

  Upgd-Hdwr (Get-VM $taskTab[$_.Id])

  $taskTab.Remove($_.Id)

  $runningTasks--

  }

  elseif($taskTab.ContainsKey($_.Id) -and $_.State -eq "Error"){

  write-Warning "There was a problem with $($taskTab[$_.Id]), please check the VMtools upgrade manually."

  add-content ./tools_fail.txt -value $($taskTab[$_.Id])

  $taskTab.Remove($_.Id)

  $runningTasks--

  }

  }

  Start-Sleep -Seconds 15

}

Reply
0 Kudos
RichardJohnsonR
Contributor
Contributor
Jump to solution

Hi

Im using your script to update the hardware.  That works fine.  But i want to be able to set it back to "never" at the end of the servers patch window as i dont want the servers auto updating if they get restarted outside of the regular maintenance windows.  I am using the below and Powershell shows me a task ID is created.  If i look at the variables afterwards they havent changed, but if i look in the vCenter Web Client, the check box is no longer ticked.  I dont want to put this in production without being fully comfortable with that fact that it works correctly.   Also, it doesnt seem to matter if the upgradePolicy is set to always or never, the if statement still runs, which i find odd.  Any thoughts?

$VirtualMachines = Get-Content $InputFile

foreach ($vm in (Get-VM -Name $VirtualMachines)){

    if($vm.ExtensionData.Config.ScheduledHardwareUpgradeInfo.UpgradePolicy = "always") {

        $spec = New-Object -TypeName VMware.Vim.VirtualMachineConfigSpec

        $spec.ScheduledHardwareUpgradeInfo = New-Object -TypeName VMware.Vim.ScheduledHardwareUpgradeInfo

        $spec.ScheduledHardwareUpgradeInfo.UpgradePolicy = "never"

        $spec.ScheduledHardwareUpgradeInfo.VersionKey = $null

        $spec.ScheduledHardwareUpgradeInfo.ScheduledHardwareUpgradeStatus = "none"

        $vm.ExtensionData.ReconfigVM_Task($spec)

        Write-Host "$vm hardware upgrade policy is now set to never" -ForegroundColor green

    } #end if

Type                                                                                    Value                                                                                 

----                                                                                    -----                                                                                 

Task                                                                                    task-991494                                                                           

VM1 hardware upgrade policy is now set to never

PS Microsoft.PowerShell.Core\FileSystem::$vm.ExtensionData.Config.ScheduledHardwareUpgradeInfo

UpgradePolicy                  : always

VersionKey                     : vmx-10

ScheduledHardwareUpgradeStatus : pending

Fault                          :

DynamicType                    :

DynamicProperty                :

Reply
0 Kudos
Darwyn99
Contributor
Contributor
Jump to solution

These two scripts by work for me.  I successfully set the upgrade task to happen, then I did a soft reboot from the OS and it upgraded the hardware version.

$vm = Get-VM -Name MyVM
$vm.ExtensionData.Config.ScheduledHardwareUpgradeInfo
$spec = New-Object -TypeName VMware.Vim.VirtualMachineConfigSpec
$spec.ScheduledHardwareUpgradeInfo = New-Object -TypeName VMware.Vim.ScheduledHardwareUpgradeInfo
$spec.ScheduledHardwareUpgradeInfo.UpgradePolicy = "onSoftPowerOff"
$spec.ScheduledHardwareUpgradeInfo.VersionKey = "vmx-09"
$spec.ScheduledHardwareUpgradeInfo.ScheduledHardwareUpgradeStatus = "pending"
$vm.ExtensionData.ReconfigVM_Task($spec)

You can  check the scheduled VM compatibility upgrade for all your vm's with:

Get-VM |
Select-Object -Property Name,
@{N
="UpgradePolicy";E={$_.ExtensionData.Config.ScheduledHardwareUpgradeInfo.UpgradePolicy}},
@{N
="VersionKey";E={$_.ExtensionData.Config.ScheduledHardwareUpgradeInfo.VersionKey}},
@{N
="ScheduledHardwareUpgradeStatus";E={$_.ExtensionData.Config.ScheduledHardwareUpgradeInfo.ScheduledHardwareUpgradeStatus}},
@{N
="Fault";E={$_.ExtensionData.Config.ScheduledHardwareUpgradeInfo.Fault}},
@{N
="DynamicType";E={$_.ExtensionData.Config.ScheduledHardwareUpgradeInfo.DynamicType}},
@{N
="DynamicProperty";E={$_.ExtensionData.Config.ScheduledHardwareUpgradeInfo.DynamicProperty}}|
Format-Table

How would you revert this if you decide you set a VM to upgrade but then later change your mind.  How do you delete the task programatically?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Change the UpgradePolicy property to [VMware.Vim.ScheduledHardwareUpgradeInfoHardwareUpgradePolicy]::never


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

Reply
0 Kudos