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 | 3 Comments | 7,857 views | 16/03/2014 08:12

Due to WMI changes on Hyper-V Server 2012 R2, you need to use following script to get Virtual Machine info.

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
function Get-VMGuestInfo
{
<#
    .SYNOPSIS
 
        Gets virtual machine guest information
 
    .EXAMPLE
 
        Get-VMGuestInfo -VMName Test01
 
    .EXAMPLE
 
        Get-VMGuestInfo -VMName Test01 -HyperVHost Host01
 
    .NOTES
 
        Author: Yusuf Ozturk
        Website: http://www.yusufozturk.info
        Email: ysfozy[at]gmail.com
 
#>
 
[CmdletBinding(SupportsShouldProcess = $true)]
param (
 
    [Parameter(
        Mandatory = $true,
        HelpMessage = 'Virtual Machine Name')]
    $VMName,
 
    [Parameter(
        Mandatory = $false,
        HelpMessage = 'Hyper-V Host Name')]
    $HyperVHost = "localhost",
 
	[Parameter(
        Mandatory = $false,
        HelpMessage = 'Debug Mode')]
    [switch]$DebugMode = $false
)
	# Enable Debug Mode
	if ($DebugMode)
	{
		$DebugPreference = "Continue"
	}
	else
	{
		$ErrorActionPreference = "silentlycontinue"
	}
 
	$VMState = (Get-VM -ComputerName $HyperVHost -Name $VMName).State
 
	if ($VMState -eq "Running")
	{
		filter Import-CimXml
		{
			$CimXml = [Xml]$_
			$CimObj = New-Object -TypeName System.Object
			foreach ($CimProperty in $CimXml.SelectNodes("/INSTANCE/PROPERTY"))
			{
				if ($CimProperty.Name -eq "Name" -or $CimProperty.Name -eq "Data")
				{
					$CimObj | Add-Member -MemberType NoteProperty -Name $CimProperty.NAME -Value $CimProperty.VALUE
				}
			}
			$CimObj
		}
 
		$VMConf = Get-WmiObject -ComputerName $HyperVHost -Namespace "root\virtualization\v2" -Query "SELECT * FROM Msvm_ComputerSystem WHERE ElementName like '$VMName' AND caption like 'Virtual%' "
		$KVPData = Get-WmiObject -ComputerName $HyperVHost -Namespace "root\virtualization\v2" -Query "Associators of {$VMConf} Where AssocClass=Msvm_SystemDevice ResultClass=Msvm_KvpExchangeComponent"
		$KVPExport = $KVPData.GuestIntrinsicExchangeItems
 
		if ($KVPExport)
		{
			# Get KVP Data
			$KVPExport = $KVPExport | Import-CimXml
 
			# Get Guest Information
			$VMOSName = ($KVPExport | where {$_.Name -eq "OSName"}).Data
			$VMOSVersion = ($KVPExport | where {$_.Name -eq "OSVersion"}).Data
			$VMHostname = ($KVPExport | where {$_.Name -eq "FullyQualifiedDomainName"}).Data
		}
		else
		{
			$VMOSName = "Unknown"
			$VMOSVersion = "Unknown"
			$VMHostname = "Unknown"
		}
	}
	else
	{
		$VMOSName = "Unknown"
		$VMOSVersion = "Unknown"
		$VMHostname = "Unknown"
	}
 
	$Properties = New-Object Psobject
	$Properties | Add-Member Noteproperty VMName $VMName
	$Properties | Add-Member Noteproperty VMHost $HyperVHost
	$Properties | Add-Member Noteproperty VMState $VMState
	$Properties | Add-Member Noteproperty VMOSName $VMOSName
	$Properties | Add-Member Noteproperty VMOSVersion $VMOSVersion
	$Properties | Add-Member Noteproperty VMHostname $VMHostname
	Write-Output $Properties
}

Usage of this script:

Get-VMGuestInfo -VMName TEST01 -HyperVHost VMHOSTT01

That will output like:

