VMware Cloud Community
scottD65
Contributor
Contributor

ReconfigVM_Task, get-task and wait-task in Vsphere 7

I have been using a simple bit of code to wait for drives to be added 'hot' in a mutli-writer shared VM situation.  Below is an example of the code I have been using for a few years now.  

$task = $vmview.ReconfigVM_Task($spec)
$task1 = Get-Task -Id ("Task-$($task.value)")
$task1 | Wait-Task

The reconfigVM_task $spec is pre-loaded with all the required info and then submitted on the first line.  The task info is usually retrieved there and then I can wait for it to complete.  Apparently, in vsphere 7, the task info is not being passed.  I use this to wait for the disk creation task to complete before I add it to the sharing VMs.  The trouble is, in vsphere 7, the task information is not returned and the script then skips to adding the disk to the partner VMs and that fails.  Course this creates a bit of a mess and manual clean-up is required.  Adding to the mix is that we are using VSAN a good bit now and disk creation can take time there depending on the storage policy applied.  

My question is, is there another method that works to wait for the task to complete?  I have not been able to find anything yet.  Any help would be great.  😁

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership

I wouldn't know if the Get-Task cmdlet behaviour has changed since I never used that method.
I always used something like this (and that still seems to work in 7.*)

$tMoRef = $vm.ExtensionData.ReconfigVM_Task($spec)

$tObj = Get-View -Id $tMoRef
while($tobj.Info.State -eq 'running'){
  sleep 1
  $tobj.UpdateViewData()
}


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

Reply
0 Kudos
scottD65
Contributor
Contributor

That works.  In my case I did $taskMoRef = $vmview.ReconfigVM_Task($spec); $tObj = Get-View -Id $taskMoRef which worked fine.  I had done $vmview = Get-View -ViewType VirtualMachine -Property Name, Config.Hardware.Device -Filter @{"Name" = $virtMach.name } earlier in the script. $virtMach is the passed get-vm variable in my function. 

Thanks for your help. 

Reply
0 Kudos
randylawrence42
Contributor
Contributor

I had the same issue.


I replaced my multiwriter functions with following code at the end.

$task = $vm.ReconfigVM_Task($spec)
$task
$task1 = Get-Task -Id "$task"
$task1
$task1 | Wait-Task

Basically it was the output that changed so you dont need  the Task- Prefix anymore.

 $task1 = Get-Task -Id ("Task-$($task.value)")

 

Reply
0 Kudos
scottD65
Contributor
Contributor

Ah good to know.  Thanks for adding that bit of code to the discussion.  

Reply
0 Kudos