1
#!/usr/bin/env -S pwsh -nop
param (
[Parameter()][switch] $g,
[Parameter()][switch] $m,
[Parameter()][switch] $k
)
$dec = [Globalization.CultureInfo]::CurrentCulture.NumberFormat.NumberDecimalDigits
$sep = [Globalization.CultureInfo]::CurrentCulture.NumberFormat.NumberDecimalSeparator
[Globalization.CultureInfo]::CurrentCulture.NumberFormat.NumberDecimalSeparator = '.'
$raw = (Get-Content /proc/meminfo) -replace '\sKB' | Out-String | ConvertFrom-StringData -Delimiter ':'
$data = @{}
foreach ($i in $raw.Keys) { $data.Add($i, ([int64] $raw.$i * 1kb)) }
$digit, $unit =
switch ($PSBoundParameters.Keys)
{
'g' { 1gb, 'GB' }
'm' { 1mb, 'MB' }
'k' { 1kb, 'KB' }
}
if (-not $digit) { $unit = 'bytes' }
else
{
[float] $data.MemTotal /= $digit
[float] $data.MemFree /= $digit
[float] $data.Cached /= $digit
[float] $data.Buffers /= $digit
}
$used = $data.MemTotal - $data.MemFree - $data.Cached - $data.Buffers
$data.Add('Used', $used)
if ($unit -match 'bytes|KB') { [Globalization.CultureInfo]::CurrentCulture.NumberFormat.NumberDecimalDigits = 0 }
[pscustomobject] @{
Total = [math]::Round($data.MemTotal,2)
Free = [math]::Round($data.MemFree,2)
Used = [math]::Round($data.Used,2)
Cached = [math]::Round($data.Cached,2)
Buffers = [math]::Round($data.Buffers,2)
Units = $unit
}
[Globalization.CultureInfo]::CurrentCulture.NumberFormat.NumberDecimalDigits = $dec
[Globalization.CultureInfo]::CurrentCulture.NumberFormat.NumberDecimalSeparator = $sep
For immediate assistance, please email our customer support: [email protected]