使用 powercli 对 VMware 虚拟机 vMotion 进行排队

使用 powercli 对 VMware 虚拟机 vMotion 进行排队

我编写了这个快速的 Powershell 脚本,它将虚拟机的 vMotion 排队,并且每次只运行 4 个 vMotion 任务。我认为这可能会对某些人有所帮助,因此您可以根据自己的喜好随意修改代码。

我们将虚拟机移至两个不同的数据存储区,以便在夜间进行 DR 演习。

我在脚本中加入了注释,但如果您有任何疑问,请在下面评论。我相信这可以用更好的方式完成,但这是一件很快的事情,因为我必须让它运行,所以虚拟机会在一夜之间迁移。

对我有帮助的主要命令是 get-task。

我不知道为什么我不需要为 vMotion 指定主机,但虚拟机已迁移到正确的主机,也许 DRS 已经处理好了这个问题

答案1

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

相关内容