DEV Community

redhcp
redhcp

Posted on

2 1

Obtain Memory VM - Powershell

This script returns the memory that the VM contains.


[CmdletBinding()]
    param(
        [Parameter(ValueFromPipelineByPropertyName=$true)][alias("DNSHostName","Name")]$ComputerName = '.',
        [PSCredential]$Credential
    )

    BEGIN {}

    PROCESS  {
        Foreach ($Comp in $ComputerName) {
            $param = @{
                'ComputerName' = $Comp
                'ErrorVariable' = 'WmiRequestError'
            }
            if($Credential -and ($Comp -notin @($env:COMPUTERNAME,'.'))){$param.Credential = $Credential}

            try{
                $PerfOS_Memory = Get-WmiObject -Class Win32_PerfRawData_PerfOS_Memory @param
                $PhysicalMemory = Get-WmiObject -Class Win32_PhysicalMemory @param
                $TotalPhysicalMemory = ($PhysicalMemory | Measure-Object -Sum -Property Capacity).Sum
            } Catch {$WmiRequestError; break}

            if($PerfOS_Memory -and !$WmiRequestError){

                [pscustomobject][ordered]@{
                    'ComputerName' = $PerfOS_Memory.PSComputerName
                    'AvailableGB' = [System.Math]::Round(($PerfOS_Memory.AvailableBytes/1gb),2)
                    'inUseGB' = [System.Math]::Round(($TotalPhysicalMemory/1gb - $PerfOS_Memory.AvailableBytes/1gb),2)
                    'CacheGB' = [System.Math]::Round(($PerfOS_Memory.CacheBytes/1gb),2)
                    'CommittedGB' = [System.Math]::Round(($PerfOS_Memory.CommittedBytes/1gb),2)
                    'CommitLimitGB' = [System.Math]::Round(($PerfOS_Memory.CommitLimit/1gb),2)
                    'PoolPagedMB' = [System.Math]::Round(($PerfOS_Memory.PoolPagedBytes/1mb),2)
                    'PoolNonpagedMB' = [System.Math]::Round(($PerfOS_Memory.PoolNonpagedBytes/1mb),2)
                    'TotalPhysicalMemory' = [System.Math]::Round(($TotalPhysicalMemory/1gb),2)
                    'ModuleSize' = ($PhysicalMemory | Group-Object -Property Capacity | % {[string]$($_.Count.ToString() + ' x ' + ($_.Name / 1GB).ToString() + 'GB')}) -join ', '
                }
            }
            $PerfOS_Memory = $Null
            $PhysicalMemory = $Null
            $TotalPhysicalMemory = $Null
            $WmiRequestError = $Null

        }

    }

    END {}
Enter fullscreen mode Exit fullscreen mode

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay