VMware Cloud Community
RobMokkink
Expert
Expert
Jump to solution

wait-task problem

I have a script that updates hba firmware. I first wan't to put the server into maintenance mode.

$task1 = get-vmhost esx1.example.com | set-vmhost -state maintenance

wait-task -task $task1

I get a:

Wait-Task : Cannot bind parameter 'Task'. Cannot conver esx1.example.com to "Vmware.VimAutomation.Types.Task"

How to use Wait-Task properly

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There were some issues reported with Wait-Task in the past.

You could try the EnterMaintenanceMode_Task method.

$esx = Get-VMHost <ESX-hostname> | Get-View

$taskMoRef = $esx.EnterMaintenanceMode_Task(0, $true)

$task = Get-View $taskMoRef 
while ($task.Info.State -eq "running" -or $task.Info.State -eq "queued") {
  sleep 2
  $task = Get-View $taskMoRef
}


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

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

Have a look at .

You could use the script I posted there or even better the Get-VIObjectByVIView cmdlet Carter suggested.


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

RobMokkink
Expert
Expert
Jump to solution

I looked at the Get-VIObjectByVIView, but i have no clue how to use it.:0

0 Kudos
ykalchev
VMware Employee
VMware Employee
Jump to solution

Hi,

Set-VMHost returns VMHost object and there is not need to pass the result to wait-task cmdlet.

If you want to call explicitly wait-task you should use RunAsync parameter

$vmhost = get-vmhost esx1.example.com | set-vmhost -state maintenance

is equal to

$task1 = get-vmhost esx1.example.com | set-vmhost -state maintenance -RunAsync
$vmhost = Wait-Task $task1

Regards,

Yasen

Yasen Kalchev, vSM Dev Team
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sorry Rob, I didn't notice you used a cmdlet.

The other post is useful when you use a SDK method.

In your case it is rather straightforward.

You run the cmdlet with the -RunAsync parameter, capture the TaskImpl object and use that in the Wait-Task cmdlet.

$task = Get-VMHost <ESX-hostname> | Set-VMHost -State maintenance -RunAsync
Wait-Task -Task $task


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

0 Kudos
RobMokkink
Expert
Expert
Jump to solution

Wait-Task works with -runasync

Unfortunally, the script continues, that's not what i want.

The server must be in maintenance mode, before the script continues, because hba firmware gets updated.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

There were some issues reported with Wait-Task in the past.

You could try the EnterMaintenanceMode_Task method.

$esx = Get-VMHost <ESX-hostname> | Get-View

$taskMoRef = $esx.EnterMaintenanceMode_Task(0, $true)

$task = Get-View $taskMoRef 
while ($task.Info.State -eq "running" -or $task.Info.State -eq "queued") {
  sleep 2
  $task = Get-View $taskMoRef
}


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

0 Kudos
RobMokkink
Expert
Expert
Jump to solution

Thanks Luc i was looking for that.

0 Kudos