tdubb123
Expert
Expert

patching script quit before the host comes back online

I am trying to run this script to patch a esxi host with update manager but the script quits before the host comes back online

$DC = read-host "Enter the Datacenter to patch"

$vmhost = get-vmhost -Location (Get-Datacenter $dc) | ?{$_.name -like "*r640*"}

$VMs = get-datacenter $dc | get-vm | ?{$_.powerstate -eq "Poweredon"}

$baseline = get-baseline  -Name *Critical*

 

foreach ($vm in $VMs) {

Shutdown-VMGuest -VM $vm -Confirm:$false

$counter = 0

while($vm.PowerState -eq 'PoweredOn'){

if ($counter -gt 180) {

$vm = Get-VM -Name $vm.Name

stop-vm -vm $vm -confirm:$false

} else { 

 

sleep 5

   $vm = Get-VM -Name $vm.Name

$counter+=5

}

   }

 

   }

 

 

 

set-vmhost -VMHost $vmhost -State Maintenance -Confirm:$false

 

$baseline | Attach-Baseline -entity $vmhost -Confirm:$false

 

Scan-Inventory -Entity $vmhost 

$UpdateTask = Update-Entity -Baseline $baseline -Entity $vmhost -RunAsync -Confirm:$false -ErrorAction Stop

while ($UpdateTask.percentcomplete -ne 100) {

 

    Write-Progress -Activity "Patching $vmhost" -PercentComplete $updatetask.PercentComplete -Status "$($UpdateTask.PercentComplete) %"

 

    Start-Sleep 5

 

    $Updatetask = Get-Task -Id $Updatetask.id

 

}

get-vmhost -name $vmhost | set-vmhost -state Connected -Confirm:$false

$vms | start-vm -Confirm:$false

 

 

any idea?

 

0 Kudos
7 Replies
LucD
Leadership
Leadership

What error do you get from the script?
Did you try to adapt the WebOperationTimeoutSeconds with the Set-PowerCLIConfiguration cmdlet?


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

0 Kudos
tdubb123
Expert
Expert

tdubb123_0-1613313074253.png

this is the error I keep seeing

0 Kudos
LucD
Leadership
Leadership

Did you check what is in $UpdateTask?
You're hitting the difference between a Client-Side task and a Server-Side task.

There is no Id property.

 


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

0 Kudos
tdubb123
Expert
Expert

how do i find that while script is running

0 Kudos
LucD
Leadership
Leadership

Just put the variable on a line on its own after the line where you populate it.
The content will appear on the console


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

0 Kudos
tdubb123
Expert
Expert

tdubb123_0-1613342461711.png

here it is

0 Kudos
LucD
Leadership
Leadership

That doesn't really help.
You have 3 error messages I can see, but only one display of the variable.
I would expect the content before each error message


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

0 Kudos