PowerShell/scripts/write-value.ps1

79 lines
2.2 KiB
PowerShell
Raw Normal View History

2024-12-01 20:46:27 +01:00
<#
.SYNOPSIS
Writes a value with value range
.DESCRIPTION
2024-12-02 20:13:11 +01:00
This PowerShell script writes the given value with value range to the console.
2024-12-01 20:46:27 +01:00
.PARAMETER value
Specifies the value
.EXAMPLE
PS> ./write-value.ps1 0.5 Mach 0 10
[00.5 Mach10]
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-12-03 07:16:25 +01:00
param([float]$value = 0.5, [string]$unit = "Mach", [float]$redMin = 0.0, [float]$redMax = 10.0)
2024-12-01 20:46:27 +01:00
2024-12-02 20:13:11 +01:00
function WriteLine([float]$count) {
while ($count -ge 1.0) {
2024-12-01 20:46:27 +01:00
Write-Host "" -noNewline
2024-12-02 20:13:11 +01:00
$count -= 1.0
2024-12-01 20:46:27 +01:00
}
2024-12-02 20:13:11 +01:00
return $count
2024-12-01 20:46:27 +01:00
}
2024-12-03 07:16:25 +01:00
function WriteBlock([float]$value, [string]$unit, [float]$redMin, [float]$yellowMin, [float]$yellowMax, [float]$redMax) {
$text = "[$redMin $($value)$unit $redMax]"
2024-12-02 20:13:11 +01:00
[float]$left = 20.0 - $text.Length
2024-12-03 07:16:25 +01:00
if ($value -gt $redMax) {
Write-Host "[$redMin" -noNewline
2024-12-02 20:13:11 +01:00
$rest = WriteLine $left
2024-12-03 07:16:25 +01:00
Write-Host "$redMax]" -noNewline
2024-12-02 20:13:11 +01:00
Write-Host " $($value)$unit " -noNewline -foregroundColor red
2024-12-03 07:16:25 +01:00
} elseif ($value -lt $redMin) {
2024-12-02 20:13:11 +01:00
Write-Host "$($value)$unit" -noNewline -foregroundColor red
2024-12-03 07:16:25 +01:00
Write-Host " [$redMin" -noNewline
2024-12-02 20:13:11 +01:00
$rest = WriteLine $left
2024-12-03 07:16:25 +01:00
Write-Host "$redMax] " -noNewline
} elseif (($value -le $yellowMin) -or ($value -ge $yellowMax)) {
[float]$percent = (($value - $redMin) * $left) / ($redMax - $redMin)
Write-Host "[$redMin" -noNewline
$rest = WriteLine $percent
Write-Host "$($value)$unit" -noNewline -foregroundColor yellow
$rest = WriteLine ($left - $percent + $rest + 1.0)
Write-Host "$redMax] " -noNewline
2024-12-01 20:46:27 +01:00
} else {
2024-12-03 07:16:25 +01:00
[float]$percent = (($value - $redMin) * $left) / ($redMax - $redMin)
Write-Host "[$redMin" -noNewline
2024-12-02 20:13:11 +01:00
$rest = WriteLine $percent
Write-Host "$($value)$unit" -noNewline -foregroundColor green
$rest = WriteLine ($left - $percent + $rest + 1.0)
2024-12-03 07:16:25 +01:00
Write-Host "$redMax] " -noNewline
2024-12-01 20:46:27 +01:00
}
}
2024-12-03 07:16:25 +01:00
WriteBlock -3.5 "°C" 0 10 90 100
2024-12-02 20:13:11 +01:00
Write-Host "CPU too cold"
2024-12-01 20:46:27 +01:00
2024-12-03 07:16:25 +01:00
WriteBlock 5 "°C" 0 10 90 100
Write-Host "CPU quite cold"
WriteBlock 15 "°C" 0 10 90 100
2024-12-02 20:13:11 +01:00
Write-Host "OK"
2024-12-01 20:46:27 +01:00
2024-12-03 07:16:25 +01:00
WriteBlock 50 "°C" 0 10 90 100
2024-12-02 20:13:11 +01:00
Write-Host "OK"
2024-12-01 20:46:27 +01:00
2024-12-03 07:16:25 +01:00
WriteBlock 70 "°C" 0 10 90 100
2024-12-02 20:13:11 +01:00
Write-Host "OK"
2024-12-01 20:46:27 +01:00
2024-12-03 07:16:25 +01:00
WriteBlock 95 "°C" 0 10 90 100
Write-Host "CPU quite hot "
WriteBlock 110 "°C" 0 10 90 100
2024-12-02 20:13:11 +01:00
Write-Host "CPU too hot "
2024-12-01 20:46:27 +01:00
exit 0 # success