PowerCLI Script for SvMotion

PowerCLI Script for SvMotion

A co-worker of mine, Nonna put together a script for us that I wanted to share.  The basis behind  the script is that we need to move our VM's off one SAN and onto  another.  What the script does is create a clone of the source VM onto a  "transit" LUN, when complete it starts the SvMotion, and if successful  removes the clone.  There are other steps involved but, try and  see if  it is something you may use in your environment.

Connect-VIServer "vCenter@yourdomain.com"

## Variables  #
$VM = ""
$Source = Get-VM "$VM"
$SourceView = $Source | Get-View
$SourceDS = $SourceView.summary.get_Config().VmPathName
$TargetDS = Get-Datastore ""
$BackpTargetDS = Get-Datastore ""
$CloneTargetDS = Get-Datastore ""
$VMHost = Get-VMHost $Source.Host
$Clone = "$Source clone"

##Cloning of the VM    #
New-VM -VMHost $VMHost -Name $Clone -Datastore $CloneTargetDS -VM $Source -ErrorAction SilentlyContinue
$NewClone = Get-VM $Clone -ErrorAction SilentlyContinue
if ($NewClone){
    ## if cloning WAS successful - proceed with the sVMotion           #
    ## If cloning was NOT successful, send an email and terminate script, #
    ## Checking if cloning is successful by looking if clone exist on target Datastore #
    $CloneView = Get-VM $Clone | Get-View
    if ((Get-VM $Clone).PowerState -eq "PoweredOff" -and $CloneView.Summary.get_Config().VmPathName -eq "[$CloneTargetDS] $Clone/$Clone.vmx")
    {
    $Capacity = $TargetDS.CapacityMB
    $Free15Proc = ($Capacity)*15/100
    $MovedVMSizeMB = ($Source.UsedSpaceGB)*1024
    $TargetDSReady = ($Free15Proc)+($MovedVMSizeMB)
    if ($TargetDS.FreeSpaceMB -ge $TargetDSReady)
    {Move-VM -Datastore $TargetDS -VM $Source -ErrorAction SilentlyContinue
            ##line below checks if migrated VM is still powered on and if the storage path of VM is not the Source Datastore #
            if ((Get-VM $Source).PowerState -eq "PoweredOn" -and (Get-VM $Source | Get-View).summary.get_Config().VmPathName -eq "$SourceDS")
            {
            #Send e-mail that VM has not been moved    #
            Write-Host "Migration of $Source failed"
            Send-MailMessage -To email@yourdomain.com -From email@yourdomain.com -SmtpServer smtpserver.yourdomain.com -Subject "STATUS of $Source Migration" -Body "Migration of $Source failed"
            }
            if ((Get-VM $Source).PowerState -eq "PoweredOn" -and (Get-VM $Source | Get-View).summary.get_Config().VmPathName -ne "$SourceDS")
            { Write-Host "SVMotion of $Source was successful"
            Send-MailMessage -To email@yourdomain.com -From email@yourdomain.com -SmtpServer smtpserver.yourdomain.com -Subject "STATUS of $Source Migration" -Body "$Source Migration was successful - $Clone is being removed"
            Remove-VM $Clone -DeleteFromDisk:$true -Confirm:$false
            }
    }
    if ($TargetDS.FreeSpaceMB -lt $TargetDSReady) {
    $Capacity = $BackpTargetDS.CapacityMB
    $Free15Proc = ($Capacity)*15/100
    $MovedVMSizeMB = ($Source.UsedSpaceGB)*1024
    $BackpTargetDSReady = ($Free15Proc)+($MovedVMSizeMB)
            if ($BackpTargetDS.FreeSpaceMB -ge $BackTargetDSReady)
            {Move-VM -Datastore $BackpTargetDS -VM $Source -ErrorAction SilentlyContinue
            ##line below checks if migrated VM is still powered on and if the storage path of VM is not the Source Datastore #
            if ((Get-VM $Source).PowerState -eq "PoweredOn" -and (Get-VM $Source | Get-View).summary.get_Config().VmPathName -eq "$SourceDS")
            {
            #Send e-mail that VM has not been moved    #
            Write-Host "Migration of $Source failed"
            Send-MailMessage -To email@yourdomain.com -From email@yourdomain.com -SmtpServer smtpserver.yourdomain.com -Subject "STATUS of $Source Migration" -Body "Migration of $Source failed"
            }
            if ((Get-VM $Source).PowerState -eq "PoweredOn" -and (Get-VM $Source | Get-View).summary.get_Config().VmPathName -ne "$SourceDS")
            { Write-Host "SVMotion of $Source was successful"
            Send-MailMessage -To email@yourdomain.com -From email@yourdomain.com -SmtpServer smtpserver.yourdomain.com -Subject "STATUS of $Source Migration" -Body "$Source Migration was successful - $Clone is being removed"
            Remove-VM $Clone -DeleteFromDisk:$true -Confirm:$false
            }
        }
    }
            if ($BackpTargetDS.FreeSpaceMB -lt $BackpTargetDSReady)
            {
            #If both Target Datastores do not have enough free space - Send an email - exit script
            Write-Host "Not enough of free space on $TargetDS and $BackpTargetDS. Script terminated"
            Send-MailMessage -To email@yourdomain.com -From email@yourdomain.com -SmtpServer smtpserver.yourdomain.com -Subject "STATUS of $Source Migration" -Body "Not enough of free space on $TargetDS and $BackpTargetDS. Script terminated"
            }
    }
        else {
            #If clone check was not successful - Send an email - exit script
            Write-Host "Cloning of $Source failed"
            Send-MailMessage -To email@yourdomain.com -From email@yourdomain.com -SmtpServer smtpserver.yourdomain.com -Subject "STATUS of $Source Migration" -Body "Cloning of $Source failed "
            }
    }

else
    {#If cloning was not successful - Send an email - exit script
    Write-Host "Cloning of $Source miserably failed"
    Send-MailMessage -To email@yourdomain.com -From email@yourdomain.com -SmtpServer smtpserver.yourdomain.com -Subject "STATUS of $Source Migration" -Body "Cloning of $Source miserably failed "
    }
Disconnect-VIServer -Confirm:$false
Write-Host "End of script"
Attachments
Comments

I've done migrations from Clariion to vMax arrays using a similar type script, but I'm unclear on this script as to why a clone would need to be generated first? Why not just do a straight sVmotion? What's the purpose of the 'transit' lun? Just curious really.

Chris Nakagaki (Zsoldier)

http://tech.zsoldier.com

The transit LUN is only there for our clones in case of emergency. We use this for a DR type situation. What happens if the sVMotion crashes and burns for whatever reason and the original is no longer recoverable? We have the clone, which sits on the transit LUN to move back into production in case of emergency. This is the only reason for the clone.

Ahh, that makes sense. We use Netbackup and vStorage API's for backups, so I'm less worried about that. Probably why it didn't occur to me. Thanks for the explanation.

Chris Nakagaki (Zsoldier)

http://tech.zsoldier.com

Version history
Revision #:
1 of 1
Last update:
‎10-27-2010 09:47 AM
Updated by: