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,736 views | 25/03/2014 16:44

You can use this script to move your VMs into another Cloud (Cloud Migration) and Cluster 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Parameters
$DestinationCloud = "New Cloud"
$DestinationHostCluster = "cluster.domain.com"
 
# 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
		if ($DestinationHostCluster)
		{
			$VMHostName = (@(Get-SCVMHost | Where {$_.VMHostGroup -like $HostGroupPath -and $_.HostCluster.Name -eq $DestinationHostCluster -and $_.CoresPerCPU -eq $CurrentVMHost.CoresPerCPU -and $_.CPUArchitecture -eq $CurrentVMHost.CPUArchitecture -and $_.CPUFamily -eq $CurrentVMHost.CPUFamily } | Select Name,AvailableMemory | Sort AvailableMemory -Descending)[0]).Name
		}
		else
		{
			$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
		}
 
		if ($VMHostName)
		{
			# 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 "There is no available host to migrate VM.."
			Write-Host "Skipping migration.."
			Write-Host " "
			Write-Host " "
		}
	}
	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.


Posted in Windows Powershell, Windows Server | No Comment | 2,477 views | 24/03/2014 11:02

You can query network adapter information via WMI. Just add your all hosts into Servers.txt and use following script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$Servers = Get-Content Servers.txt
foreach ($Server in $Servers)
{
	$ErrorActionPreference = "silentlycontinue"
	$Ping = New-Object System.Net.Networkinformation.ping
	$Status = $Null
	$Status = ($Ping.Send("$Server", 1)).Status
	if ($Status -eq "Success")
	{
		ForEach ($Adapter in (Get-WmiObject Win32_NetworkAdapter -ComputerName $Server))
		{  
			$Config = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "Index = '$($Adapter.Index)'" -ComputerName $Server
			if ($Config.IPAddress)
			{
				$Output = $Server + ";" + $Adapter.Name + ";" + $Config.IPAddress[0] + ";" + $Config.MacAddress
				Write-Host $Output
			}
		}
	}
}

After that you will see all network adapters and ip configurations.


Posted in Virtual Machine Manager, Windows Powershell, Windows Server | No Comment | 7,348 views | 17/03/2014 15:51

You can use following script to get HBA Info and Port WWN:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function Get-WinHBAInfo
{
	param ($ComputerName="localhost")
 
	# Get HBA Information
	$Port = Get-WmiObject -ComputerName $ComputerName -Class MSFC_FibrePortHBAAttributes -Namespace "root\WMI"
	$HBAs = Get-WmiObject -ComputerName $ComputerName -Class MSFC_FCAdapterHBAAttributes  -Namespace "root\WMI"
 
	$HBAProperties = $HBAs | Get-Member -MemberType Property, AliasProperty | Select -ExpandProperty name | ? {$_ -notlike "__*"}
	$HBAs = $HBAs | Select $HBAProperties
	$HBAs | %{ $_.NodeWWN = ((($_.NodeWWN) | % {"{0:x2}" -f $_}) -join ":").ToUpper() }
 
	ForEach($HBA in $HBAs) {
 
		# Get Port WWN
		$PortWWN = (($Port |? { $_.instancename -eq $HBA.instancename }).attributes).PortWWN
		$PortWWN = (($PortWWN | % {"{0:x2}" -f $_}) -join ":").ToUpper()
		Add-Member -MemberType NoteProperty -InputObject $HBA -Name PortWWN -Value $PortWWN
 
		# Output
		$HBA
	}
}

Usage:

Get-WinHBAInfo -ComputerName Server01

After that you will see HBA and Port WWN information.


Posted in Windows Powershell, Windows Server | No Comment | 6,212 views | 16/03/2014 08:38

You can use following script to verify your PTR records.

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
# Get Reverse Lookup Zones
$ReverseLookupZones = Get-DnsServerZone | Where IsReverseLookupZone -eq $True | Where IsAutoCreated -eq $False
 
