PowerShell/Scripts/weather-alert.ps1

40 lines
1.1 KiB
PowerShell
Raw Normal View History

2020-11-11 21:00:51 +01:00
#!/snap/bin/powershell
# Syntax: ./weather-alert.ps1
# Description: checks the current weather for critical values
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell
# License: CC0
$GeoLocation="" # empty means determine automatically
function Check {
2020-11-12 11:44:08 +01:00
param ([int]$Value, [int]$NormalMin, [int]$NormalMax, [string]$Unit)
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 {
Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}