VMware Cloud Community
Bean78
Contributor
Contributor

vHardware upgrade based on the ESXi version

Hello Everyone,

I have written a script to upgrade vHardware based on the ESXi host version from the list of VMs that i planned to upgrade, when i run the script it completes the executiont but the hardware version is not upgraded. Can anyone help me to look into the script and correct me if am using any deprecated properties and make this script to run as expected 

------------------------------------------------------------------------------------------------

$computerName = Import-Csv C:\VMToolsandvHardware\ExportList.csv
foreach ($server in $computerName)
{
Get-vm $($server.Name) | Shutdown-VMGuest -Confirm:$false

Write-Host -ForegroundColor Green "shutingdown $($server.Name) and going to sleep mode for 20 sec" #shuting down
start-sleep -seconds 5sec

# 1. Query ESXi host information the VM hosted
$esxiHostVer = (Get-vm $($server.Name) | get-vmhost).Version
# 2. Get the ESXi host version (6.5.0 or 6.7.0 or 7.0.1) and store it in variable and pass it to switch ($variable)
switch ($esxiHostVer)
{
6.5.0 {$HardwareVersion = "vmx-13"}
6.7.0 {$HardwareVersion = "vmx-15"}
7.0.1 {$HardwareVersion = "vmx-18"}
}
$vmObj.ExtensionData.UpgradeVM($HardwareVersion)
Write-Host -ForegroundColor Green "Hardware version upgraded for $($server.Name) to $($HardwareVersion) "
start-sleep -seconds 2
Get-vm $($server.Name) | Start-VM
}

----------------------------------------------------------------------------------------------

41 Replies
LucD
Leadership
Leadership

Waiting for 5 seconds and then assuming that the Guest OS is stopped is unreliable.
Best is the test the PowerState in a loop until that actually states PoweredOff.

The sleep after the UpgradeVM call is not needed since you don't call that method with a background task.


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

Reply
0 Kudos
Bean78
Contributor
Contributor

LucD,

Can you help to insert the PowerState loop to the script and even when the VM is in poweroff state the vHardware upgrade is not happening. Can you please comment on it. 

Reply
0 Kudos
LucD
Leadership
Leadership

Your issue might come from the fact that your calling the method on $vmObj, which is nowhere initialised.

Try something like this

Import-Csv -Path C:\VMToolsandvHardware\ExportList.csv -UseCulture -PipelineVariable server |
ForEach-Object -Process {
    Get-vm $($server.Name) | Shutdown-VMGuest -Confirm:$false

    Write-Host -ForegroundColor Green "shutingdown $($server.Name) and going to sleep mode for 20 sec" #shuting down

    while((Get-VM -Name $server.Name).PowerState -ne 'PoweredOff'){
        sleep 2
    }

    # 1. Query ESXi host information the VM hosted
    $esxiHostVer = (Get-VM $($server.Name) | Get-VMHost).Version
    # 2. Get the ESXi host version (6.5.0 or 6.7.0 or 7.0.1) and store it in variable and pass it to switch ($variable)
    switch ($esxiHostVer)
    {
    6.5.0 {$HardwareVersion = "vmx-13"}
    6.7.0 {$HardwareVersion = "vmx-15"}
    7.0.1 {$HardwareVersion = "vmx-18"}
    }
    
    (Get-VM -Name $server.Name).ExtensionData.UpgradeVM($HardwareVersion)
    Write-Host -ForegroundColor Green "Hardware version upgraded for $($server.Name) to $($HardwareVersion) "
    Get-VM $($server.Name) | Start-VM -Confirm:$false
}


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

Bean78
Contributor
Contributor

Thank you LucD the script almost worked, but i obtain this error. vHardware upgrade did not happen. When i manually performed the vHardware upgrade there was a prompt stating that VM is at 6.5 version later compatibility and now you are upgrading it to 7.0 version compatibility. Is it something that we need to force it in the script, so the vHardware upgrade happens.

shutingdown xxxxxx and going to sleep mode for 20 sec
Exception calling "UpgradeVM" with "1" argument(s): "The operation is not supported on the object."
At E:\VMToolsandvHardware\LucD.ps1:21 char:5
+ (Get-VM -Name $server.Name).ExtensionData.UpgradeVM($HardwareVers ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException

 

Reply
0 Kudos
LucD
Leadership
Leadership

Is that for VMs or one or more specific ones?

WHat exact message do you get when you try it via the Web Client?
Could it be that there are ESXi nodes, in the cluster where the VM is running, at a version not supported by the intended HW version?


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

Reply
0 Kudos
Bean78
Contributor
Contributor

All the ESXi servers in the Cluster are running at version 7.0U1, the current compatibility of the VM is ESXi 6.5 and later (vm version 13), the ran the same script for another VM but again it gave me the same error message for not upgrading the vHardware. When i performed the vHardware upgrade through web client, i prompted the following messages.

a) VM compatibility upgrade, this operation changes the compatibility of your virtual machine. It is an irreversible operation that makes you virtual machine incompatible with earlier versions of VMware software products make a backup and upgrade your compatibility (yes/no)   - When i can yes for it