foreach ($ReverseLookupZone in $ReverseLookupZones)
{
	# Clear Variables
	$Servers = $Null;
 
	# Get Zone Information
	$DNSZoneName = $ReverseLookupZone.ZoneName
 
	# Get IP Information
	$ReverseIP = $ReverseLookupZone.ZoneName.TrimEnd(".in-addr.arpa");
	$ReverseIPSuffix = $ReverseIP.Split(".")
	[array]::reverse($ReverseIPSuffix)
	$ReverseIPSuffix = $ReverseIPSuffix -join "."
 
	# Get Servers
	$Servers = Get-DnsServerResourceRecord -ZoneName $DNSZoneName | Where HostName -ne "@"
 
	foreach ($Server in $Servers)
	{		
		# Get Server IP Address
		$ServerHostName = $Server.HostName
		$ServerIPSuffix = $ServerHostName.Split(".")
		[array]::reverse($ServerIPSuffix)
		$ServerIPSuffix = $ServerIPSuffix -join "."
		$ServerIPAddress = $ReverseIPSuffix + "." + $ServerIPSuffix
 
		# Get Server DNS Hostname
		$ServerDNSName = $Server.RecordData.PtrDomainName
		$ServerDNSName = $ServerDNSName.TrimEnd(".")
 
		Write-Host Working on $ServerDNSName ..
 
		# Get Server DNS Subnet
		$ServerDNSSubnet = $ServerIPAddress.Split(".")[0] + "." + $ServerIPAddress.Split(".")[1] + "." + $ServerIPAddress.Split(".")[2] + ".0/24"
 
		# Resolve DNS Name
		$DNSName = (Resolve-DnsName $ServerDNSName)
 
		if ($DNSName)
		{
			# Clear Values
			$Control = 0;
 
			foreach ($DNSRecord in $DNSName)
			{
				# Get Reverse DNS Name
				$DNSIPAddress = $DNSRecord.IPAddress
 
				if ($DNSIPAddress -eq $ServerIPAddress)
				{
					$Control = 1;
				}
			}
 
			if ($Control -eq "0")
			{						
				$Output = $ServerIPAddress + ";" + $ServerDNSSubnet + ";" + $ServerDNSName + ";" + $DNSIPAddress
				Add-Content -Value $Output -Path PTRError.txt
				Write-Warning $Output
			}
		}
	}
}

You should run this script on your Windows Server 2012/R2 DNS server with elevated privileges.


Posted in Windows Powershell, Windows Server | 2 Comments | 2,438 views | 14/03/2014 10:09

You can check all reverse DNS records with DNS records with following script to see if they are equal:

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
$Servers = Get-DnsServerResourceRecord -ZoneName contoso.domain.com
 
foreach ($Server in $Servers)
{
	# Clear Values
	$ServerName = $Null;
	$DNSName = $Null;
	$DNSHostName = $Null;
	$DNSIPAddress = $Null;
	$ReverseDNSHostName = $Null;
 
	# Resolve DNS Name
	$ServerName = $Server.HostName
	$DNSName = (Resolve-DnsName $ServerName)
 
	if ($DNSName)
	{	
		# Get Reverse DNS Name
		$DNSHostName = $DNSName.Name
		$DNSIPAddress = $DNSName.IPAddress
		$ReverseDNSHostName = ([System.Net.Dns]::GetHostByAddress("$DNSIPAddress")).HostName
 
		if ($DNSHostName -eq $ReverseDNSHostName -and $ReverseDNSHostName -ne $Null)
		{
			Write-Host "."
		}
		else
		{
			$Output = $DNSHostName + ";" + $ReverseDNSHostName
			Write-Host $Output
		}
	}
	else
	{
		Write-Host "Cannot resolve $ServerName"
	}
}

If they are not equal, that will output results.
You should run this script on a Windows Server 2012/R2 DNS server with elevated privileges.


Posted in Windows Powershell, Windows Server | 1 Comment | 5,880 views | 13/03/2014 11:57

You can use following function to get EMC Disk Numbers via PowerPath:

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
function Get-EMCDiskNumber
{
	param ($DiskID)
 
	function Get-PowerMtOutput
	{	
		# Get Powermt Output
		$Outputs = powermt display dev=all
 
		# Create Hash Table
		$Properties = New-Object Psobject
 
		foreach ($Output in $Outputs)
		{
			# Clear Variables
			$DiskNumber = $Null;
			$DiskID = $Null;
 
			if ($Output -ne "")
			{
				if ($Output -match 'Pseudo name=harddisk(?<Name>\d+)') { $DiskNumber = $Matches["Name"] }
				if ($Output -match 'Logical device ID=(?<ID>\w+)') { $DiskID = $Matches["ID"] }
 
				if ($DiskID) { $Properties | Add-Member Noteproperty DiskID $DiskID }
				if ($DiskNumber) { $Properties | Add-Member Noteproperty DiskNumber $DiskNumber }
			}
			else
			{
				# Output Data
				Write-Output $Properties
 
				# Create New Hash Table
				$Properties = New-Object Psobject
			}
		}
	}
 
	if ($DiskID)
	{
		Get-PowerMtOutput | Where { $_.DiskID -eq $DiskID }
	}
	else
	{
		Get-PowerMtOutput
	}
}

