Hi,
The easiest Way is Move-VM
Below is a small script that I put together to help me with some VM Migrations I had recently.
I got a lot of help with this from others on this forum so thanks to those guys.
The script takes a list of VM Names from a text file and migrates them randomly to DataStores of your choosing.
i.e
- if your datastore names are DataStore1, DataStore2, Datastore3, LabStorage1, LabStorage2, TestStorage1
- you want to move from TestStorage1 to one of the "DataStoreX" locations, in the script, set $storage equal to DataStore
- The Script checks the amount of free space on all the "DataStoreX" locations and makes sure that it's greater than Provisioned Space + 20%
- If the amount of free space is ok, VM's are randomly moved to the new locations
Hope this helps
$filelocation = Read-Host "Please enter the name of the target file, include the full path, E.g. c:\Temp\List.txt"
$VMs = get-vm (Get-Content $filelocation)
$storage = Get-Datastore | where {$_.name -match “Your_DataStore_Name_Goes_here"}
foreach ($v in $VMs) {
$random_Datastore_in_array = Get-Datastore -Name $storage.name | where {$_.freespacegb -GE ($v.provisionedspaceGB*1.20)} | Get-Random -Count 1
Write-Host "Storage vMotion of $v to $random_Datastore_in_array beginning"
Move-VM -VM $v -Datastore $random_Datastore_in_array -Confirm:$false
Get-VM $v | Get-Datastore |
select @{N="Date";E={(Get-Date).ToString()}},
@{N="VM";E={($v).Name}},
@{N="LUN";E={$random_Datastore_in_array.Name}},
@{N="FreeSpace (GB)";E={[math]::Round($random_Datastore_in_array.FreeSpaceGB)}} |
Export-Csv c:\Temp\Storage_migrations.csv -NoTypeInformation -Append
}
C:\Temp\Storage_migrations.csv