b) Configure VM compatibility (Select a compatibility for vm upgrade) compatible with ESXi 7.0 U1 and later, this vm uses hardware version 18, which is compatible with ESXi 7.0 U1 and later. (ok/Cancel) if you hit ok the VM's vHardware gets upgraded to v18 version

 

Reply
0 Kudos
LucD
Leadership
Leadership

But messages are warning messages from the Web Client.
The API method UpgradeVM does not have those prompts.

There are a couple of possible faults when calling the method, including AlreadyUpgraded and InvalidPowerState.
it might be useful to check the vpxd log to see which fault it is.


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

Reply
0 Kudos
Bean78
Contributor
Contributor

I checked the vpxd log but should not find anything strange. Meanwhile i tried to mold the script (by increasing the shutdown time), but it worked in the first attempt but failed in the second attempt. I have a script to authenticate multiple vCenter at a time to find the VM present in the csv file. Any help to make the script work as expected.

$computerName = Import-Csv E:\VMToolsandvHardware\ExportList.csv

foreach ($server in $computerName)
{

Get-vm $($server.Name) | Shutdown-VMGuest -Confirm:$false

Write-Host -ForegroundColor Green "shutingdown $($server.Name) and going to sleep mode for 60 sec" #shuting down

start-sleep -seconds 60


$vmObj = Get-VM -Name $($server.Name)

# 1. Query ESXi host information the VM hosted

$esxiHostVer = (Get-vm $($server.Name) | get-vmhost).Version

# 2. Get the ESXi host version (6.5.0 or 6.7.0 or 7.0.1) and store it in variable and pass it to switch ($variable)

switch ($esxiHostVer)
{
6.5.0 {$vmHWVersion = "vmx-13"}
6.7.0 {$vmHWVersion = "vmx-15"}
7.0.1 {$vmHWVersion = "vmx-18"}
}

$vmObj.ExtensionData.UpgradeVM($vmHWVersion)


Write-Host -ForegroundColor Green "Hardware version upgraded for $($server.Name) to $($vmHWVersion) "

start-sleep -seconds 5

Get-vm $($server.Name) | Start-VM

}

 

Reply
0 Kudos
LucD
Leadership
Leadership

Why do you insist on using the Sleep cmdlet?
What is wrong with the While-loop I provided earlier?

What exactly failed?


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

Reply
0 Kudos
Bean78
Contributor
Contributor

I executed you script, we are almost there, shutdown of the VM happens and the script checks the power state using "while and do" and then triggers the vHardware upgrade but suddenly it gives an error specified below and then powers on the VM without hardware upgrade. Really don't know where we are going wrong. Does the vHardware upgrade happens at your end using the same script.

shutingdown xxxxx and going to sleep mode for 20 sec
Exception calling "UpgradeVM" with "1" argument(s): "The operation is not supported on the object."
At line:23 char:5
+ (Get-VM -Name $server.Name).ExtensionData.UpgradeVM($HardwareVers ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException

Hardware version upgraded for xxxxxx to

Reply
0 Kudos
LucD
Leadership
Leadership

I'm completely on vSphere 7 in my lab, so my tests are limited.

Do you know which VM causes that error?
Is actually powered off when call the UpgradeVM method?
Is it perhaps a vCLS VM , or an FT node VM?


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

Bean78
Contributor
Contributor

The script works for one time and at another time it does not. The VM's are powered off by the script and it is not a vCLS or FT VM.

Then i tried another script to check whether the upgrade happens through this one and it also fails dont know where am going wrong.
foreach($vmlist in (Get-Content -Path E:\VMToolsandvHardware\vmlist.txt)){
$vm = Get-VM -Name $vmlist
Shutdown-VMGuest -VM $vm -Confirm:$false
}

foreach($vmlist in (Get-Content -Path E:\VMToolsandvHardware\vmlist.txt)){
$vm = Get-VM -Name $vmlist
Set-VM -VM $vm -Version vmx-18 -Confirm:$false
}

foreach($vmlist in (Get-Content -Path E:\VMToolsandvHardware\vmlist.txt)){
$vm = Get-VM -Name $vmlist
Start-VM -VM $vm -Confirm:$false
}

Set-VM : Cannot bind parameter 'Version'. Cannot convert value "vmx-18" to type "VMware.VimAutomation.ViCore.Types.V1.VM.VMVersion". Error: "Unable to match the
identifier name vmx-18 to a valid enumerator name. Specify one of the following enumerator names and try again:
Unknown, v4, v7, v8, v9, v10, v11, v12, v13, v14"
At line:3 char:25
+ Set-VM -VM $vm -Version vmx-18 -Confirm:$false
+ ~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Set-VM], ParameterBindingException
+ FullyQualifiedErrorId : CannotConvertArgumentNoMessage,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetVM

