Reply to Message

View discussion in a popup

Replying to:
Zsoldier
Expert
Expert

Like this:

<#

  .SYNOPSIS

  Will enter a wait/loop if there are running vMotion or svMotion tasks.

  .DESCRIPTION

  Checks the tasks and counts the number running vMotion or svMotion tasks.

  If the number of active tasks is greater than the specified limit

  then the function sleeps and checks again. The intended is as a throttle

  in large (s)vMotion loops to prevent overloading the environment.  Is

  especially useful in throttling a script while an unrelated event happens,

  such as putting a host or datastore into maintenance mode.

  .PARAMETER vMotionLimit

  The active tasks to test for.  If the number of active tasks is less than

  this the function will exit.  The default is 1, and must be an integer.

  .PARAMETER DelayMinutes

  How long to wait before checking the active tasks again.  The default

  is 1, and must be an integer.

  .EXAMPLE

  Wait-mTaskvMotions -Verbose

  .EXAMPLE

  Wait-mTaskvMotions -vMotionLimit 4

  .EXAMPLE

  Wait-mTaskvMotions -vMotionLimit 2 -DelayMinutes 3 -Verbose

  .NOTES

  Using -Verbose will output standard script started and finished messages,

  and how long the function will sleep before it checks the active tasks again.

  .LINK

  http://mongit201.be.monster.com/chrism/virtech/blob/master/Repo/Wait-mTaskvMotions.ps1

#>

function Wait-mTaskvMotions {

[CmdletBinding()]

Param(

  [int] $vMotionLimit=1,

  [int] $DelayMinutes=5

)

$NumvMotionTasks = (Get-Task | ? { ($_.PercentComplete -ne 100) -and ( ($_.Description -like '*DRS*') -or ($_.Description -like '*vMotion*') )} | Measure-Object).Count

While ( $NumvMotionTasks -ge $vMotionLimit ) {

  Write-Verbose "$(Get-Date)- Waiting $($DelayMinutes) minute(s) before checking again."

  Start-Sleep ($DelayMinutes * 60)

  $NumvMotionTasks = (Get-Task | ? { ($_.PercentComplete -ne 100) -and ( ($_.Description -like '*DRS*') -or ($_.Description -like '*vMotion*') )} | Measure-Object).Count

}

Write-Verbose "$(Get-Date)- Proceeding."

} # end function

$filepath = "D:\PathtoCSV\filename.csv

$csvobj = import-csv $filepath

foreach ($row in $csvobj) {

     $vmobj = get-vm $row.vmname

     $ds = get-datastore $row.destds

     $vmobj | move-vm -datastore $ds -confirm:$false -runasync

     Wait-mTaskvMotions -vMotionLimit 4 # This will keep going through the foreach loop until 4 tasks are registered (vMotion or Storage vMotion), waits 5 minutes between checks.  Will only continue to process loop when vMotion tasks are less than 4.

     }

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier