VMware Cloud Community
rxjoseph
Enthusiast
Enthusiast

vMotion VMs in batches using a CSV file

Hi LucD

The script below works fine, however if i run this it will kick off all Vmotions at the same time "RunAsync", because of this I manually comment the VMs and send in batchhes of 50 VMs to migrate at one time
And that sorts the problem. But due to this limitation I have to sit and wait until i complete 1000's of VMs
Is there a way i can tell that at one time migrate 50 VMs and once thats completed kick off the next 50 and next 50 until it finish all vMotions

Please advise


# Import vMotion CSV file
$vMotionDetails = Get-Content -Path "$($FilesAndLogDir)\$vMotion_Template_Name" |Select-String '^[^#]' | ConvertFrom-Csv -UseCulture


################################################################################
### ###
### KICK OFF VMOTION TO MOVE VMS FROM STANDARD SWITCH TO VCF WORKLOAD DOMAIN ###
### ###
################################################################################

 

Write-host "################################################################################" -ForegroundColor Yellow
Write-host "### ###" -ForegroundColor Yellow
Write-host "### KICK OFF VMOTION TO MOVE VMS FROM STANDARD SWITCH TO VCF WORKLOAD DOMAIN ###" -ForegroundColor Yellow
Write-host "### ###" -ForegroundColor Yellow
Write-host "################################################################################" -ForegroundColor Yellow


foreach ($vMotionTask in $vMotionDetails )

{

Try {

### Assign VM from CSV file to a variable
$VM = Get-vm -name $vMotionTask.VMname -ErrorAction Stop
}

Catch {

Write-Warning ""
Write-Host "Error Ran into issues: $($psitem.ToString()) " -ForegroundColor Red
""
Write-Host "Unable to migrate VM" $vMotionTask.VMname "due to above error" -ForegroundColor DarkYellow
""
}


#Getting a Random host for vMotion
$Destination = Get-cluster $selectedCLuster.name |Get-VMHost |where {$_.Manufacturer -notlike "*Hitachi*"} |Get-Random

$networkAdapter = Get-NetworkAdapter -vm $VM

 

Try {


$destinationSDIPortGroups = @()
$destinationSDIPortGroups += Get-VDPortgroup -name $vMotionTask.SDI_NW1 -ErrorAction Stop #|where {$_.networktype -eq "Distributed"} -ErrorAction Stop
$destinationSDIPortGroups += Get-VDPortgroup -name $vMotionTask.SDI_NW2 -ErrorAction Stop #|where {$_.networktype -eq "Distributed"} -ErrorAction Stop
$destinationSDIPortGroups += Get-VDPortgroup -name $vMotionTask.SDI_NW3 -ErrorAction Stop #|where {$_.networktype -eq "Distributed"} -ErrorAction Stop
$destinationSDIPortGroups += Get-VDPortgroup -name $vMotionTask.SDI_NW4 -ErrorAction Stop #|where {$_.networktype -eq "Distributed"} -ErrorAction Stop

}

Catch {
""
Write-Host "VM" $VM "2nd and/or 3rd NIC not found" -ForegroundColor Yellow } #{ Write-Host "Error ran into issues: $($PSItem.ToString())" -ForegroundColor Red }


Try {

Move-VM -VM $VM -Destination $Destination -NetworkAdapter $networkAdapter -PortGroup $destinationSDIPortGroups -Confirm:$false -ErrorAction Stop -RunAsync |Out-Null
#
Write-Host "VM ==>"$VM "Moved to destination ==>"$Destination "ESXi host on Cluster ==>" $selectedCLuster.name -ForegroundColor Green
}

Catch
{
""
Write-Warning ""

Write-Host "Error Ran into issues: $($PSItem.ToString()) " -ForegroundColor Red
""
Write-Host "Error !!! $VM could not be migrated due to above error" -ForegroundColor DarkYellow
}

 

Many thanks

RXJ

0 Kudos
1 Reply
LucD
Leadership
Leadership

That same question has been asked multiple times.
You can in your script check the status of the running tasks and only start new ones when the number is below a specified maximum.
See for example Solved: Re: limit vMotion operations? - VMware Technology Network VMTN


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

0 Kudos