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

Badges
MCSE
Community

Cozumpark Bilisim Portali
Get Similar Virtual Machines in Hyper-V Clusters via PowerShell
Posted in Virtual Machine Manager, Windows Powershell | No Comment | 2,241 views | 21/02/2014 10:49

Following script could be used to find similar virtual machines in same Cluster or Hyper-V Host.
This is very useful to split virtual machines if they have same workloads.

For example if you have two virtual machines with same workload like:

IISServer01
IISServer02

This script will find them and list them with a useful output. You should use it on SCVMM 2012 R2 Console:

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
function Get-SimilarVM
{
<#
    .SYNOPSIS
 
        Gets similar virtual machines in same cluster, host or cloud
 
    .EXAMPLE
 
        Get-SimilarVM
 
    .EXAMPLE
 
        Get-SimilarVM -ClusterName "VMHOSTCluster01"
 
    .EXAMPLE
 
        Get-SimilarVM -VMHostName "VMHOST01"
 
    .NOTES
 
        Author: Yusuf Ozturk
        Website: http://www.yusufozturk.info
        Email: ysfozy@gmail.com
        Date created: 20-Feb-2014
        Last modified: 21-Feb-2014
        Version: 1.1
 
    .LINK
 
        http://www.yusufozturk.info
 
#>
 
[CmdletBinding(SupportsShouldProcess = $true)]
param (
    [Parameter(
        Mandatory = $false,
        HelpMessage = 'Cluster Name')]
    [string]$ClusterName,
 
    [Parameter(
        Mandatory = $false,
        HelpMessage = 'Hyper-V Host')]
    [string]$VMHostName,
 
	[Parameter(
        Mandatory = $false,
        HelpMessage = 'Debug Mode')]
    [switch]$DebugMode = $false
)
	# Enable Debug Mode
	if ($DebugMode)
	{
		$DebugPreference = "Continue"
	}
	else
	{
		$ErrorActionPreference = "silentlycontinue"
	}
 
	# Create VM Array
	$VMArray = New-Object System.Collections.ArrayList
 
	if ($VMHostName)
	{
		# Get VMs
		$VMs = Get-SCVMHost -ComputerName $VMHostName | Get-VM
 
		# Clear VM Array
		$VMArray.Clear();
 
		foreach ($VM in $VMs)
		{
			# Get VM Subname
			$VMName = $VM.Name
			$VMSubName = $VMName.Substring(0,(($VMName.Length)-2))
 
			# Find Similar VMs
			$SimilarVMs = $VMs | Where Name -like "$VMSubName*"
 
			# Similar VM Count
			$SimilarVMCount = $SimilarVMs.Count
 
			if ($SimilarVMCount -gt "1")
			{
				foreach ($SimilarVM in $SimilarVMs)
				{
					$SimilarVMName = $SimilarVM.Name
					if ($VMArray.Contains("$SimilarVMName") -ne $True)
					{
						# Update VM Array
						$UpdateVMArray = $VMArray.Add("$SimilarVMName")
 
						# VM Total Size
						$VMTotalSize = [math]::round(($SimilarVM.TotalSize/1GB), 0)
 
						$Properties = New-Object Psobject
						$Properties | Add-Member Noteproperty Name $SimilarVM.Name
						$Properties | Add-Member Noteproperty VMHost $SimilarVM.VMHost.Name
						$Properties | Add-Member Noteproperty Cluster $Cluster.Name
						$Properties | Add-Member Noteproperty Cloud $SimilarVM.Cloud.Name
						$Properties | Add-Member Noteproperty UserRole $SimilarVM.UserRole.Name
						$Properties | Add-Member Noteproperty CPUCount $SimilarVM.CPUCount
						$Properties | Add-Member Noteproperty Memory $SimilarVM.Memory
						$Properties | Add-Member Noteproperty TotalSize $VMTotalSize
						$Properties | Add-Member Noteproperty BootDiskResource $SimilarVM.DiskResources[0].Name
						$Properties | Add-Member Noteproperty Status $SimilarVM.Status
						$Properties | Add-Member Noteproperty HasVirtualFibreChannelAdapters $SimilarVM.HasVirtualFibreChannelAdapters
						$Properties | Add-Member Noteproperty HasPassthroughDisk $SimilarVM.HasPassthroughDisk
						$Properties | Add-Member Noteproperty HasSharedStorage $SimilarVM.HasSharedStorage
						Write-Output $Properties
					}
				}
			}
		}
	}
	else
	{
		if ($ClusterName)
		{
			# Get Clusters
			$Clusters = Get-SCVMHostCluster -Name "$ClusterName"
		}
		else
		{
			# Get Clusters
			$Clusters = Get-SCVMHostCluster
		}
 
		foreach ($Cluster in $Clusters)
		{
			# Get VMs
			$VMs = Get-SCVMHost -VMHostCluster $Cluster | Get-VM
 
			# Clear VM Array
			$VMArray.Clear();
 
			foreach ($VM in $VMs)
			{
				# Get VM Subname
				$VMName = $VM.Name
				$VMSubName = $VMName.Substring(0,(($VMName.Length)-2))
 
				# Find Similar VMs
				$SimilarVMs = $VMs | Where Name -like "$VMSubName*"
 
				# Similar VM Count
				$SimilarVMCount = $SimilarVMs.Count
 
				if ($SimilarVMCount -gt "1")
				{
					foreach ($SimilarVM in $SimilarVMs)
					{
						$SimilarVMName = $SimilarVM.Name
						if ($VMArray.Contains("$SimilarVMName") -ne $True)
						{
							# Update VM Array
							$UpdateVMArray = $VMArray.Add("$SimilarVMName")
 
							# VM Total Size
							$VMTotalSize = [math]::round(($SimilarVM.TotalSize/1GB), 0)
 
							$Properties = New-Object Psobject
							$Properties | Add-Member Noteproperty Name $SimilarVM.Name
							$Properties | Add-Member Noteproperty VMHost $SimilarVM.VMHost.Name
							$Properties | Add-Member Noteproperty Cluster $Cluster.Name
							$Properties | Add-Member Noteproperty Cloud $SimilarVM.Cloud.Name
							$Properties | Add-Member Noteproperty UserRole $SimilarVM.UserRole.Name
							$Properties | Add-Member Noteproperty CPUCount $SimilarVM.CPUCount
							$Properties | Add-Member Noteproperty Memory $SimilarVM.Memory
							$Properties | Add-Member Noteproperty TotalSize $VMTotalSize
							$Properties | Add-Member Noteproperty BootDiskResource $SimilarVM.DiskResources[0].Name
							$Properties | Add-Member Noteproperty Status $SimilarVM.Status
							$Properties | Add-Member Noteproperty HasVirtualFibreChannelAdapters $SimilarVM.HasVirtualFibreChannelAdapters
							$Properties | Add-Member Noteproperty HasPassthroughDisk $SimilarVM.HasPassthroughDisk
							$Properties | Add-Member Noteproperty HasSharedStorage $SimilarVM.HasSharedStorage
							Write-Output $Properties
						}
					}
				}
			}
		}
	}
}

You can define Cluster and Hyper-V Host in function to minimize scope.

For Hyper-V Host Specific Search:

Get-SimilarVM -VMHostName "VMHOST01"

For Cluster Specific Search:

Get-SimilarVM -ClusterName "VMHOSTCluster01"

Searching in all clusters:

Get-SimilarVM

You can customize output to see what you need.



Leave a Reply