Categories
Sponsors
Archive
Blogroll
Badges
Community
|
Posted in Virtual Machine Manager, Windows Powershell, Windows Server | 1 Comment | 3,322 views | 07/05/2014 22:55
This is the first part of our Azure Monitoring script. It’s just a concept to show you how to monitor your Azure VM via PowerShell. Azure Powershell is not able to give performance data of virtual machines, so we will use remote PowerShell to connect Azure VMs.
My VMs are Windows Server 2012 R2, so Remote PowerShell is enabled by default on that servers. But if you are using earlier version of Windows, you should enable Remote PowerShell.
I simply used “Get-Credential” to get authenticated on Azure VMs but you can also store your password in a file as a plain text and use it to authenticate.
My Azure Subscriptions settings are in “Azure.Credentials.publishsettings” file. You can download it from Azure Website.
Following script is sample codes of VirtualMetric: Azure Module. You can check it from: http://www.virtualmetric.com
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
186
187
188
189
190
191
192
193
194
| ################################
## VirtualMetric: Azure Module
################################
# Debug Path
$DebugPath = "C:\PowerShell\debug.txt"
# Debug Preference
$ErrorActionPreference = "SilentlyContinue";
# Import Azure PowerShell Module
Import-Module Azure -EA Stop
# Import Azure Settings
Import-AzurePublishSettingsFile "C:\PowerShell\Azure.Credentials.publishsettings" -EA Stop
# Azure VM Credentials
$Credentials = Get-Credential
while ($True)
{
# Execution Time Calculation
$PoSHStartTime = Get-Date
# Start Work
Write-Host "Starting job.."
# Get Azure Virtual Machines
$VMs = Get-AzureVM | Where ServiceName -eq "AzureWebFarm"
foreach ($VM in $VMs)
{
# Clear Variables
$VMDetails = $Null;
$AzureEndpoints = $Null;
$VMName = $Null;
$VMServiceName = $Null;
$VMIPAddress = $Null;
$VMAvailabilitySetName = $Null;
$VMInstanceSize = $Null;
$VMPowerState = $Null;
$VMDNSAddress = $Null;
$VMDNSName = $Null;
$VMWinRMCertThumbprint = $Null;
$CheckAzureVMCert = $Null;
$OSInformation = $Null;
$CPUInformation = $Null;
# Get Azure VM Details
$VMDetails = Get-AzureVM -ServiceName $VM.ServiceName -Name $VM.Name
# Get Azure VM Information
$VMName = $VMDetails.Name
$VMServiceName = $VMDetails.ServiceName
$VMIPAddress = $VMDetails.IPAddress
$VMAvailabilitySetName = $VMDetails.AvailabilitySetName
$VMInstanceSize = $VMDetails.InstanceSize
$VMPowerState = $VMDetails.PowerState
$VMDNSAddress = $VMDetails.DNSName
$VMWinRMCertThumbprint = $VMDetails.VM.DefaultWinRMCertificateThumbprint
# Get Azure VM DNS Name
$VMDNSName = $VMDNSAddress -match "(?<=\//)(.*?)(?=\/)"
$VMDNSName = $Matches[0]
# Parameter Validation
if (!$VMAvailabilitySetName) { $VMAvailabilitySetName = "Non-HA" }
Write-Host " "
Write-Host " "
Write-Host "VM Details:"
Write-Host " "
Write-Host "VM Name: $VMName"
Write-Host "VM ServiceName: $VMServiceName"
Write-Host "VM IP Address: $VMIPAddress"
Write-Host "VM Availability SetName: $VMAvailabilitySetName"
Write-Host "VM Instance Size: $VMInstanceSize"
Write-Host "VM Power State: $VMPowerState"
Write-Host "VM DNS Address: $VMDNSAddress"
Write-Host "VM DNS Name: $VMDNSName"
Write-Host "VM WinRM Certificate Thumbprint: $VMWinRMCertThumbprint"
# Get Azure VM Endpoints
$AzureEndpoints = $VMDetails | Get-AzureEndpoint
Write-Host " "
Write-Host " "
Write-Host "VM Endpoint Details:"
foreach ($AzureEndpoint in $AzureEndpoints)
{
# Clear Values
$AzureEndpointLBSetName = $Null;
$AzureEndpointProbePort = $Null;
$AzureEndpointProbeProtocol = $Null;
# Get Azure Endpoint Details
$AzureEndpointName = $AzureEndpoint.Name
$AzureEndpointLocalPort = $AzureEndpoint.LocalPort
$AzureEndpointLBSetName = $AzureEndpoint.LBSetName
$AzureEndpointVip = $AzureEndpoint.Vip
$AzureEndpointPort = $AzureEndpoint.Port
$AzureEndpointProtocol = $AzureEndpoint.Protocol
$AzureEndpointProbePort = $AzureEndpoint.ProbePort
$AzureEndpointProbeProtocol = $AzureEndpoint.ProbeProtocol
# Parameter Validation
if (!$AzureEndpointLBSetName) { $AzureEndpointLBSetName = "Non-HA" }
if (!$AzureEndpointProbePort) { $AzureEndpointProbePort = "Non-HA" }
if (!$AzureEndpointProbeProtocol) { $AzureEndpointProbeProtocol = "Non-HA" }
Write-Host " "
Write-Host "Azure Endpoint Name: $AzureEndpointName"
Write-Host "Azure Endpoint Local Port: $AzureEndpointLocalPort"
Write-Host "Azure Endpoint LB SetName: $AzureEndpointLBSetName"
Write-Host "Azure Endpoint Vip: $AzureEndpointVip"
Write-Host "Azure Endpoint Port: $AzureEndpointPort"
Write-Host "Azure Endpoint Protocol: $AzureEndpointProtocol"
Write-Host "Azure Endpoint Probe Port: $AzureEndpointProbePort"
Write-Host "Azure Endpoint Probe Protocol: $AzureEndpointProbeProtocol"
}
# Check Azure VM Certificate
$CheckAzureVMCert = Get-ChildItem -Path Cert:\LocalMachine\Root | Where Thumbprint -eq $VMWinRMCertThumbprint
if (!$CheckAzureVMCert)
{
# Get Azure VM Certificate
$AzureX509Cert = Get-AzureCertificate -ServiceName $VMServiceName -Thumbprint $VMWinRMCertThumbprint -ThumbprintAlgorithm sha1
# Save Certificate to Temp File
$CertTempFile = [IO.Path]::GetTempFileName()
$AzureX509Cert.Data | Out-File $CertTempFile
# Get Local Certificate File
$LocalCertFile = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $CertTempFile
# Import Local Certificate
$CertStore = New-Object System.Security.Cryptography.X509Certificates.X509Store "Root", "LocalMachine"
$CertStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$CertStore.Add($LocalCertFile)
$CertStore.Close()
# Remove Temp File
Remove-Item $CertTempFile
}
if ($VMPowerState -eq "Started")
{
# Get Azure VM WinRM Uri
$AzureVMWinRMUri = Get-AzureWinRMUri -ServiceName $VMServiceName -Name $VMName
# Get Azure VM Credentials
if (!$Credentials) { $Credentials = Get-Credential }
# Get Azure VM Data
$OSInformation = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {Get-WmiObject -Class "Win32_OperatingSystem"} -Credential $Credentials
$CPUInformation = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {Get-WmiObject -Class "Win32_Processor"} -Credential $Credentials
$DiskInformation = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {Get-WmiObject -Class "Win32_LogicalDisk"} -Credential $Credentials
if ($OSInformation -and $CPUInformation -and $DiskInformation)
{
# Get Azure VM Resource Usage
$AzureVMCPUName = $CPUInformation[0].Name
$AzureVMCPUSocket = @($CPUInformation).Count
$AzureVMCPUCount = $CPUInformation[0].NumberOfCores * $AzureVMCPUSocket
$AzureVMCPULoad = $CPUInformation | Measure-Object -Property LoadPercentage -Sum
$AzureVMCPUUsage = [math]::round(($AzureVMCPULoad.Sum / $AzureVMCPULoad.Count), 0)
$AzureVMTotalMemory = ([math]::round(($OSInformation.TotalVisibleMemorySize / 1MB), 1))
$AzureVMFreeMemory = ([math]::round(($OSInformation.FreePhysicalMemory / 1MB), 1))
$AzureVMTotalDisk = ([math]::round(($DiskInformation.Size / 1GB), 1))
$AzureVMFreeDisk = ([math]::round(($DiskInformation.FreeSpace / 1GB), 1))
$AzureVMTotalDiskUsage = [int]$AzureVMTotalDisk - [int]$AzureVMFreeDisk
$AzureVMDiskReadIO = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {[math]::round(((Get-Counter -Counter "\LogicalDisk(C:)\Disk Reads/Sec").CounterSamples[0].CookedValue), 0)} -Credential $Credentials
$AzureVMDiskWriteIO = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {[math]::round(((Get-Counter -Counter "\LogicalDisk(C:)\Disk Writes/Sec").CounterSamples[0].CookedValue), 0)} -Credential $Credentials
Write-Host " "
Write-Host "CPU Name: $AzureVMCPUName"
Write-Host "CPU Count: $AzureVMCPUCount"
Write-Host "CPU Usage: $AzureVMCPUUsage"
Write-Host "Total Memory: $AzureVMTotalMemory GB"
Write-Host "Free Memory: $AzureVMFreeMemory GB"
Write-Host "Total Disk: $AzureVMTotalDisk GB"
Write-Host "Free Disk: $AzureVMFreeDisk GB"
Write-Host "Disk Read IO: $AzureVMDiskReadIO"
Write-Host "Disk Write IO: $AzureVMDiskWriteIO"
}
}
}
# Prepare for Next Job
Write-Host " "
Start-Sleep 60
} |
################################
## VirtualMetric: Azure Module
################################
# Debug Path
$DebugPath = "C:\PowerShell\debug.txt"
# Debug Preference
$ErrorActionPreference = "SilentlyContinue";
# Import Azure PowerShell Module
Import-Module Azure -EA Stop
# Import Azure Settings
Import-AzurePublishSettingsFile "C:\PowerShell\Azure.Credentials.publishsettings" -EA Stop
# Azure VM Credentials
$Credentials = Get-Credential
while ($True)
{
# Execution Time Calculation
$PoSHStartTime = Get-Date
# Start Work
Write-Host "Starting job.."
# Get Azure Virtual Machines
$VMs = Get-AzureVM | Where ServiceName -eq "AzureWebFarm"
foreach ($VM in $VMs)
{
# Clear Variables
$VMDetails = $Null;
$AzureEndpoints = $Null;
$VMName = $Null;
$VMServiceName = $Null;
$VMIPAddress = $Null;
$VMAvailabilitySetName = $Null;
$VMInstanceSize = $Null;
$VMPowerState = $Null;
$VMDNSAddress = $Null;
$VMDNSName = $Null;
$VMWinRMCertThumbprint = $Null;
$CheckAzureVMCert = $Null;
$OSInformation = $Null;
$CPUInformation = $Null;
# Get Azure VM Details
$VMDetails = Get-AzureVM -ServiceName $VM.ServiceName -Name $VM.Name
# Get Azure VM Information
$VMName = $VMDetails.Name
$VMServiceName = $VMDetails.ServiceName
$VMIPAddress = $VMDetails.IPAddress
$VMAvailabilitySetName = $VMDetails.AvailabilitySetName
$VMInstanceSize = $VMDetails.InstanceSize
$VMPowerState = $VMDetails.PowerState
$VMDNSAddress = $VMDetails.DNSName
$VMWinRMCertThumbprint = $VMDetails.VM.DefaultWinRMCertificateThumbprint
# Get Azure VM DNS Name
$VMDNSName = $VMDNSAddress -match "(?<=\//)(.*?)(?=\/)"
$VMDNSName = $Matches[0]
# Parameter Validation
if (!$VMAvailabilitySetName) { $VMAvailabilitySetName = "Non-HA" }
Write-Host " "
Write-Host " "
Write-Host "VM Details:"
Write-Host " "
Write-Host "VM Name: $VMName"
Write-Host "VM ServiceName: $VMServiceName"
Write-Host "VM IP Address: $VMIPAddress"
Write-Host "VM Availability SetName: $VMAvailabilitySetName"
Write-Host "VM Instance Size: $VMInstanceSize"
Write-Host "VM Power State: $VMPowerState"
Write-Host "VM DNS Address: $VMDNSAddress"
Write-Host "VM DNS Name: $VMDNSName"
Write-Host "VM WinRM Certificate Thumbprint: $VMWinRMCertThumbprint"
# Get Azure VM Endpoints
$AzureEndpoints = $VMDetails | Get-AzureEndpoint
Write-Host " "
Write-Host " "
Write-Host "VM Endpoint Details:"
foreach ($AzureEndpoint in $AzureEndpoints)
{
# Clear Values
$AzureEndpointLBSetName = $Null;
$AzureEndpointProbePort = $Null;
$AzureEndpointProbeProtocol = $Null;
# Get Azure Endpoint Details
$AzureEndpointName = $AzureEndpoint.Name
$AzureEndpointLocalPort = $AzureEndpoint.LocalPort
$AzureEndpointLBSetName = $AzureEndpoint.LBSetName
$AzureEndpointVip = $AzureEndpoint.Vip
$AzureEndpointPort = $AzureEndpoint.Port
$AzureEndpointProtocol = $AzureEndpoint.Protocol
$AzureEndpointProbePort = $AzureEndpoint.ProbePort
$AzureEndpointProbeProtocol = $AzureEndpoint.ProbeProtocol
# Parameter Validation
if (!$AzureEndpointLBSetName) { $AzureEndpointLBSetName = "Non-HA" }
if (!$AzureEndpointProbePort) { $AzureEndpointProbePort = "Non-HA" }
if (!$AzureEndpointProbeProtocol) { $AzureEndpointProbeProtocol = "Non-HA" }
Write-Host " "
Write-Host "Azure Endpoint Name: $AzureEndpointName"
Write-Host "Azure Endpoint Local Port: $AzureEndpointLocalPort"
Write-Host "Azure Endpoint LB SetName: $AzureEndpointLBSetName"
Write-Host "Azure Endpoint Vip: $AzureEndpointVip"
Write-Host "Azure Endpoint Port: $AzureEndpointPort"
Write-Host "Azure Endpoint Protocol: $AzureEndpointProtocol"
Write-Host "Azure Endpoint Probe Port: $AzureEndpointProbePort"
Write-Host "Azure Endpoint Probe Protocol: $AzureEndpointProbeProtocol"
}
# Check Azure VM Certificate
$CheckAzureVMCert = Get-ChildItem -Path Cert:\LocalMachine\Root | Where Thumbprint -eq $VMWinRMCertThumbprint
if (!$CheckAzureVMCert)
{
# Get Azure VM Certificate
$AzureX509Cert = Get-AzureCertificate -ServiceName $VMServiceName -Thumbprint $VMWinRMCertThumbprint -ThumbprintAlgorithm sha1
# Save Certificate to Temp File
$CertTempFile = [IO.Path]::GetTempFileName()
$AzureX509Cert.Data | Out-File $CertTempFile
# Get Local Certificate File
$LocalCertFile = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2 $CertTempFile
# Import Local Certificate
$CertStore = New-Object System.Security.Cryptography.X509Certificates.X509Store "Root", "LocalMachine"
$CertStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$CertStore.Add($LocalCertFile)
$CertStore.Close()
# Remove Temp File
Remove-Item $CertTempFile
}
if ($VMPowerState -eq "Started")
{
# Get Azure VM WinRM Uri
$AzureVMWinRMUri = Get-AzureWinRMUri -ServiceName $VMServiceName -Name $VMName
# Get Azure VM Credentials
if (!$Credentials) { $Credentials = Get-Credential }
# Get Azure VM Data
$OSInformation = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {Get-WmiObject -Class "Win32_OperatingSystem"} -Credential $Credentials
$CPUInformation = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {Get-WmiObject -Class "Win32_Processor"} -Credential $Credentials
$DiskInformation = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {Get-WmiObject -Class "Win32_LogicalDisk"} -Credential $Credentials
if ($OSInformation -and $CPUInformation -and $DiskInformation)
{
# Get Azure VM Resource Usage
$AzureVMCPUName = $CPUInformation[0].Name
$AzureVMCPUSocket = @($CPUInformation).Count
$AzureVMCPUCount = $CPUInformation[0].NumberOfCores * $AzureVMCPUSocket
$AzureVMCPULoad = $CPUInformation | Measure-Object -Property LoadPercentage -Sum
$AzureVMCPUUsage = [math]::round(($AzureVMCPULoad.Sum / $AzureVMCPULoad.Count), 0)
$AzureVMTotalMemory = ([math]::round(($OSInformation.TotalVisibleMemorySize / 1MB), 1))
$AzureVMFreeMemory = ([math]::round(($OSInformation.FreePhysicalMemory / 1MB), 1))
$AzureVMTotalDisk = ([math]::round(($DiskInformation.Size / 1GB), 1))
$AzureVMFreeDisk = ([math]::round(($DiskInformation.FreeSpace / 1GB), 1))
$AzureVMTotalDiskUsage = [int]$AzureVMTotalDisk - [int]$AzureVMFreeDisk
$AzureVMDiskReadIO = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {[math]::round(((Get-Counter -Counter "\LogicalDisk(C:)\Disk Reads/Sec").CounterSamples[0].CookedValue), 0)} -Credential $Credentials
$AzureVMDiskWriteIO = Invoke-Command -ConnectionUri $AzureVMWinRMUri -ScriptBlock {[math]::round(((Get-Counter -Counter "\LogicalDisk(C:)\Disk Writes/Sec").CounterSamples[0].CookedValue), 0)} -Credential $Credentials
Write-Host " "
Write-Host "CPU Name: $AzureVMCPUName"
Write-Host "CPU Count: $AzureVMCPUCount"
Write-Host "CPU Usage: $AzureVMCPUUsage"
Write-Host "Total Memory: $AzureVMTotalMemory GB"
Write-Host "Free Memory: $AzureVMFreeMemory GB"
Write-Host "Total Disk: $AzureVMTotalDisk GB"
Write-Host "Free Disk: $AzureVMFreeDisk GB"
Write-Host "Disk Read IO: $AzureVMDiskReadIO"
Write-Host "Disk Write IO: $AzureVMDiskWriteIO"
}
}
}
# Prepare for Next Job
Write-Host " "
Start-Sleep 60
}
As you see, after monitoring VMs, I simply output it with Write-Host. But next time, we will use XML to format it.
Posted in Virtual Machine Manager, Windows Powershell | No Comment | 1,606 views | 01/04/2014 10:41
You can search a VM directory in Cluster Shared Volumes (CSV) with following script:
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
| # VM Name
$VMName = "VM Test"
# Get Cluster Nodes
$ClusterNodes = "vmhost01.domain.com","vmhost12.domain.com"
foreach ($ClusterNode in $ClusterNodes)
{
$Volumes = Get-ChildItem -Directory -Path \\$ClusterNode\C$\ClusterStorage\
foreach ($Volume in $Volumes)
{
$ConfigFolders = Get-ChildItem -Directory -Path \\$ClusterNode\C$\ClusterStorage\$Volume
foreach ($ConfigFolder in $ConfigFolders)
{
$Result = "0";
Write-Host $ConfigFolder
if ($ConfigFolder -like "*$VMName*")
{
Write-Host " "
Write-Warning "VMName: $VMName"
Write-Warning "Volume: $Volume"
Write-Warning "VMHost: $ClusterNode"
Write-Host " "
}
}
}
} |
# VM Name
$VMName = "VM Test"
# Get Cluster Nodes
$ClusterNodes = "vmhost01.domain.com","vmhost12.domain.com"
foreach ($ClusterNode in $ClusterNodes)
{
$Volumes = Get-ChildItem -Directory -Path \\$ClusterNode\C$\ClusterStorage\
foreach ($Volume in $Volumes)
{
$ConfigFolders = Get-ChildItem -Directory -Path \\$ClusterNode\C$\ClusterStorage\$Volume
foreach ($ConfigFolder in $ConfigFolders)
{
$Result = "0";
Write-Host $ConfigFolder
if ($ConfigFolder -like "*$VMName*")
{
Write-Host " "
Write-Warning "VMName: $VMName"
Write-Warning "Volume: $Volume"
Write-Warning "VMHost: $ClusterNode"
Write-Host " "
}
}
}
}
This script requires at least PowerShell v3 to work.
Posted in Virtual Machine Manager, Windows Powershell | No Comment | 1,769 views | 31/03/2014 17:04
You can grant a user role to a virtual machine with following command:
1
2
3
| $UserRole = Get-SCUserRole -Name "Test User Role"
$VM | Set-SCVirtualMachine -Owner $UserRole.Members[0].Name -UserRole $UserRole
Grant-SCResource -Resource $VM -UserName $UserRole.Members[0].Name -UserRoleID @($UserRole.ID.Guid) |
$UserRole = Get-SCUserRole -Name "Test User Role"
$VM | Set-SCVirtualMachine -Owner $UserRole.Members[0].Name -UserRole $UserRole
Grant-SCResource -Resource $VM -UserName $UserRole.Members[0].Name -UserRoleID @($UserRole.ID.Guid)
After that you will be able to reach it from System Center App Controller.
Posted in Virtual Machine Manager, Windows Powershell | 2 Comments | 3,774 views | 31/03/2014 16:45
You can use following script to get VM CPU Counts on SCVMM 2012 R2:
1
2
3
4
5
6
7
8
9
| PS C:\Users\yusufozt\Desktop> $Results = Get-VM | Measure-Object -Property CPUCount -Sum
PS C:\Users\yusufozt\Desktop> $Results
Count : 108
Average :
Sum : 292
Maximum :
Minimum :
Property : CPUCount |
PS C:\Users\yusufozt\Desktop> $Results = Get-VM | Measure-Object -Property CPUCount -Sum
PS C:\Users\yusufozt\Desktop> $Results
Count : 108
Average :
Sum : 292
Maximum :
Minimum :
Property : CPUCount
You can get total count by using $Results.Sum value.
Posted in Virtual Machine Manager, Windows Powershell | 2 Comments | 6,053 views | 25/03/2014 16:50
You can get VM list in a VMHostCluster with following command:
1
| ((Get-SCVMHOST -VMHostCluster $Cluster).Name | foreach { Get-VM -VMHost $_ }).Name |
((Get-SCVMHOST -VMHostCluster $Cluster).Name | foreach { Get-VM -VMHost $_ }).Name
That will output all virtual machine names in that cluster.
Posted in Virtual Machine Manager, Windows Powershell, Windows Server | No Comment | 1,738 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 " "
}
} |
# 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 Virtual Machine Manager, Windows Powershell, Windows Server | No Comment | 7,380 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
}
} |
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 |
Get-WinHBAInfo -ComputerName Server01
After that you will see HBA and Port WWN information.
|