PowerCLI script to Update all the ESXi Host in the vCenter Server

  • Post category:Power CLI / VMware
  • Post last modified:August 2, 2024

Here is a PowerCLI Script which you can use the Update all ESXi Host in a vCenter Server.

# Connect to the vCenter Server
$vcServer = "vcenter-server.example.com"
$vcUsername = "username"
$vcPassword = "password"
Connect-VIServer -Server $vcServer -Username $vcUsername -Password $vcPassword

# Get all the hosts in the vCenter Server
$hosts = Get-VMHost

# Update each host
foreach ($host in $hosts) {
Write-Host "Updating host: $($host.Name)"

# Check for available updates
$hostView = $host | Get-View
$updateMgr = $hostView.ConfigManager.UpdateManager
$hostUpdates = $updateMgr.QueryHostPatch_Task() | Select-Object -ExpandProperty HostPatch

# Install updates
$task = $updateMgr.InstallHostPatch_Task($hostUpdates)

# Monitor update task progress
do {
Start-Sleep -Seconds 10
$task = Get-Task -Id $task.ExtensionData.Task
$progress = $task.Progress
Write-Host "Update task progress: $progress%"
} while ($task.State -eq "Running")

Write-Host "Host updated: $($host.Name)"
}

# Disconnect from the vCenter Server
Disconnect-VIServer -Server $vcServer -Confirm:$false

Before running the script, make sure to replace “vcenter-server.example.com”, “username”, and “password” with the appropriate values for your vCenter Server.

This script connects to the vCenter Server, retrieves all the hosts, checks for available updates on each host, installs the updates, and monitors the update task progress. Finally, it disconnects from the vCenter Server.

Please note that updating ESXi hosts can cause temporary disruptions in the virtualized environment, and it’s essential to ensure compatibility and have proper backups before applying updates.

Ashutosh Dixit

I am currently working as a Senior Technical Support Engineer with VMware Premier Services for Telco. Before this, I worked as a Technical Lead with Microsoft Enterprise Platform Support for Production and Premier Support. I am an expert in High-Availability, Deployments, and VMware Core technology along with Tanzu and Horizon.

Leave a Reply