Hyper-V Cluster – Get Nodes Memory Usage

Why

Similar as before (here) having information about your cluster nodes memory usage can be helpful. Especially when creating a bunch of new VMs.

How

  • I can get the information from Failover Cluster Manager (one node at a time):

  • I can get it using Windows Admin Center

  • or I can get it through PowerShell

    The PowerShell way

Because both solutions (Failover Cluster Manager and WAC) lack the flexibility (node by node, or no accurate number) i prefer PowerShell.

As I mentioned before – I tend to use alternative credentials to access critical resources. Unfortunately Get-ClusterNode doesn’t accept Credential parameter. So, despite it’s possible to query nodes directly, I’d rather use Invoke-Command to access cluster information. This way I can connect cross-domain as well. Then, the information I need can be easily retrieved using WMI:

Get-WmiObject -Class win32_operatingsystem -ComputerName $ClusterNode

It’s good to have all active nodes in a cluster queried with one sweep. I’ll be using Select-Object to filter the output. It’s easier to read the code that way (link).

Let’s wrap it into a function:


function Get-ClusterNodeMemory {
<#
.Synopsis
Get the available memory of each node in a cluster.
.DESCRIPTION
Get the available memory of each node in a cluster particularly for Hyper-V.
.PARAMETER Cluster
Provide Cluster Name. Will accept an array of cluter names
.PARAMETER Credential
Optional PSCredential Parameter
.EXAMPLE
Get-ClusterNodeMemory -Cluster Cluster0 | Format-Table -AutoSize
will return all not down nodes memory statistics
Cluster ComputerName FreePhysicalMemory TotalVisibleMemorySize % Free
——- ———— —————— ———————- ——
Cluster0 ClusterNode1 231 512 45,05
Cluster0 ClusterNode2 249 512 48,66
Cluster0 ClusterNode3 195 512 38,01
Cluster0 ClusterNode4 267 512 52,16
Cluster0 ClusterNode5 292 512 57,05
Cluster0 ClusterNode6 172 512 33,64
.EXAMPLE
Get-ClusterNodeMemory -Cluster Cluster0,Cluster1 -Credential (Get-Credential) | Format-Table -AutoSize
Will return all nodes memory statistics from two clusters using provided Credentials
Cluster ComputerName FreePhysicalMemory TotalVisibleMemorySize % Free
——- ———— —————— ———————- ——
Cluster0 ClusterNode1 230 512 45,02
Cluster0 ClusterNode2 249 512 48,65
Cluster0 ClusterNode3 195 512 38
Cluster0 ClusterNode4 267 512 52,16
Cluster0 ClusterNode5 292 512 56,99
Cluster0 ClusterNode6 172 512 33,63
Cluster1 ClusterNode1 1 2 53,53
Cluster1 ClusterNode2 1 2 45,37
#>
[CmdletBinding()]
param(
[Parameter(Mandatory, HelpMessage = 'Provide Cluster Name',
ValueFromPipeline, ValueFromPipelineByPropertyName)]
[ValidateNotNullOrEmpty()]
[System.String[]]
$Cluster,
[Parameter(Mandatory = $false, HelpMessage = 'Provide Credentials for Cluster',
ValueFromPipeline, ValueFromPipelineByPropertyName)]
[System.Management.Automation.PSCredential]
$Credential
)
process {
foreach ($ClusterName in $Cluster) {
$connectionParams = @{
ComputerName = $ClusterName
}
if ($PSBoundParameters.ContainsKey('Credential')) {
$connectionParams.Credential = $Credential
Write-Verbose Message "Processing with provided credentials {$($Credential.UserName)}"
}
else {
Write-Verbose Message "Processing with default credentials of user {$($env:USERNAME)}"
}
#region CODE
Write-Verbose Message "Processing Cluster {$ClusterName}"
Write-Verbose Message "Retrieving cluster nodes from cluster {$ClusterName} which are not [Down]"
$clusterNodes = Invoke-Command @connectionParams ScriptBlock {
Get-ClusterNode | Where-Object {$PSItem.State -ne "Down"}
}
foreach ($clNode in $clusterNodes) {
$wmiConnectionParams = @{
ComputerName = $clNode.Name
}
if ($PSBoundParameters.ContainsKey('Credential')) {
$wmiConnectionParams.Credential = $Credential
}
Write-Verbose Message "Retrieving memory statistics from node {$($clNode.Name)} from cluster {$Cluster}."
$selectObjectFilter = @(
@{name = 'Cluster'; e = {$ClusterName}},
@{name = 'ComputerName'; e = {$PSItem.__SERVER}},
@{name = 'FreePhysicalMemory'; e = {$PSItem.FreePhysicalMemory / 1MB -as [int]}},
@{name = 'TotalVisibleMemorySize'; e = {$PSItem.TotalVisibleMemorySize / 1MB -as [int]}},
@{name = '% Free'; e = {[System.Math]::Round(($PSItem.FreePhysicalMemory / $PSItem.TotalVisibleMemorySize * 100), 2)}}
)
Get-WmiObject win32_operatingsystem @wmiConnectionParams | Select-Object $selectObjectFilter
}
}
}
}

The output will be like this:

This comes quite handy for a quick glance:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s