PowerShell/Scripts/weather-alert.ps1

39 lines
1.1 KiB
PowerShell
Raw Normal View History

2020-11-11 21:00:51 +01:00
#!/snap/bin/powershell
2020-12-29 15:14:21 +01:00
<#
.SYNTAX ./weather-alert.ps1
.DESCRIPTION checks the current weather for critical values
.LINK https://github.com/fleschutz/PowerShell
.NOTES Author: Markus Fleschutz / License: CC0
#>
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 {
2020-12-25 11:30:43 +01:00
write-error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
2020-12-09 10:30:55 +01:00
exit 1
}