Platform-Info.psm1

<#
 .Synopsis
  Cross-platform access to actual hardware on Windows, Linux and Mac.
 
 .Description
  Cross-platform access to actual hardware on Windows, Linux and Mac. Both powershell and pwsh are supported.
 
 .Example
   # Show OS Platform, Linux|Windows|Mac|FreeBSD
   Get-Os-Platform
 
 .Example
   # Display CPU Name
   Write-Host "CPU: $(Get-Cpu-Name)"
 
 .Example
   # Display CPU Name
   echo "Memory $((Get-Memory-Info).Description)"
 
 
#>


# Linux/Darwin/FreeBSD, Error on Windows
function Get-Nix-Uname-Value {
  param([string] $arg)
  return (& uname "$arg" | Out-String).TrimEnd(@([char]13,[char]10))
}

# Returns Linux/Windows/Mac/FreeBSD
function Get-Os-Platform {
  [OutputType([string])] param()

  $platform = [System.Environment]::OSVersion.Platform;
  if ($platform -like "win*") { return "Windows"; }

  $nixUnameSystem = Get-Nix-Uname-Value "-s"
  if ($nixUnameSystem -eq "Linux") { return "Linux"; }
  if ($nixUnameSystem -eq "Darwin") { return "Mac"; }
  if ($nixUnameSystem -eq "FreeBSD") { return "FreeBSD"; }

  return "Unknown"

  <#
     .OUTPUTS
     One of the following values: "Linux", "Windows", "Mac", "FreeBSD", "Unknown"
  #>

}

function Get-Cpu-Name-Implementation {
  $platform = Get-Os-Platform
  if ($platform -eq "Windows") {
    return "$((Get-WmiObject Win32_Processor).Name)"
  }

  if ($platform -eq "Mac") {
    return (& sysctl "-n" "machdep.cpu.brand_string" | Out-String).TrimEnd(@([char]13,[char]10))
  }

  if ($platform -eq "Linux") {
    # TODO: Replace grep, awk, sed by NET
    $shell="cat /proc/cpuinfo | grep -E '^(model name|Hardware)' | awk -F':' 'NR==1 {print `$2}' | sed -e 's/^[[:space:]]*//'"
    return (& bash -c "$shell" | Out-String).TrimEnd(@([char]13,[char]10))
  }

  $ret = $null;
  try { $ret = Get-Nix-Uname-Value "-m"; } catch {}
  if ($ret) { return "$ret"; }

  return "Unknown"
}

function Get-Cpu-Name {
  [OutputType([string])] param()

  if (-not $Global:_Cpu_Name) { $Global:_Cpu_Name = "$(Get-Cpu-Name-Implementation)"; }
  return $Global:_Cpu_Name;
}

function Get-Memory-Info {
  [OutputType([object])] param()

  $platform = Get-Os-Platform
  if ($platform -eq "Windows") {
    $mem=(Get-CIMInstance Win32_OperatingSystem | Select FreePhysicalMemory,TotalVisibleMemorySize)[0];
    $total=[int] ($mem.TotalVisibleMemorySize / 1024);
    $free=[int] ($mem.FreePhysicalMemory / 1024);
  }

  if ($platform -eq "Mac") {
    $total=[long] (& sysctl -n hw.memsize | Out-String).TrimEnd(@([char]13,[char]10))
    $total=[int] ($total/1024/1024)
    $free=[long] (& vm_stat | grep "Pages free" | awk -v OFMT="%.0f" '{print (4 * $NF / 1024)}' | Out-String).TrimEnd(@([char]13,[char]10))
    $inactive=[long] (& vm_stat | grep "Pages inactive" | awk -v OFMT="%.0f" '{print (4 * $NF / 1024)}' | Out-String).TrimEnd(@([char]13,[char]10))
    $free = [int]$free + [int]$inactive;
    # Write-Host "Mac Total: $total, Free: $free"
  }

  if ($platform -eq "Linux") {
    # total: $2, $used: $3, shared: $5. free = total-(used+shared)
    $total=[int] (& free -m | awk 'NR==2 {print $2}' | Out-String).TrimEnd(@([char]13,[char]10))
    $used =[int] (& free -m | awk 'NR==2 {print $3 + $5}' | Out-String).TrimEnd(@([char]13,[char]10))
    $free=$total-$used
  }

  if ($total) {
    $info="Total RAM: $($total.ToString("n0")) MB. Free: $($free.ToString("n0")) MB ($([Math]::Round($free * 100 / $total, 1))%)";
    return @{
        Total=$total;
        Free=$free;
        Description=$info;
    }
  }

  <#
     .OUTPUTS
     Object with 3 properties: [int] Total, [int] Free, [string] Description
  #>


}

function test() {
  echo "Memory $((Get-Memory-Info).Description)"
  echo "OS Platform: '$(Get-Os-Platform)'"
  if ("$(Get-Os-Platform)" -ne "Windows") { echo "UName System: '$(Get-Nix-Uname-Value "-s")'" }
  echo "CPU: '$(Get-Cpu-Name)'"
}; # test

Export-ModuleMember -Function Get-Cpu-Name
Export-ModuleMember -Function Get-Os-Platform
Export-ModuleMember -Function Get-Memory-Info