PowerShell/Scripts/weather-alert.ps1

39 lines
1.1 KiB
PowerShell
Raw Normal View History

2021-04-07 11:53:57 +02:00
#!/usr/bin/pwsh
2020-12-29 15:14:21 +01:00
<#
2021-04-07 15:17:49 +02:00
.SYNTAX weather-alert.ps1
2021-03-22 20:10:18 +01:00
.DESCRIPTION checks the current weather for critical values
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
2020-12-29 15:14:21 +01:00
#>
2020-11-11 21:00:51 +01:00
$GeoLocation="" # empty means determine automatically
2020-12-29 15:14:21 +01:00
function Check { param([int]$Value, [int]$NormalMin, [int]$NormalMax, [string]$Unit)
2020-11-12 11:44:08 +01:00
if ($Value -lt $NormalMin) {
2020-11-22 10:47:29 +01:00
return "$Value $Unit ! "
2020-11-11 21:00:51 +01:00
}
2020-11-12 11:44:08 +01:00
if ($Value -gt $NormalMax) {
2020-11-22 10:47:29 +01:00
return "$Value $Unit ! "
2020-11-11 21:00:51 +01:00
}
return ""
}
try {
$Weather=(Invoke-WebRequest http://wttr.in/${GeoLocation}?format=j1 -UserAgent "curl" ).Content | ConvertFrom-Json
$Result+=Check $Weather.current_condition.windspeedKmph 0 48 "km/h"
2020-11-12 11:44:08 +01:00
$Result+=Check $Weather.current_condition.visibility 1 1000 "km visibility"
$Result+=Check $Weather.current_condition.temp_C -20 40 "°C"
$Result+=Check $Weather.current_condition.uvIndex 0 6 "UV"
2020-11-11 21:00:51 +01:00
if ($Result -eq "") {
2020-11-12 11:44:08 +01:00
echo "No weather alert"
2020-11-11 21:00:51 +01:00
} else {
echo "WEATHER ALERT: $Result"
}
exit 0
2020-12-09 10:30:55 +01:00
} catch {
2021-02-16 10:03:20 +01:00
write-error "ERROR: line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2020-12-09 10:30:55 +01:00
exit 1
}