Invoking Storage vMotion from PowerCLI

It is possible to invoke Storage vMotion tasks using PowerCLI by invoking the Move-VM cmdlet and specifying the Datastore and DiskStorageFormat (to specify a new storage format) parameters, as below:

Move-VM <VM> -Datastore <Datastore> -DiskStorageFormat <Thin | Thick | EagerZeroedThick>

I was looking to use the below as part of testing a new storage array, where the VMs targeted would be simulating IO  using the IOmeter utility, as part of my testing I wanted to invoke a Storage vMotion tasks on each of the VMs on a schedule.

I could achieve this by creating a script to invoke the Move-VM cmdlet agasint a collection of VMs and selecting a random datastore as the target:

Firstly, we will connect to the VI Server and build a collection of VMs, in the below example the VMs I wish to invoke the Storage vMotion task were named as VMIOM with a numerical identifier as the suffix. For Example, VMIOM7.

Connect-VIServer server1.domain.local 
$VMs = Get-VM | Where-Object {$_.Name -like "VMIOM*"}

For Each VM returned in the collection, we will get the name of the Datastore and store this as a variable.

ForEach ($VM in $VMs) 
    $Source = (Get-VM $VM | Get-Datastore).Name 

The datastores on the new storage array follow a particular naming convention, in the below example, we will say that they are name SAN4-VMFS with a numerical identifier as the suffix. For Example, SAN4-VMFS12. So will return a collection of datastores where the name is like SAN4-VMFS* and is not equal to the source datastore name, then using the Get-Random cmdlet return a random datastore for the collection and store as a variable.

$Target = Get-Datastore | Where-Object {$_.Name -like "SAN4-VMFS*"-and $_.Name -ne $Source}
$Target = $Target | Get-Random

Now that we have a target datastore we can invoke the Move-VM cmdlet specifying the random Datastore name returned as the target.

Get-VM $VM | Move-VM- Datastore (Get-Datastore $Target)

From the task details pane, you should see that the VM has been relocated as below:

Migrating VMIOM3 from esxi1.domain.local, SAN4-VMFS3 to esxi1.domain.local, SAN4-VMFS11 in Datacenter1 
Migrating VMIOM3 off host esxi1.domain.local in Datacenter1 
Migration of virtual machine VMIOM3 from esxi1.domain.local, SAN4-VMFS3 to esxi1.domain.local, SAN4-VMFS11 completed

Leave a Reply

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

You are commenting using your 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