VMware Cloud Community
slayer991
Enthusiast
Enthusiast
Jump to solution

svMotion of templates - Task not completing - Need to wait until migration is complete before trying to set as template

I have the following script that's designed to storage vMotion templates from 1 datastore to another.  It sets the template as a vMotion, migrates the VM, then sets it back as a template.

  Foreach ($Template in Get-template | where-object {$_.Name -like '*win*'}){

        Try {get-template -name $template -datastore $vcdatastore -ErrorAction silentlycontinue;$TemplateMoved = $false }Catch {$TemplateMoved = $true}

            If($TemplateMoved -eq $false){

                  Write-Log -Message "svMotion Process Started: $($template) moving to $vcdatastore on vCenter $($vchostname)"

                  Set-Template -template $template -ToVM | Move-VM -Datastore $vcdatastore -inventorylocation WinTemplates | Set-VM -ToTemplate -confirm:$false -erroraction stop

                  Write-Log -Message "svMotion Process Completed: $($template) moved to $vcdatastore on vCenter $($vchostname)"

            }

                      elseif ($TemplateMoved -eq $true){

                            Write-Log -Message "$($template) has already been moved to $vcdatastore on vCenter $($vchostname)"

                      }

  }

Which is throwing an error:

Move-VM : 1/16/2019 1:05:28 PM  Move-VM        Operation is not valid due to the current state of the object.

At C:\Users\me\Documents\Scripts\VMware\template-svmotion.ps1:129 char:58

+ ... ate -ToVM | Move-VM -Datastore $vcdatastore -inventorylocation Win ...

+                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Move-VM], VimException

    + FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.MoveVM

It starts the migration, throws the error, and starts migrating all the templates.

After thinking about it, I realized of course it can't set it as a template...it's in the process of migrating.  The question I have is how can I get it to wait until the migration is complete before attempting to set it as a template and beginning on the next template.

Thanks,

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You can monitor for the events that indicate a successful or failed relocation event.

See for example Re: Need Help to automate Poweroff, SVMotion (and VMotion), Upgrade VM Hardware and VMware Tools and...


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

You can monitor for the events that indicate a successful or failed relocation event.

See for example Re: Need Help to automate Poweroff, SVMotion (and VMotion), Upgrade VM Hardware and VMware Tools and...


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

0 Kudos
slayer991
Enthusiast
Enthusiast
Jump to solution

What about task-wait?  How would that work or would it work in this use case?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are not using RunAsync, so there is no background task.


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

0 Kudos
slayer991
Enthusiast
Enthusiast
Jump to solution

Your reply got me thinking. I'm not running this async so commands should execute in order.   I took the pipe out and now it waits for execution of the previous task.

   Foreach ($Template in Get-template | where-object {$_.Name -like '*win*'}){

        Try {get-template -name $template -datastore $vcdatastore -ErrorAction stop;$TemplateMoved = $true }Catch {$TemplateMoved = $false}

             If($TemplateMoved -eq $false){

             Write-Log -Message "svMotion Process Started: $($template) moving to $vcdatastore on vCenter $($vchostname)"

             Set-Template -template $template -ToVM | Move-VM -Datastore $vcdatastore -inventorylocation WinTemplates

             Get-VM -name $template |Set-VM -ToTemplate -confirm:$false

             Write-Log -Message "svMotion Process Completed: $($template) has moved to $vcdatastore on vCenter $($vchostname)"

      } 

                  elseif ($TemplateMoved -eq $true){

                       Write-Log -Message "$($template) has already been moved to $vcdatastore on vCenter $($vchostname)"

                }

     }

The other thing I found is I could query against the running task for RelocateVM_task and wait for success.  This would work for us as we have sDRS disabled so the only svMotions running would be from this task. 

{get-task |?($_.name -eq "RelocateVM_Task" -and $_.state -eq "Success")}

Thanks LucD

0 Kudos