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

Badges
MCSE
Community

Cozumpark Bilisim Portali
Posted in Virtual Machine Manager, Windows Powershell, Windows Server | No Comment | 1,408 views | 30/09/2013 15:17

You should run this script on Cluster node. Migrates all VM online.

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 = "cluster.domain.com"
 
# 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();
 
# Get VMs and Add to VM Array
$VMs = Get-VM | Where {(($_ | Select -Expand HardDrives).Path -like "Disk*") -or (($_ | Select -expand FibreChannelHostBusAdapters) -ne $Null)}
$AddArray = $VMs | % {$VMArray.Add($_.Name)}
 
# 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 $False)
    {
		# 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 ..
	}
}

After migration, VM will be started on destination host.


Posted in Virtual Machine Manager, Windows Powershell, Windows Server | No Comment | 1,593 views | 30/09/2013 15:15

You should run this script on Cluster node. Migrates all VM by shutting down first.

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
55
56
57
58
59
60
61
62
63
64
65
66
# Configuration
$DestinationCluster = "cluster.domain.com"
 
# 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();
 
# Get VMs and Add to VM Array
$VMs = Get-VM | Where {(($_ | Select -Expand HardDrives).Path -like "Disk*") -or (($_ | Select -expand FibreChannelHostBusAdapters) -ne $Null)}
$AddArray = $VMs | % {$VMArray.Add($_.Name)}
 
# 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 $False)
    {
		# 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
 
		# Stop Virtual Machine
		if ($VMState -ne "Off")
		{
			Stop-VM $VMName -Force
		}
 
		# 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
 
		# Start Virtual Machine
		if ($VMState -ne "Off")
		{
			Start-VM $VMName -ComputerName $DestinationNode
		}
 
		Write-Host Done.
	}
	else
    {
		Write-Host " "
		Write-Host Skipping $VMName ..
	}
}

After migration, VM will be started on destination host.


Posted in Virtual Machine Manager, Windows Powershell | 10 Comments | 16,290 views | 04/04/2013 20:25

Hi guys and ladies! :)

Update: v1.4 is here! Thanks for your feedbacks.

Hyper-V VM Migration Script is ready! Now it’s possible to migrate your VMs from Hyper-V v2 to Hyper-V v3 with quick migration capabilities.

It supports following scenarios:

1. Standalone Hyper-V v2 Host
2. Hyper-V v2 Host in trusted domain
3. Hyper-V v2 Host in untrusted domain

Update v1.4:
1. Fixed network configuration issue on different subnets
2. Improved debugging

Update v1.3:
1. Added support for servers on different subnets

Update v1.2:
1. Linux VM migration support
2. Fixed a bug while getting vhd name
3. Advanced debugging switch (use -DebugMode)

So why it supports untrusted domains? Because you may not want to install Windows Server 2012 Cluster into your existing Windows Server 2008 R2 environment. If you have totally different environment for Hyper-V v3, then this script is your best friend.

Advantages:

1. It supports BITS! It’s ready to move large VHD files.
2. It supports “Rollback“. If there will be any problem, this script will undo any changes!!! :)
3. Licensed in GPLv2. You can use this script in your organization without any licensing issues. Totally free!

How to Install?
1. First download Hyper-V VM Migration Script:
Download Script

2. You can use exe or msi file to installation. I’ve just used migration.exe file to installation.

3. Change Powershell path if you have different drive path.

4. Click Install to begin installation.

5. That’s it! You can start using migration script.

6. Go to Powershell and type following to change execution policy as “AllSigned”.

Set-ExecutionPolicy AllSigned

7. Allow my code publishing certificate. This will prevent you to run untrusted scripts.

New-Migration

8. Installation is done! Now we can start migration of virtual machines.

How to use?

Example 1: If want to migrate VM01 from HV001 (10.10.10.2) to your local Hyper-V v3 host, use following:

New-Migration -VMName "VM01" -VMHost "HV001" -VMPath "C:\ClusterStorage\Volume1"

BTW, -VMPath switch is the destination path. Not your current VM path. This script finds VM path automatically.

Example 2: If HV001 (10.10.10.2) is a standalone machine, use following:

New-Migration -VMName "VM01" -VMHost "10.10.10.2" -Username "Administrator" -Password "123456" -VMPath "C:\ClusterStorage\Volume1"

Example 3: If HV001 (10.10.10.2) is a member of a untrusted domain, use following:

New-Migration -VMName "VM01" -VMHost "10.10.10.2" -Username "Domain\Administrator" -Password "123456" -VMPath "C:\ClusterStorage\Volume1"

Example 4: Having trouble with migration? Use debugging switch (-DebugMode) to see what really makes that problem:

New-Migration -VMName "VM01" -VMHost "10.10.10.2" -VMPath "C:\ClusterStorage\Volume1" -DebugMode

Requirements:

1. You should install this script on to your Hyper-V v3 host.

2. This script doesn’t allow you to migrate VMs if they have snapshots. You should remove them first.

3. Requires Powershell v3. (It comes by default on Windows Server 2012)

4. Hyper-V v2 host and Hyper-V v3 host should have same network switch names. If they are different, this script connects first external network to your virtual machine.

Thanks for using Hyper-V VM Migration script!

-Yusuf.