Reply
0 Kudos
LucD
Leadership
Leadership

Which PowerCLI do you have?
The more recent HW versions are only available in the recent PowerCLI version.
The current latest PowerCLI release does not support vmx-18, you will have to use the UpgradeVM method.


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

Reply
0 Kudos
Bean78
Contributor
Contributor

Current PowerCLI version is 11.1.0.11289667,

When i try to update it to the latest version, i get this message

PackageManagement\Install-Package : Authenticode issuer 'CN=VeriSign Class 3 Public Primary Certification Authority - G5, OU="(c) 2006 VeriSign, Inc. - For authorized
use only", OU=VeriSign Trust Network, O="VeriSign, Inc.", C=US' of the new module 'VMware.VimAutomation.Sdk' with version '12.2.0.17531155' is not matching with the
authenticode issuer 'CN=VeriSign Class 3 Code Signing 2010 CA, OU=Terms of use at https://www.verisign.com/rpa (c)10, OU=VeriSign Trust Network, O="VeriSign, Inc.",
C=US' of the previously-installed module 'VMware.VimAutomation.Sdk' with version '11.0.0.10334495'. If you still want to install or update, use -SkipPublisherCheck
parameter.

How do we proceed on this....

Reply
0 Kudos
LucD
Leadership
Leadership

Did you use the Update-Module cmdlet?

Update-Module -Name VMware.PowerCLI -Force


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

Bean78
Contributor
Contributor

yes i used this switch even then am get this message

PackageManagement\Install-Package : Authenticode issuer 'CN=VeriSign Class 3 Public Primary Certification Authority - G5, OU="(c) 2006 VeriSign, Inc. - For authorized
use only", OU=VeriSign Trust Network, O="VeriSign, Inc.", C=US' of the new module 'VMware.VimAutomation.Sdk' with version '12.2.0.17531155' is not matching with the
authenticode issuer 'CN=VeriSign Class 3 Code Signing 2010 CA, OU=Terms of use at https://www.verisign.com/rpa (c)10, OU=VeriSign Trust Network, O="VeriSign, Inc.",
C=US' of the previously-installed module 'VMware.VimAutomation.Sdk' with version '11.0.0.10334495'. If you still want to install or update, use -SkipPublisherCheck
parameter.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:2052 char:20
+ ... $sid = PackageManagement\Install-Package @PSBoundParameters
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Exception
+ FullyQualifiedErrorId : AuthenticodeIssuerMismatch,Validate-ModuleAuthenticodeSignature,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage

Reply
0 Kudos
LucD
Leadership
Leadership

Did you try with

Install-Module -Name VMware.PowerCLI -SkipPublisherCheck -Force


You do seem to have a rather old version of the PowerShellGet module.
Perhaps update that first.
See for example PowerShell: How to update PowerShellGet and Package Management


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

Reply
0 Kudos
Bean78
Contributor
Contributor

Upgraded the PowerCLI version.

Back to the original challenge, the machine have already upgraded to vx-18 one time but i have reverted to the snapshot to check the script again. This is what i see in the task description

Cannot upgrade virtual machine compatibility.

Upgrading virtual machine compatibility of xxxxx in xxx to vm.hwVersion..label

Task: Upgrade VM compatibility - The operation is not supported on the object.

vpxd does not tell me more about this error.

How to check whether $HardwareVersion in the script is obtaining the vHardware version mentioned in the script 

Since we can spin a VM with 6.5 compatability in 7.0 ESXi and then upgrade to 7.0 compatability, at your end. Can we try that..

 

Reply
0 Kudos
LucD
Leadership
Leadership

Are you doing this with the cmdlet or with the API method?


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

Reply
0 Kudos