我编写了这个快速的Powershell脚本,它将VM的vMotion排队,并且一次只运行4个vMotion任务。 我认为这可能会帮助别人,所以你可以自由地修改代码。
我们正在将虚拟机迁移到2个不同的数据存储库,以便在一夜之间进行DR操作
我已经在脚本中添加了评论,但如果您有任何疑问,请在下面评论。 我相信这可以以更好的方式完成,但是这是一个很快的事情,因为我不得不离开它,所以虚拟机在一夜之间迁移。
帮助我的主要命令是获得任务。
我不知道为什么我不需要为vMotion指定主机,但是虚拟机已迁移到正确的主机,也许DRS处理
Start-Transcript #import the servernames from csv file Import-Csv 'C:\Users\Administrator\Desktop\RestoreDetails Queue.csv' | foreach { #check if vm already on required datastore, change the datastore name as per your environment if ((get-vm $_.host |Get-Datastore).name -ne "DataStore1" -or (get-vm $_.host |Get-Datastore).name -ne "DataStore2") { Write-Host "`nStaring vMotion session for "$_.host #Get current running tasks for Relocatin VM and check if the count is lower than 4 #I am checking for currently running tasks and with Perecentage over 0%, sometimes there are tasks that are just waiting if ((Get-Task | ?{$_.name -eq "RelocateVM_Task" -and $_.state -eq "Running" -and $_.PercentComplete -ne "0"}).count -lt "4") { Write-Host "`nLess than 4 vMotions Migrations are going" #if the count is lower than 4 then check which datastore has more freespace available if ((Get-Datastore -Name "DataStore1").freespacegb -ge (Get-Datastore -Name "DataStore2").freespacegb) { Write-Host ("`nStarting " + $_.host + " to Datastore DataStore1") #send an email Send-MailMessage -to "[email protected]" -from [email protected] -Subject ("Starting " + $_.host + " to Datastore DataStore1") -SmtpServer smtp.domain.com -DeliveryNotificationOption OnFailure, delay #start vMotion to DataStore1 get-vm -Name $_.HOST | Move-VM -Datastore DataStore1 -RunAsync } else { Write-Host ("`nStarting " + $_.host + " to DataStore2") Send-MailMessage -to "[email protected]" -from [email protected] -Subject ("Starting " + $_.host + " to Datastore DataStore1") -SmtpServer smtp.domain.com -DeliveryNotificationOption OnFailure, delay #Start vMotion to Storage DataStore2 get-vm -Name $_.HOST | Move-VM -Datastore DataStore2 -RunAsync } } else { #if more that 4 relocation tasks are running then wait for them to finish sleep 5 Write-Host "`nWaiting for current vMotions to finish..." Write-Host "`nCurrent number of vMotions:"(Get-Task | ?{$_.name -eq "RelocateVM_Task" -and $_.state -eq "Running" -and $_.PercentComplete -ne "0"}).count do { #wait 60 seconds and recheck again sleep 60 } while ((Get-Task | ?{$_.name -eq "RelocateVM_Task" -and $_.state -eq "Running"-and $_.PercentComplete -ne "0"}).count -ge "4") #Repeate the above process when vMotion tasks go lower than 4 if ((Get-Task | ?{$_.name -eq "RelocateVM_Task" -and $_.state -eq "Running" -and $_.PercentComplete -ne "0"}).count -lt "4") { Write-Host "`nLess than 4 vMotions Migrations are going" if ((Get-Datastore -Name "DataStore1").freespacegb -ge (Get-Datastore -Name "DataStore2").freespacegb) { Write-Host ("`nStarting " + $_.host + " to Datastore1") Send-MailMessage -to "[email protected]" -from [email protected] -Subject ("Starting " + $_.host + " to Datastore DataStore1") -SmtpServer smtp.domain.com -DeliveryNotificationOption OnFailure, delay get-vm -Name $_.HOST | Move-VM -Datastore DataStore1 -RunAsync } else { Write-Host ("`nStarting " + $_.host + " to Datastore2") Send-MailMessage -to "[email protected]" -from [email protected] -Subject ("Starting " + $_.host + " to Datastore DataStore1") -SmtpServer smtp.domain.com -DeliveryNotificationOption OnFailure, delay get-vm -Name $_.HOST | Move-VM -Datastore DataStore2 -RunAsync } } } } } #Check which VM's are on DR DataStore storage already Import-Csv 'C:\Users\Administrator\Desktop\RestoreDetails Queue.csv' | foreach { if ((get-vm $_.host |Get-Datastore).name -ne "DataStore2" -or (get-vm $_.host |Get-Datastore).name -ne "DataStore1") { [array]$DRStorage =+ $_.host } else { [array]$NoDRStorage =+ $_.host } } Stop-Transcript