Content:
Introduction
Upgrading an ESXi host is an essential task for maintaining a stable and secure virtual environment. With the help of PowerShell, the upgrade process can be automated, reducing the potential for human error and saving time. In this blog post, we will discuss how to upgrade an ESXi host using PowerShell, including placing the host in maintenance mode and waiting for the maintenance mode event to complete.
Advantages of using a PowerShell script to upgrade an ESXi host:
Efficiency: Automating the upgrade process using PowerShell can save a significant amount of time compared to manually upgrading each host.
Consistency: By using the same script for each upgrade, you can ensure that the upgrade process is consistent across all hosts, reducing the potential for human error.
Reliability: The use of PowerShell can reduce the potential for errors and oversights that can occur during manual upgrades, resulting in a more reliable upgrade process.
Flexibility: The script can be easily customized to meet the specific needs of your environment, such as adding custom VIBs or modifying the maintenance mode options.
Scalability: The script can be used to upgrade multiple hosts simultaneously, making it ideal for larger environments.
By leveraging the power of PowerShell, you can streamline and simplify the ESXi upgrade process, resulting in a more efficient and reliable infrastructure.
Prerequisites
Before beginning the upgrade process, you will need to ensure that you have the following:
An ESXi host that needs to be upgraded
A compatible ESXi upgrade bundle downloaded from the VMware website
PowerShell installed on your system
VMware PowerCLI installed on your system
Step 1: Connect to the ESXi Host
To connect to the ESXi host, you can use the Connect-VIServer cmdlet in VMware PowerCLI. Replace <ESXi-host>, <Username>, and <Password> with the appropriate values for your environment.
Connect-VIServer -Server <ESXi-host> -User <Username> -Password <Password>
Step 2: Place the ESXi Host in Maintenance Mode
Before upgrading the ESXi host, you will need to place it in maintenance mode. This can be accomplished using the Set-VMHost cmdlet.
Set-VMHost -VMHost <ESXi-host> -State "Maintenance" -Confirm:$false
The -Confirm:$false parameter is used to suppress the confirmation prompt.
Step 3: Wait for the Maintenance Mode Event to Complete
After placing the ESXi host in maintenance mode, it is important to wait for the maintenance mode event to complete before proceeding with the upgrade. This can be accomplished using the Get-VIEvent cmdlet and the Where-Object cmdlet to filter the events to those that contain the message "entered maintenance mode". The while loop waits until an event with this message is found, indicating that the maintenance mode has completed.
Step 4: Create a New ESXi Image Profile for the Upgrade
Next, you will need to download the patch file from the VMware path download site
After downloading the zip file manually then you can use the below command.
Add-EsxSoftwareDepot -DepotUrl "C:\esxi-depot.zip"
Or use the New-EsxImageProfile cmdlet to download. Replace <new-profile-name> with the name you want to give to the new Image Profile.
$newProfileName = "<new-profile-name>"
$newProfile = New-EsxImageProfile -CloneProfile -ProfileName $newProfileName -Vendor "ESXi"
Step 5: Add the Upgrade Bundle to the New Image Profile
Add the upgrade bundle(it can be patched for network/storage drivers or patches from VMware) to the new Image Profile using the Add-EsxSoftwarePackage cmdlet. Replace <path-to-upgrade-bundle> with the path to the upgrade bundle.
$upgradeBundlePath = "<path-to-upgrade-bundle>"
Add-EsxSoftwarePackage -ImageProfile $newProfile -SoftwarePackage $upgradeBundlePath
Step 6: Validate the New Image Profile
Validate the new Image Profile using the Test-EsxImageProfile cmdlet to ensure that it is valid and ready for the upgrade process.
Test-EsxImageProfile -ImageProfile $newProfile
Step 7: Apply the Upgrade to the ESXi Host
Finally, you can apply the upgrade to the ESXi host using the Set-EsxImageProfile cmdlet. Replace <ESXi-host> with the name or IP address of the ESXi host.
Set-EsxImageProfile -ImageProfile $newProfile -VMHost <ESXi-host> -NoSignatureCheck -NoAutoReboot -Confirm:$false
The -NoSignatureCheck parameter is used to skip the signature check of the upgrade bundle, -NoAutoReboot parameter is used to prevent the host from automatically rebooting after the upgrade process is complete, and -Confirm:$false parameter is used to suppress the confirmation prompt.
Step 8: Verify the Upgrade
After the upgrade process is complete, you can verify that the upgrade was successful using the Get-VMHost cmdlet.
Get-VMHost <ESXi-host> | Select-Object Version, Build, Name
Full Script:
Below is the full script, I try to put all the above actions into a function.
To use this function, simply call it with the required parameters:
To scale this function to multiple hosts, you can use PowerShell remoting to run the function on each host remotely. Here's an example of how you can do this:
This script will run the Upgrade-ESXiHost function on each host in the $hosts array, using PowerShell remoting to execute the function remotely.
An alternative approach using esxcli commands:
Conclusion:
Automating the upgrade process using PowerShell can save time and reduce the potential for human error. By following the steps outlined in this blog post, you can easily upgrade your ESXi hosts in a more efficient and reliable manner.
Reference :