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)"
#>


# 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 {
  $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"
}

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 {
  if (-not $Global:_Cpu_Name) { $Global:_Cpu_Name = "$(Get-Cpu-Name-Implementation)"; }
  return $Global:_Cpu_Name;
}

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)'"

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