Mass Storage Migrations using vSphere Storage vMotion and PowerCLI

We ran into an issue where we needed an entire SAN frame retired. Problem is, there are several datastores and several guests running on that frame.

I wanted to script it out, which worked just fine. Then, we had more to do, so I edited the script and ran it again. After the third or fourth time, I decided to write a script that takes params via the cli.

Make sure your datastore names are similar, for instance, mine appends ‘_New’ to the end. So my datastores have to be named like this: ‘vmdatastore’ and ‘vmdatastore_New’. It will get all guests on the datastore and migrate them one by one over to the new datastore. When done, just delete the old datastore (or rename it to _Old) and rename the new one to match.

My script:

#lets get our cli args
param([string]$vc = "vc", [string[]]$datastores = "datastores")

#add the snapin, just in case
Add-PSSnapin VMware.VimAutomation.Core

#check to make sure we have both args we need
if (($vc -eq "vc") -or ($datastores -eq "datastores"))
	{
	write-host `n "Migrate-Datastores moves all running VMs from one LUN to a new LUN of the same name with `"_New`" on the end" `n
	Write-host `n "Required parameters are vc and datastore" `n `n "Example: `"Migrate-Datastores -vc vcenterserver -datastores `(`"datastore1`",`"datastore2`",`"datastore3`)`"`"" `n
	break
	}

#connect to VC

Connect-VIServer $vc

write-host "Migrating these datastores:" $datastores

foreach ($datastore in $datastores)
{
    Write-host "Moving all Servers residing on" $datastore
    $servers = get-vm -datastore $datastore
    foreach ($server in $servers)
        {
        $datastore_new = $datastore.ToString() + "_New"
        write-host "Moving" $server "from" $datastore "to" $datastore_new
        Move-VM $server -datastore $datastore_new
        }
}

Write-host "Done moving to new LUNs"

Disconnect-VIServer -confirm:$false

Then, to run it, I simply type this (after saving the file as Migrate-Storage.ps1):

Migrate-Storage -vc vcenterserver -datastores (“datastore1″,”datastore2″,”datastore3″)

This will check datastore1, get all VMs, migrate them to datastore1_New, then move on to the next datastore.

This entry was posted in Scripts, Virtualization and tagged , , , , , , , by Luke. Bookmark the permalink.

About Luke

Hello! My name is Luke Huckaba and I'm an addict. My drug of choice is technology and I'm far from sober. I'm currently a Systems Specialist and specialize in anything Windows-related, Dell server hardware, and Virtualization using VMWare products. In my spare time, when not toying with the above mentioned items, I can be found riding my motorcycle, working on cars, and attempting to play softball. Feel free to shoot me an e-mail with any questions you may have.

One thought on “Mass Storage Migrations using vSphere Storage vMotion and PowerCLI

  1. Pingback: Emptying a VMFS datastore with PowerCLI | Christian's blog

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>