tests/Test-HostMemory.ps1

function Test-HostMemory {
    [CmdletBinding()]
    param (
        [parameter()][string] $TestName = "Test-HostMemory",
        [parameter()][string] $TestGroup = "configuration",
        [parameter()][string] $Description = "Verify site system has at least minimum required memory",
        [parameter()][hashtable] $ScriptParams
    )
    try {
        $startTime = (Get-Date)
        [int]$MinMemory = Get-CmHealthDefaultValue -KeySet "siteservers:MinimumMemoryGB" -DataSet $CmHealthConfig
        $MinMem = $($MinMemory * 1GB)
        [System.Collections.Generic.List[PSObject]]$tempdata = @() # for detailed test output to return if needed
        $stat   = "PASS"
        $except = "WARNING"
        $msg    = "No issues found"
        $SystemInfo = Get-WmiQueryResult -ClassName "Win32_OperatingSystem" -Params $ScriptParams
        $TotalRAM = $SystemInfo.TotalVisibleMemorySize/1MB
        $FreeRAM  = $SystemInfo.FreePhysicalMemory/1MB
        $UsedRAM  = $TotalRAM - $FreeRAM
        $RAMPercentFree = ($FreeRAM / $TotalRAM) * 100
        $TotalRAM = [Math]::Round($TotalRAM, 2)
        $FreeRAM  = [Math]::Round($FreeRAM, 2)
        $UsedRAM  = [Math]::Round($UsedRAM, 2)
        $RAMPercentFree = [Math]::Round($RAMPercentFree, 2)
        Write-Verbose "total memory = $TotalRAM"
        Write-Verbose "minimum allowed memory = $MinMemory"
        if ($TotalRAM -lt $MinMem) {
            $stat = $except
            $msg  = "$($TotalRam) GB is below the minimum recommended $MinMemory GB"
            $res | Foreach-Object {$tempdata.Add("Total=$($TotalRam),Expected=$($MinMemory)")}
        } elseif ($RAMPercentFree -lt 10) {
            $stat = $except
            $msg  = "Less than 10 percent memory is available"
            $res | Foreach-Object {$tempdata.Add("PctFree=$($RAMPercentFree),Expected=10")}
        }
    }
    catch {
        $stat = 'ERROR'
        $msg  = $_.Exception.Message -join ';'
    }
    finally {
        $rt = Get-RunTime -BaseTime $startTime
        Write-Output $([pscustomobject]@{
            TestName    = $TestName
            TestGroup   = $TestGroup
            TestData    = $tempdata
            Description = $Description
            Status      = $stat
            Message     = $msg
            Credential  = $(if($ScriptParams.Credential){$($ScriptParams.Credential).UserName} else { $env:USERNAME })
        })
    }
}