VMName      : TEST01
VMHost      : VMHOSTT01
VMState     : Running
VMOSName    : Windows Server 2008 R2 Enterprise
VMOSVersion : 6.1.7601
VMHostname  : TEST01.domain.contoso.com

This script also works on Hyper-V Server 2012 without R2.


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


Posted in Virtual Machine Manager, Windows Powershell | No Comment | 5,118 views | 19/02/2014 18:21

You can use following script to get VMs with Passthrough Disks on SCVMM 2012 R2.

$VMs = Get-VM | Where HasPassthroughDisk

Then you can get virtual machine hardware information:

$VMs | ft Name,Status,CpuCount,Memory,TotalSize

You will see pretty good output :)


Posted in Virtual Machine Manager, Windows Powershell | No Comment | 1,700 views | 19/02/2014 18:16

You can use following script to get VMs with vHBA on SCVMM 2012 R2.

$VMs = Get-VM | Where HasVirtualFibreChannelAdapters

Then you can get virtual machine hardware information:

$VMs | ft Name,Status,CpuCount,Memory,TotalSize

You will see pretty good output :)


Posted in Virtual Machine Manager, Windows Powershell | No Comment | 2,616 views | 19/02/2014 17:05

You can grant user roles into Virtual Machines for AppController with following script:

1
2
3
4
5
6
7
8
9
$VMs = Get-VM
foreach ($VM in $VMs)
{
	$UserRole = $VM.UserRole.Name
	$UserRole = Get-SCUserRole -Name "$UserRole"
	$UserRoleID = $UserRole.ID.Guid
	$UserName = $VM.Owner
	Grant-SCResource -Resource $VM -UserName $UserName -UserRoleID @("$UserRoleID")
}

That will apply VM’s owner and UserRole as a granted user role.


Posted in Virtual Machine Manager, Windows Powershell | No Comment | 5,900 views | 17/02/2014 14:48

You may see duplicate VMs in SCVMM 2012 R2 after a host crash in a cluster.

In that use you can use following script:

1
Get-VM "DuplicateVM" | Where Cloud -eq $Null | Remove-VM -force

That will remove that VM from SCVMM database only. VM will be online on host after operation.


Posted in Virtual Machine Manager, Windows Powershell | No Comment | 4,452 views | 15/02/2014 12:41

You can use this script to move your VMs into another Cloud (Cloud Migration) on SCVMM 2012 R2.
This script only works on SCVMM 2012 R2 due to vHBA control.
You can remove vHBA control to make it work on SCVMM 2012 SP1.

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
# Parameters
$DestinationCloud = "New Cloud"
 
# Create VM Array
$VMArray = New-Object System.Collections.ArrayList
$VMArray.Clear();
 
# Add VMs into Array
$AddArray = $VMArray.Add("VM01")
$AddArray = $VMArray.Add("VM02")
$AddArray = $VMArray.Add("VM03")
$AddArray = $VMArray.Add("VM04")
 
# Get Destination Cloud
$Cloud = Get-SCCloud -Name $DestinationCloud
 
# Cloud Host Group Path
$HostGroupPath = (($Cloud.HostGroup)[0]).Path + "*"
 
