Automating Storage vMotion

Here you can find an automated script for performing an storage vmotion. Basically knowing the current VM and the target DataStore it will use

the Power Cli cmd-lets to start moving a running VM after checking both VM and DataStores exist. An email will be sent before and after the task.

########################################
# Name: vMotion-Me
# Desc: Storage Vmotion for a specific VM
# VM and Target Storage are given by  command line parameters
# Date: 05/10/2010
# Auth: Alberto Ruiz
########################################

# Check commandline syntax
Param($VM_Name, $DS_Name)
If (($VM_Name -eq $Null ) -or ($DS_Name -eq $Null) ) {
cls
Write-Host -ForeGroundColor White “Usage: “
Write-Host -ForeGroundColor Gray ““vMotion-Me ”
Exit }

# Constant Definition
$VM_is_OFF = “PoweredOff”
$VM_is_ON = “PoweredON”
$vCENTER = “vCenterServer”
$smtpServer = “smtp.server”
$timeStart = (Get-Date -f “HH:MM:ss”)
$emailFrom = “PowerCLI-Script@mail.com”
$emailTo = “vCenterAdmin@mail.com”

# PowerCLI Header CmdLets
$VMwareLoaded = $(Get-PSSnapin | ? {$_.Name -like “*VMware*”} ) -ne $null
If (!$VMwareLoaded) { Add-PSSnapin -Name “VMware.VimAutomation.Core” }

# Build my Credentials if your are going to us it very often or automated
$Password = cat C:\Credencial.txt | convertto-securestring
$MyID = New-Object -typename System.Management.Automation.PSCredential -argumentlist “UserName”,$Password

Cls

# Connect VI Server
Write-host -foregroundcolor Yellow “Connecting to vCenter Server …. ”
Connect-VIserver $vCENTER -credential $MyID | OUT-NULL

# Starting mail
$subject = “$VM_Name Migrating Started at $TimeStart”
$body = ”
————————————————–
$VM_NAME Started migrating to $DS_NAME at $TimeStart
————————————————–”

$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom,$emailTo,$subject,$body)

# Main

# Check for the Correct Data
Write-host -foregroundcolor Yellow “Checking for VM Name $VM_Name …. ”
if ( (Get-vm | Where-Object {$_.Name -eq $VM_Name}) -eq $Null )

{
write-host -foregroundcolor red “ERROR: $VM_Name does NOT exist”
Disconnect-VIServer -Confirm:$False
exit

}

Write-host -foregroundcolor Yellow “Checking for DataStore Name $DS_Name …. ”
if ( (Get-DataStore | Where-Object {$_.Name -eq $DS_Name}) -eq $Null )

{
write-host -foregroundcolor red “ERROR: $DS_Name does NOT exist”
Disconnect-VIServer -Confirm:$False
exit

}

Get-vm $VM_Name | move-vm -datastore (Get-Datastore $DS_Name)

# Check to Power VM On if needed

$VM_State = Get-VM $VM_Name | Select PowerState

if ( $VM_State.PowerState -eq $VM_is_OFF)

{
Write-Host -foreground Yellow “Powering on VM : $VM_Name”
Start-VM $VM_Name

}
# End Main

# Disconnect-Vi Server
Disconnect-VIServer -Confirm:$False

#Send Email
$timeEnd = (Get-Date -f “HH:MM:ss”)
$subject = “$VM_Name Successfully moved”
$body = ”
————————————————–
Virtual Machine : $VM_Name
Has been completely moved to DataStore : $DS_Name
Time Started : $timeStart
time Finished : $TimeEnd
————————————————–”

$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.Send($emailFrom,$emailTo,$subject,$body)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s