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

Badges
MCSE
Community

Cozumpark Bilisim Portali
Live Migration between Hyper-V Clusters of Selected Virtual Machines
Posted in Windows Powershell, Windows Server | No Comment | 1,591 views | 26/10/2013 20:44

You can use this script to move only selected VMs live between Hyper-V Clusters.

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
44
45
46
47
48
49
50
51
52
53
54
# Configuration
$DestinationCluster = "HyperVCluster"
 
# Get Host and Cluster Information
$Hostname = (Get-VMHost).Name
$SourceCluster = (Get-Cluster $Hostname).Name
 
# Create VM Array
$VMArray = New-Object System.Collections.ArrayList
$VMArray.Clear();
 
$AddArray = $VMArray.Add("VM01")
$AddArray = $VMArray.Add("VM02")
$AddArray = $VMArray.Add("VM03")
 
# Get All Virtual Machines
$AllVMs = Get-VM
foreach ($VM in $AllVMs)
{
	# Get VM and Cluster Information
	$VMName = $VM.Name
	$HyperVHost = $VM.ComputerName
	$VMState = $VM.State
	$ClusterGroup = Get-ClusterGroup | where Name -like "*$VMName*"
 
	if ($VMArray.Contains($VMName) -eq $True)
    {
		# Remove from Cluster Resources
		if ($ClusterGroup)
		{
			Get-ClusterGroup $ClusterGroup.Name -Cluster $SourceCluster | Remove-ClusterGroup -RemoveResources -Force
		}
 
		Write-Host " "
		Write-Host Working on $VMName ..
 
		# Get Volume and Memory Information
		$Volume = ((Get-ClusterSharedVolume -Cluster $DestinationCluster | Select -ExpandProperty SharedVolumeInfo | Select @{label="Name";expression={(($_.FriendlyVolumeName).Split("\"))[-1]}},@{label="FreeSpace";expression={($_ | Select -Expand Partition).FreeSpace}} | Sort FreeSpace -Descending)[0]).Name
		$DestinationNode = ((Get-Cluster $DestinationCluster | Get-ClusterNode | Select Name,@{label="FreeMemory";expression={([math]::round(((Get-WmiObject -ComputerName $_.Name -Class Win32_OperatingSystem).FreePhysicalMemory / 1KB), 0))}} | Sort FreeMemory -Descending)[0]).Name
 
		# Move Virtual Machine
		Move-VM $VMName -ComputerName $HyperVHost -DestinationHost $DestinationNode -IncludeStorage -DestinationStoragePath C:\ClusterStorage\$Volume\$VMName
 
		# Add to Destination Cluster Resources
		Add-VMToCluster $VMName -Cluster $DestinationCluster
 
		Write-Host Done.
	}
	else
    {
		Write-Host " "
		Write-Host Skipping $VMName ..
	}
}

You can add many VMs into array. Script will skip other VMs.



Leave a Reply