Not sure why this does not get out of the while loop
$esxhost = read-host "Enter the host to upgrade"
$poweredonvms = get-vmhost -name $esxhost | get-vm | ?{$_.powerstate -eq "Poweredon"}
also in the 60s, some vm are not shutdown yet and the task proceeded to remediate. how do i check and see if all vms are powered off
To wait for all VMs to be powered off, you could do
#shutdown all powered on vms
$poweredonVms | Shutdown-VMGuest -Confirm:$false
While ((Get-VM -Name ($poweredonvms.Name).PowerState -contains 'poweredon' )){
Start-Sleep -Seconds 5
}
Did you check what is actually in $updatetask?
Especially the Id and PercentComplete properties.
Does the Get-Task in the loop actually return a Task object?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
i think I am not seeing a task
Write-Progress : Cannot validate argument on parameter 'PercentComplete'. The argument is null, empty, or an element of the argument collection contains a null
value. Supply a collection that does not contain any null values and then try the command again.
upgrade_esxi.ps1:30 char:99
+ ... nish patch installation" -PercentComplete $UpdateTask.PercentComplete
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Write-Progress], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.WriteProgressCommand
Get-Task : Cannot validate argument on parameter 'Id'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command
again.
upgrade_esxi.ps1:34 char:32
+ $UpdateTask = Get-Task -id $UpdateTask.id
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Task], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Common.Commands.Cmdlets.GetTask
also seeing this while remediating host
RemediateTask
percent complete: 44
[ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo ]
Id : HostSystem-host-27940
DatastoreIdList : {Datastore-datastore-27941}
Remediate-Inventory : 8/7/2022 10:56:13 AM Update-Entity The underlying connection was closed: A connection that was expected to be kept alive was closed by
the server.
upgrade_esxi.ps1:22 char:31
+ ... sk = $baseline | Remediate-Inventory -Entity $esxhost -Confirm:$false
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Update-Entity], ViError
+ FullyQualifiedErrorId : Client20_QueryServiceImpl_WaitForUpdates_ViError,VMware.VumAutomation.Commands.RemediateInventory
Those are other issues with your script as far as I can tell.
They are not related to the snippet to make sure all VMs are powered off before proceeding
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference