Hello Luc,
Good morning,
few days back i discussed with you regarding powercli script to upgrade esxi hosts.
below is the code with few modification like
1:including both upgrade and patch baseline in single command .
2:assigning license key .
i was hoping below code will loop on each esxi host of cluster as we are using foreach but it did not happen and stuck at following
also the upgrading host bar does not progress as we thought as per code below.
![]()
#code
$vcenter=read-host "vcenter name"
$cred=Get-Credential
$std_version=Read-Host "what version you want to upgrade to"
$std_build=read-host "what build you want to update to"
$license=read-host "specify license key"
connect-viserver -server $vcenter -Credential $cred
#$baseline_cisco= get-baseline -Name 'Cisco Custom ESXi 6.5 Upgrade'
import-module vmware.vumautomation
$baseline_dell=get-baseline -name "ESXi6.5Upgrade-Dell"
$baseline_dell_patch=Get-PatchBaseline -Name "ESXI 6.5 20180604001_19thAug2018"
#above can be different baseline also depending on the model.
$cluster=read-host "cluster name"
set-cluster -Cluster $cluster -DrsAutomationLevel FullyAutomated
Set-Cluster -Cluster $cluster -HAAdmissionControlEnabled $false
$hosts_cluster=get-vmhost -location $cluster
$count=$hosts_cluster.count
$count
# start vmotion test is done using vmotion_test_function
foreach ($esx in $hosts_cluster)
{
$esxi=get-vmhost -name $esx
write-host "currently working on "$esxi.name -ForegroundColor Green
if ($esxi.version -ne $std_version -and $esxi.build -ne $std_build)
{
write-host $esxi.name "needs to be upgraded to " $std_version "and" $std_build -BackgroundColor Yellow
write-host "putting host" $esxi.name "in mainteneace mode"
$maint = Set-VMHost -VMHost $esxi -State Maintenance -RunAsync
Do
{
Write-Host -NoNewline '.'
sleep 1
$esxi = Get-VMHost -Name $esxi.name
} until ($esxi.State -eq "maintenance" -and $maint.PercentComplete -eq 100)
Write-Host "`rDone"
Write-Output "Task '$($maint.Description)/$($maint.Id)' for $($maint.Result.Name) ended with status $($maint.State)"
}
else
{
write-host $esxi "is on standard version and build"
}
if($esxi.Manufacturer -match "Dell Inc.")
{
Attach-Baseline -Entity $esxi -Baseline $baseline_dell,$baseline_dell_patch
$update_esxi=Update-Entity -Baseline $baseline_dell,$baseline_dell_patch -Entity $esxi -runasync
while('Running','Queued' -contains $update_esxi.State){
Write-Progress -Activity 'upgrading host ' -PercentComplete $update_esxi.PercentComplete
sleep 2
}
Write-Output "Task '$($update_esxi.Description)/$($update_esxi.Id)' for $($update_esxi.Result.Name) ended with status $($update_esxi.State)"
#assigning license to upgraded host
set-vmhost -vmhost $esxi -LicenseKey $license
#taking host out of maintenance mode.
Set-VMHost -VMHost $esxi -State Connected
Start-Sleep -Seconds 240
}}
is it possible for you take a look one more time on above code.