|
#requires -version 7.5 |
|
#requires -Modules pwshSpectreConsole,CimCmdlets |
|
|
|
<# |
|
Show-SystemStatus.ps1 |
|
Last Updated: Thursday, February 12, 2026 |
|
|
|
An earlier version of this function was first published at: |
|
https://buttondown.com/behind-the-powershell-pipeline/archive/more-formatting-fun-with-pwshspectreconsole/ |
|
|
|
#> |
|
|
|
Function Show-SpectreSystemStatus { |
|
[cmdletbinding()] |
|
[OutputType('Spectre.Console.Panel')] |
|
[Alias("sss","SysStat")] |
|
Param( |
|
[Parameter(Position=0)] |
|
[ValidateNotNullOrEmpty()] |
|
[Alias("CN")] |
|
[string]$Computername, |
|
|
|
[Parameter(Position=1)] |
|
[ValidateNotNullOrEmpty()] |
|
[Alias("RunAs")] |
|
[PSCredential]$Credential, |
|
|
|
[Parameter(HelpMessage= "Specify a logo image")] |
|
[ValidateScript({Test-Path $_})] |
|
[ValidateNotNullOrEmpty()] |
|
[string]$Logo = "$PSScriptRoot\MsPowerShell.png" |
|
) |
|
|
|
#define a version number for this stand-alone function |
|
$ver = "1.5.1" |
|
#define style variables up front for easy editing |
|
#styles are stored in a default external data file |
|
if (Test-Path $PSScriptRoot\SystemStatusStyle.psd1) { |
|
$styles = Import-PowerShellDataFile $PSScriptRoot\SystemStatusStyle.psd1 |
|
#define style variables from the hashtable |
|
$styles.GetEnumerator().Foreach({Set-Variable -name $_.key -Value $_.Value -scope Script }) |
|
} |
|
else { |
|
#use defaults |
|
$statusPanel = "default" #Gold1 |
|
$linkStyle = "default italic" # "chartreuse1 italic" |
|
$runStyle = "default" #"yellow2" |
|
$processStyle = "default" #"yellow3" |
|
$physicalStyle = "default" #"orange1" |
|
$osStyle = "default bold" #"Thistle1 bold" |
|
$usedStyle = "default" #"OrangeRed1" |
|
$freeStyle = "default" #"GreenYellow" |
|
} |
|
|
|
If ($PSBoundParameters.ContainsKey("ComputerName")) { |
|
#remove Logo if passed as a bound parameter and using remoting |
|
($PSBoundParameters.ContainsKey("Logo")) ? $([void]$PSBoundParameters.Remove("Logo")) : $null |
|
#create a CIM session if a computername is passed |
|
Try { |
|
$cimSess = New-CimSession @PSBoundParameters -ErrorAction Stop |
|
} |
|
Catch { |
|
Throw $_ |
|
} |
|
} |
|
else { |
|
#use the local host |
|
$Computername = $env:COMPUTERNAME |
|
$cimSess = $Computername |
|
} |
|
|
|
#get the data |
|
$cDrive = Get-CimInstance Win32_LogicalDisk -Property Size,Freespace -filter "DeviceID='C:'" -CimSession $cimSess |
|
$os = Get-CimInstance Win32_OperatingSystem -property FreePhysicalMemory,TotalVisibleMemorySize,LastBootUpTime,Caption,OSArchitecture -CimSession $cimSess |
|
$svc = Get-CimInstance Win32_Service -filter "State='Running'" -Property Name -CimSession $cimSess |
|
$cs = Get-CimInstance Win32_ComputerSystem -Property SystemFamily,Manufacturer,Model -CimSession $cimSess |
|
$proc = Get-CimInstance Win32_Process -Property ProcessID,Name,WorkingSetSize,CreationDate -CimSession $cimSess |
|
|
|
#assuming this is being run in Windows Terminal or a host that supports links |
|
$link = Write-SpectreHost "[$linkStyle link=https://jdhitsolutions.com/yourls/newsletter]Ctrl+Click here to expand your PowerShell skills[/]" -PassThru | |
|
Format-SpectrePadded -Top 1 -left 1 -Bottom 0 -Right 0 |
|
|
|
$padParams = @{ |
|
Left = 20 |
|
Top = 1 |
|
Bottom = 0 |
|
Right = 0 |
|
} |
|
<# |
|
logo path needs to be a file system path |
|
There is a bug in the current version of pwshSpectreConsole that fails if the image path |
|
is a PSDrive, so I'm converting the path to its FileSystem equivalent |
|
#> |
|
$logoImage = Get-SpectreImage (Convert-Path $Logo) -MaxWidth 10 | Format-SpectrePadded @padParams |
|
|
|
if ($cimSess.ComputerName) { |
|
Remove-CimSession $cimSess |
|
} |
|
|
|
#process the data and create chart elements |
|
$driveData = @( |
|
(New-SpectreChartItem -Label Used -Value $([math]::Round(($cDrive.size - $cDrive.FreeSpace)/1gb,2)) -Color $usedStyle), |
|
(New-SpectreChartItem -Label Free -Value ([math]::Round(($cDrive.FreeSpace)/1gb,2)) -Color $freeStyle) |
|
) |
|
$cLabel = "Disk Usage C: (GB)" | Format-SpectrePadded -Padding 1 |
|
|
|
$memData = @( |
|
(New-SpectreChartItem -Label Used -Value $([math]::Round(($os.TotalVisibleMemorySize - $os.FreePhysicalMemory)/1mb,2)) -Color $usedStyle), |
|
(New-SpectreChartItem -Label Free -Value ([math]::Round(($os.FreePhysicalMemory)/1mb,2)) -Color $freeStyle) |
|
) |
|
$memLabel = "Memory Usage (GB)" | Format-SpectrePadded -Padding 1 |
|
|
|
$g1 = $cLabel,($driveData | Format-SpectreBreakdownChart -Width 45) | Format-SpectreGrid |
|
$g2 = $memLabel,($memData | Format-SpectreBreakdownChart -Width 45) | Format-SpectreGrid |
|
|
|
$physPanel = $g1,$g2 | Format-SpectreRows | Format-SpectreColumns | |
|
Format-SpectrePanel -Color $physicalStyle -Title "Physical Information :computer_disk:" |
|
|
|
$uptime = "{0:dd\.hh\:mm\:ss}" -f (New-TimeSpan -start $os.LastBootUpTime -end (Get-Date)) |
|
|
|
$hardware = $cs.SystemFamily ? $cs.SystemFamily : "$($cs.Manufacturer): $($cs.Model)" |
|
$cap = Write-SpectreHost "[$osStyle]$($os.Caption)`n$($hardware)`n$($os.OSArchitecture)[/]" -PassThru | |
|
Format-SpectrePadded -padding 1 |
|
|
|
$physInfo = $physPanel | Format-SpectreRows |
|
|
|
$procTable = $proc | Sort WorkingSetSize -Descending | |
|
Select-Object -first 5 -Property @{Name = "ID";Expression = {$_.ProcessID}}, |
|
Name, |
|
@{Name="Runtime";Expression = { "{0:dd\.hh\:mm\:ss}" -f (New-TimeSpan -start $_.CreationDate -end (Get-Date))}}, |
|
@{Name="WS(M)";Expression = {[int32]($_.WorkingSetSize/1mb) }} | |
|
Format-SpectreTable -Title ":gear: Top 5 Processes" -Color $processStyle -HeaderColor $processStyle |
|
|
|
$runGrid = Format-SpectreGrid -Data @( |
|
@("Running Processes:",$proc.Count), |
|
@("Running Services :",$svc.Count), |
|
@("System Uptime :",$upTime) |
|
) | Format-SpectrePadded -Padding .85 | |
|
Format-SpectrePanel -Color $runStyle -title " Run Information :wrench: " |
|
|
|
$runInfo = @($cap,$runGrid,$procTable) | ForEach-Object {$_ | New-SpectreGridRow} | |
|
Format-SpectreGrid | Format-SpectrePanel -Border none |
|
|
|
$pi = $physInfo,$link,$logoImage | ForEach-Object {$_ | New-SpectreGridRow} | |
|
Format-SpectreGrid |
|
|
|
#create the output panel |
|
$runInfo,$pi | Format-SpectreColumns -Padding 1 | |
|
Format-SpectrePadded -Padding .5 | |
|
Format-SpectrePanel -Title " :information: [italic $statusPanel]$($Computername.ToUpper()): System Status v.$ver[/]" -Color $statusPanel |
|
|
|
} #end function |