Skip to content

Instantly share code, notes, and snippets.

@mczerniawski
Created October 2, 2018 20:17
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mczerniawski/4b1cc1319d9a38eae3c352c8536f78a6 to your computer and use it in GitHub Desktop.
Save mczerniawski/4b1cc1319d9a38eae3c352c8536f78a6 to your computer and use it in GitHub Desktop.
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
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment