PowerShell/Scripts/check-weather.ps1

60 lines
1.9 KiB
PowerShell
Raw Normal View History

2021-09-27 10:38:12 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
2021-09-24 17:19:49 +02:00
Checks the weather for critical values
2021-10-04 21:29:23 +02:00
.DESCRIPTION
2021-10-16 16:50:10 +02:00
Checks the current weather for critical values.
.PARAMETER location
Specifies the location to use (determined automatically per default)
2021-07-13 21:10:02 +02:00
.EXAMPLE
2021-09-24 17:19:49 +02:00
PS> ./check-weather
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
2021-08-29 17:50:03 +02:00
Author: Markus Fleschutz · License: CC0
2021-04-19 19:20:49 +02:00
#>
2021-09-24 17:19:49 +02:00
param([string]$location = "") # empty means determine automatically
2021-04-19 19:20:49 +02:00
function Check { param([int]$Value, [int]$NormalMin, [int]$NormalMax, [string]$Unit)
if ($Value -lt $NormalMin) {
return "$Value $Unit ! "
}
if ($Value -gt $NormalMax) {
return "$Value $Unit ! "
}
return ""
}
try {
2021-09-24 17:19:49 +02:00
$Weather = (Invoke-WebRequest http://wttr.in/${location}?format=j1 -UserAgent "curl" ).Content | ConvertFrom-Json
2021-04-19 19:20:49 +02:00
$Temp = $Weather.current_condition.temp_C
$Precip = $Weather.current_condition.precipMM
$Humidity = $Weather.current_condition.humidity
$Pressure = $Weather.current_condition.pressure
$WindSpeed = $Weather.current_condition.windspeedKmph
$WindDir = $Weather.current_condition.winddir16Point
$UV = $Weather.current_condition.uvIndex
$Visib = $Weather.current_condition.visibility
$Clouds = $Weather.current_condition.cloudcover
$Desc = $Weather.current_condition.weatherDesc.value
$Area = $Weather.nearest_area.areaName.value
$Region = $Weather.nearest_area.region.value
"Now: $($Temp)°C $($Precip)mm $($Humidity)% $($WindSpeed)km/h from $WindDir $($Pressure)hPa UV$($UV) $($Visib)km $($Clouds)% $Desc at $Area ($Region)"
$Result+=Check $Weather.current_condition.windspeedKmph 0 48 "km/h"
$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"
if ($Result -eq "") {
"Calm weather"
} else {
"WEATHER ALERT: $Result"
}
2021-09-27 10:09:45 +02:00
exit 0 # success
2021-04-19 19:20:49 +02:00
} catch {
2021-09-16 20:19:10 +02:00
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
2021-04-19 19:20:49 +02:00
exit 1
}