This code will fully migrate a powered on VM to another cluster (On the same vcenter)
This will migrate the VMs storage and the memory state with one command.

### Set your vars
$vmname = “MyVM”
$going2Host = “MyHost.pcli.me”
$datastorename = “Datastore-1”

###
Get-VM $vmname | Move-VM -Destination $going2Host -Datastore (get-datastore “$datastorename”) -confirm:$false -RunAsync:$true

 

####### ####### ####### ####### ####### ####### #######
You can use this code if you want to select the datastore cluster instead of a single LUN.
This may error out if you have SDRS enabled on the VM.
Check this code to disable it before running the migration line below or just disable it via the GUI : http://www.pcli.me/?p=159

$vmname = “MyVM”
$going2Host = “MyHost.pcli.me”
$datastoreclustername = “MyDataStoreCluster”

Get-VM $vmname | Move-VM -Destination $going2Host -Datastore (get-datastorecluster “$datastoreclustername”) -confirm:$false -RunAsync:$true

VMware Links:
http://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/html/Move-VM.html

This code will reconfigure your Storage DRS Cluster Settings for a single VM.

### Set your vars ###
$theVM = “MyVM”
$theDatastoreCluster = “MyStorageCluster”

###
$SRMan = Get-View StorageResourceManager
$DSCluster = Get-DatastoreCluster -Name $theDatastoreCluster
$spec = New-Object VMware.Vim.StorageDrsConfigSpec
$DSCluster.ExtensionData.PodStorageDrsEntry.StorageDrsConfig.VmConfig |
where {(Get-View $_.VM | Select -ExpandProperty Name) -like $theVM} | %{
$entry = New-Object VMware.Vim.StorageDrsVmConfigSpec
$entry.Operation = “edit”
$entry.Info = $_
$entry.Info.Enabled = $false
$spec.vmConfigSpec += $entry
}
$SRMan.ConfigureStorageDrsForPod($DSCluster.ExtensionData.MoRef,$spec,$true)