foreach ($VMName in $VMArray)
{
	# Get VM
	$VM = Get-SCVirtualMachine -Name $VMName
 
	# Output
	Write-Host VM Name: $VMName
	Write-Host " "
 
	if ($VM.HasPassthroughDisk -eq $False -and $VM.HasVirtualFibreChannelAdapters -eq $False)
	{
		# Current Cloud
		$CurrentCloud = Get-SCCloud -Name $VM.Cloud.Name
 
		# Current Hyper-V Host
		$CurrentVMHost = Get-SCVMHost -ComputerName $VM.VMHost.Name
 
		# Create Job Guid
		$JobGuid = [System.Guid]::NewGuid().toString()
 
		# Remove from Cloud
		$SetCloud = $VM | Set-SCVirtualMachine -RemoveFromCloud
 
		# Get Best Available Hyper-V Host
		$VMHostName = ((Get-SCVMHost | Where {$_.VMHostGroup -like $HostGroupPath -and $_.CoresPerCPU -eq $CurrentVMHost.CoresPerCPU -and $_.CPUArchitecture -eq $CurrentVMHost.CPUArchitecture -and $_.CPUFamily -eq $CurrentVMHost.CPUFamily } | Select Name,AvailableMemory | Sort AvailableMemory -Descending)[0]).Name
 
		# Output
		Write-Host Target Host: $VMHostName
 
		# Get Best Available CSV
		$VolumeName = ((Get-SCStorageVolume | Where {$_.VMHost -eq $VMHostName -and $_.IsClusterSharedVolume -eq $True} | Select Name,FreeSpace | Sort FreeSpace -Descending)[0]).Name
 
		# Output
		Write-Host Target Volume: $VolumeName
		Write-Host " "
 
		# Get Hyper-V Host Information
		$VMHost = Get-SCVMHost -ComputerName $VMHostName
		[int64]$VMHostAvailableMemory = [int64]$VMHost.AvailableMemory + 10240 		# Leave 10 GB Available Memory
 
		# Get CSV Information
		$Volume = Get-SCStorageVolume -Name $VolumeName -VMHost $VMHostName
		[int64]$VolumeFreeSpace = [int64]$Volume.FreeSpace + 107374182400 			# Leave 100 GB Free Space
 
		# Control Free Memory
		if ($VM.Memory -lt $VMHostAvailableMemory -and $VM.TotalSize -lt $VolumeFreeSpace)
		{
			# Get Virtual Network Adapters
			$VirtualNetworkAdapters = $VM | Get-SCVirtualNetworkAdapter
 
			foreach ($VirtualNetworkAdapter in $VirtualNetworkAdapters)
			{
				# Clear VM Network
				$VMNetwork = $Null;
 
				# Get VM Network
				$VMNetwork = Get-SCVMNetwork | Where {$_.Name -eq $VirtualNetworkAdapter.VMNetwork.Name}
 
				if (!$VMNetwork)
				{
					$VMNetwork = Get-SCVMNetwork | Where {$_.VMSubnet.SubnetVLANs.VLanID -eq $VirtualNetworkAdapter.VLanID}
				}
 
				# Destination Virtual Network
				$VirtualNetwork = ((Get-VM -Cloud $Cloud | Where {$_.VirtualNetworkAdapters.VMNetwork.Name -eq $VirtualNetworkAdapter.VMNetwork.Name -and $_.VirtualNetworkAdapters.VirtualNetwork})[0]).VirtualNetworkAdapters.VirtualNetwork
 
				# Set VM Network Adapter
				$SetSCVirtualNetworkAdapter = Set-SCVirtualNetworkAdapter -VirtualNetworkAdapter $VirtualNetworkAdapter -VirtualNetwork $VirtualNetwork -VMNetwork $VMNetwork -JobGroup $JobGuid
			}
 
			# Move VM
			$MoveSCVirtualMachine = $VM | Move-SCVirtualMachine -VMHost $VMHostName -HighlyAvailable $True -UseLAN -UseDiffDiskOptimization -JobGroup $JobGuid -Path $VolumeName
 
			Write-Host "Migration process is finished."
			Write-Host "Please check job results to ensure that if operation is successful.."
			Write-Host " "
			Write-Host " "
 
			# Set Cloud
			$SetCloud = $VM | Set-SCVirtualMachine -Cloud $Cloud
 
			# Refresh VM
			$RefreshVM = $VM | Refresh-VM
		}
		else
		{
			Write-Host "Not enough resources to move VM.."
			Write-Host "Skipping migration.."
			Write-Host " "
			Write-Host " "
 
			# Set Cloud
			$SetCloud = $VM | Set-SCVirtualMachine -Cloud $CurrentCloud
		}
	}
	else
	{
		Write-Host "VM has Pass-through disks or vHBA.."
		Write-Host "Skipping migration.."
		Write-Host " "
		Write-Host " "
	}
}

After migrations, please check SCVMM job results to see if migrations are successful.