If you know Disk ID, you can use it:

Usage:

Get-EMCDiskNumber -DiskID "11E40"

Also you can display all disk names and ids:

Usage:

Get-EMCDiskNumber

That will output disk informations.


Posted in Windows Powershell, Windows Server | No Comment | 7,775 views | 02/03/2014 23:34

You have a Hyper-V Cluster and you use vHBA on your virtual machines?
Then you may be familiar with this error after restart your virtual machine:

TestVM: Virtual port (C003FF4CC9C5003D) creation failed with a NPIV error (Virtual machine ID 12BE2D01-D693-4488-AA5F-3715CBDA4F10)

Technet gives 2 reasons for that error as a possible cause:

Virtual Port creation failure because the WWPN is still in use because of:
· HBA failing to remove virtual port
· Host unresponsiveness

You can check documentation from here:

Only solution in that case is restarting your Hyper-V server or changing WWNN and WWPN addresses of virtual machine.
But i don’t like both ways because it takes more operator time and requires more service downtime..

So I went a little bit deeper to see what may be the reason. Because I was not able to start my virtual machine.
I installed QLogic SanSurfer on my Hyper-V host to check Physical HBA and Virtual Ports.

Then I noticed something:

Step1

My Virtual Machine was off, but its virtual port was still active on server.
After discovering that, removed that virtual port via QLogic Tools.

Step2

It worked like a charm! I was able to start my virtual machine again.
So I wrote a PowerShell script to find other inactive vHBA ports and remove them.

First, download Hyper-V vHBA PowerShell Module and put it into PowerShell Modules Directory.

You can query your inactive vHBA ports with following command:

Get-InactiveFCVirtualPort

If you want to query a remote Hyper-V server, use following command:

Get-InactiveFCVirtualPort -ComputerName RemoteServerName

That will give you inactive ports as an output:

ComputerName : VMHOST36
vHBAWWNN     : C0:03:FF:00:00:FF:FF:00
vHBAWWPN     : C0:03:FF:F1:25:95:00:0B
pHBAWWNN     : 50:06:0B:00:00:C2:62:A1
pHBAWWPN     : 50:06:0B:00:00:C2:62:A0
 
ComputerName : VMHOST36
vHBAWWNN     : C0:03:FF:00:00:FF:FF:00
vHBAWWPN     : C0:03:FF:F1:25:95:00:09
pHBAWWNN     : 50:06:0B:00:00:C2:62:A3
pHBAWWPN     : 50:06:0B:00:00:C2:62:A2

You can use following command to remove all inactive virtual ports:

Remove-InactiveFCVirtualPort

Of course you can also remove inactive virtual ports on remote server:

Remove-InactiveFCVirtualPort -ComputerName RemoteServerName

After that you should be able to start your virtual machine.

RemovePortsSS

You can also list your all virtual ports and their virtual machine information:

PS C:\Users\yusufozt> Get-FCVirtualPort
 
ComputerName : localhost
VMName       : VMDB01
VMState      : Running
VMSanName    : Switch_A
VMSanSet     : Set A
vHBAWWNN     : C0:03:FF:00:00:FF:FF:00
vHBAWWPN     : C0:03:FF:F1:25:95:00:04
pHBAWWNN     : 50:06:0B:00:00:C2:62:1F
pHBAWWPN     : 50:06:0B:00:00:C2:62:1E
 
ComputerName : localhost
VMName       : VMDB01
VMState      : Running
VMSanName    : Switch_B
VMSanSet     : Set A
vHBAWWNN     : C0:03:FF:00:00:FF:FF:00
vHBAWWPN     : C0:03:FF:F1:25:95:00:06
pHBAWWNN     : 50:06:0B:00:00:C2:62:1D
pHBAWWPN     : 50:06:0B:00:00:C2:62:1C

If you have a Hyper-V cluster, then you can use something like that:

1
2
3
4
5
6
$ClusterNodes = Get-Cluster | Get-ClusterNode
 
foreach ($ClusterNode in $ClusterNodes)
{
     Remove-InactiveFCVirtualPort -ComputerName $ClusterNode
}

That will remove all inactive virtual ports on cluster.

You should run this script on one of your Hyper-V host.