Loop through and do storage migration on Hyper-V

To migrate or move storage of a VM in Hyper-V, we can simply write a foreach-loop to run Move-VMStorage to all VMs in an array.

To get an array of your target VMs, do:

$vms = Get-VM | Where-Object {( $_.Name -Match 'Static' )}

$vmarray = $vms.VMName

If you type $vmarray in the same powershell window, you will see the VMs that will be targeted.

To target the array and move storage, do:

 foreach($vm in $vmarray) {

 Move-VMStorage -VMName "$vm" -DestinationStoragePath "R:\VHD\$vm" -asjob

 Start-Sleep 180

 Write-Output "Waiting 3 mins for $vm"

}

Sometime, this stops at a single VM for unknown reason. To check if all storage has been migrated as intended. Run the following:

Get-VM -Name Static* | Select-Object -ExpandProperty HardDrives

Get-VM -Name Static* | Select-Object -ExpandProperty HardDrives | foreach { $_.Path }

If it somehow failed to Move-VMStorage on some VMs. Just run the foreach loop again.



Kommentarer