VMware Cloud Community
manis0728
Contributor
Contributor

script for SVmotion the VMs to different datastore

Hi All,

I have a necessity to move VMs from one datastore to another datastores in the same cluster.

The script needs to identify the available free datastore and place the VM.

Have anyone used the script to do svmotion? if possible, can you pls share.

Thanks,

MS

Tags (3)
0 Kudos
2 Replies
LucD
Leadership
Leadership

To clarify, you have multiple target datastores and you want to check if there is sufficient free space available before the move ?

And the VM should be moved to the datastore with the most free space ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Nerenther
Contributor
Contributor

You could do something like this:

Get-VM -Datastore "datastore1" | Move-VM -Datastore (Get-VMHost -Location 'cluster1' | Select-Object -First 1 | Get-Datastore | Where-Object {($_.Name -ne 'datastore1') -and ($_.FreeSpaceGB -gt '500')} | Get-Random)

This will move VMs from datastore1 to a random datastore in cluster1 with more than 500GB space available.

Swap out datastore1 and cluster1 with the datastore you are moving from and the cluster, and 500 with the amount of free space (in GB) you want the destination datastore to have.

In case you don't want to move to a random datastore, but always use the one with the most free space available, you can swap out Get-Random with Sort-Object FreeSpaceGB -Descending | Select-Object -First 1.

Like this:

Get-VM -Datastore "datastore1" | Move-VM -Datastore (Get-VMHost -Location 'cluster1' | Select-Object -First 1 | Get-Datastore | Where-Object {($_.Name -ne 'datastore1') -and ($_.FreeSpaceGB -gt '500')} | Sort-Object FreeSpaceGB -Descending | Select-Object -First 1)

0 Kudos