search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Live Storage Migration of Selected Virtual Machines on Hyper-V via PowerShell
Posted in Virtual Machine Manager, Windows Powershell, Windows Server | No Comment | 804 views | 05/06/2014 07:30

You can migrate selected Virtual Machines in a Hyper-V server with following script.
That will migrate all virtual machines into target volume.
If target volume is not specified, it will look for best available CSV volume, and migrate virtual machine into that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Target Volume
$TargetVolume = "Volume11"
 
# Create VM Array
$VMArray = New-Object System.Collections.ArrayList
$VMArray.Clear();
 
$AddArray = $VMArray.Add("VM01")
$AddArray = $VMArray.Add("VM02")
 
# Get All Virtual Machines
$AllVMs = Get-VM
foreach ($VM in $AllVMs)
{
	# Get VM and Cluster Information
	$VMName = $VM.Name
 
	if ($VMArray.Contains($VMName) -eq $True)
    { 
		Write-Host " "
		Write-Host Working on $VMName ..
 
		if (!$TargetVolume)
		{
			# Get Volume and Memory Information
			$Volume = ((Get-ClusterSharedVolume | Select -ExpandProperty SharedVolumeInfo | Select @{label="Name";expression={(($_.FriendlyVolumeName).Split("\"))[-1]}},@{label="FreeSpace";expression={($_ | Select -Expand Partition).FreeSpace}} | Sort FreeSpace -Descending)[0]).Name
		}
		else
		{
			$Volume = $TargetVolume
		}
 
		# Move Virtual Machine
		Move-VMStorage -VMName $VMName -DestinationStoragePath C:\ClusterStorage\$Volume\$VMName
 
		Write-Host Done.
	}
	else
    {
		Write-Host " "
		Write-Host Skipping $VMName ..
	}
}

This script should work even if you have pass-through disks on virtual machine or virtual hba.



Leave a Reply