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,905 views | 06/06/2014 08:39

You can migrate selected Virtual Machines in a Hyper-V Cluster 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
44
45
46
47
# Target Volume
$TargetVolume = "Volume1"
 
# Create VM Array
$VMArray = New-Object System.Collections.ArrayList
$VMArray.Clear();
 
$AddArray = $VMArray.Add("VM01")
$AddArray = $VMArray.Add("VM02")
$AddArray = $VMArray.Add("VM03")
 
# Get Cluster Nodes
$ClusterNodes = Get-Cluster | Get-ClusterNode
 
foreach ($ClusterNode in $ClusterNodes)
{
	# Get All Virtual Machines
	$VMs = Get-VM -ComputerName "$ClusterNode"
 
	foreach ($VM in $VMs)
	{
		# Get VM Information
		$VMName = $VM.Name
 
		if ($VMArray.Contains($VMName) -eq $True)
		{ 
			Write-Host " "
			Write-Host "Working on $VMName .."
			Write-Host "Hyper-V Host: $ClusterNode"
 
			if (!$TargetVolume)
			{
				# Get Volume 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 -ComputerName "$ClusterNode" -VMName "$VMName" -DestinationStoragePath "C:\ClusterStorage\$Volume\$VMName"
 
			Write-Host "Done."
		}
	}
}

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


Posted in Virtual Machine Manager, Windows Powershell, Windows Server | No Comment | 895 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.