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.

Tags: , , , , , , ,

// // // //

One Response to Mass Storage Migrations using vSphere Storage vMotion and PowerCLI

Leave a reply

 

Designed by ThepHuck
Wordpress Themes
Scroll to Top