Please use the below Script in order to restart All the Hosts in the vCenter Server using PowerCLI.
# 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
# Restart each host
foreach ($host in $hosts) {
Write-Host "Restarting host: $($host.Name)"
# Restart the host
Restart-VMHost -VMHost $host -Confirm:$false
# Wait for the host to restart
do {
Start-Sleep -Seconds 10
$hostState = (Get-VMHost -Name $host.Name).ConnectionState
} while ($hostState -ne "Connected")
Write-Host "Host restarted: $($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, restarts each host one by one, waits for the host to come back online, and then proceeds to the next host. Finally, it disconnects from the vCenter Server.
Please exercise caution when running scripts that restart hosts, as it can cause temporary disruptions in the virtualized environment.