Platform-Info.psm1
<#
.Synopsis Cross-platform access to actual hardware on Windows, Linux and MacOS. .Description Cross-platform access to actual hardware on Windows, Linux and MacOS. Both powershell and pwsh are supported. .Example # Show OS Platform, Linux|Windows|MacOS|FreeBSD Get-Os-Platform .Example # Display CPU Name Write-Host "CPU: $(Get-Cpu-Name)" .Example # Display CPU Name echo "Memory $((Get-Memory-Info).Description)" echo "Total RAM: $((Get-Memory-Info).Total) MB" echo "Free RAM: $((Get-Memory-Info).Free) MB" #> # Include Detected: [ ..\Includes\Measure-Action.ps1 ] # File: [C:\Cloud\vg\PUTTY\PS1-Repo\Includes\Measure-Action.ps1] function Measure-Action { Param( [string] $Title, [ScriptBlock] $Action ) $startAt = [System.Diagnostics.Stopwatch]::StartNew() try { Invoke-Command -ScriptBlock $action; $err=$null; } catch { $err=$_.Exception; } $msec = $startAt.ElapsedMilliseconds; $ea = $ErrorActionPreference $ErrorActionPreference = "SilentlyContinue" if (-not $err) { Write-Host "Success. " -ForeGroundColor Green -NoNewLine; Write-Host "'$title' took $($msec.ToString("n0")) ms" } else { # Write-Host $err.GetType() Write-Host "Fail. $($err.Message)" -ForeGroundColor Red -NoNewLine; Write-Host " '$title' took $($msec.ToString("n0")) ms" } $ErrorActionPreference=$ea } # Include Detected: [ ..\Includes\Has-Cmd.ps1 ] # File: [C:\Cloud\vg\PUTTY\PS1-Repo\Includes\Has-Cmd.ps1] function Has-Cmd { param([string] $arg) if ("$arg" -eq "") { return $false; } [bool] (Get-Command "$arg" -ErrorAction SilentlyContinue) } # Include Detected: [ ..\Includes\Out-String-And-TrimEnd.ps1 ] # File: [C:\Cloud\vg\PUTTY\PS1-Repo\Includes\Out-String-And-TrimEnd.ps1] Function Out-String-And-TrimEnd { Param ([int] $Skip=0, [int] $Take=2000000000) Begin { $n=0; $list = New-Object System.Collections.Generic.List[System.Object]} Process { $n++; if (-not ($n -le $Skip -or $n -gt ($Skip+$Take))) { $list.Add("$_"); } } End { return [string]::join([System.Environment]::NewLine, $list.ToArray()).TrimEnd(@([char]13,[char]10)) } } # Include Detected: [ ..\Includes\Get-Nix-Uname-Value.ps1 ] # File: [C:\Cloud\vg\PUTTY\PS1-Repo\Includes\Get-Nix-Uname-Value.ps1] # Linux/Darwin/FreeBSD, Error on Windows function Get-Nix-Uname-Value { param([string] $arg) return (& uname "$arg" | Out-String-And-TrimEnd) } # Include Detected: [ ..\Includes\Get-Os-Platform.ps1 ] # File: [C:\Cloud\vg\PUTTY\PS1-Repo\Includes\Get-Os-Platform.ps1] # 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 "MacOS"; } if ($nixUnameSystem -eq "FreeBSD") { return "FreeBSD"; } return "Unknown" <# .OUTPUTS One of the following values: "Linux", "Windows", "MacOS", "FreeBSD", "Unknown" #> } # Include Detected: [ ..\Includes\Get-Cpu-Name.ps1 ] # File: [C:\Cloud\vg\PUTTY\PS1-Repo\Includes\Get-Cpu-Name.ps1] function Get-Cpu-Name-Implementation { $platform = Get-Os-Platform if ($platform -eq "Windows") { if (Has-Cmd "Get-CIMInstance") { $proc=Get-CIMInstance Win32_Processor; } elseif (Has-Cmd "Get-WmiObject") { $proc=Get-WmiObject Win32_Processor; } return "$($proc.Name)" } if ($platform -eq "MacOS") { return (& sysctl "-n" "machdep.cpu.brand_string" | Out-String-And-TrimEnd) } 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:]]*//'" $ret = "$(& bash -c "$shell" | Out-String-And-TrimEnd)" if (-not $ret) { $parts = @( (Get-Nix-Uname-Value "-m"), "$(& bash -c "getconf LONG_BIT" | Out-String-And-TrimEnd) bit" ); $ret = ($parts | where { "$_" }) -join ", " } return $ret } $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; } # Include Detected: [ ..\Includes\Get-Memory-Info.ps1 ] # File: [C:\Cloud\vg\PUTTY\PS1-Repo\Includes\Get-Memory-Info.ps1] function Get-Memory-Info { [OutputType([object])] param() $platform = Get-Os-Platform if ($platform -eq "Windows") { if (Has-Cmd "Get-CIMInstance") { $os=Get-CIMInstance Win32_OperatingSystem; } elseif (Has-Cmd "Get-WmiObject") { $os=Get-WmiObject Win32_OperatingSystem; } $mem=($os | Where { $_.FreePhysicalMemory } | Select FreePhysicalMemory,TotalVisibleMemorySize -First 1); $total=[int] ($mem.TotalVisibleMemorySize / 1024); $free=[int] ($mem.FreePhysicalMemory / 1024); } if ($platform -eq "MacOS") { $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-And-TrimEnd) $inactive=[long] (& vm_stat | grep "Pages inactive" | awk -v OFMT="%.0f" '{print (4 * $NF / 1024)}' | Out-String-And-TrimEnd) $free = [int]$free + [int]$inactive; # Write-Host "Mem 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-And-TrimEnd) $used =[int] (& free -m | awk 'NR==2 {print $3 + $5}' | Out-String-And-TrimEnd) $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 #> } Export-ModuleMember -Function Get-Cpu-Name Export-ModuleMember -Function Get-Os-Platform Export-ModuleMember -Function Get-Memory-Info Export-ModuleMember -Function Measure-Action |