PowerShell/Scripts/list-weather.ps1

54 lines
1.7 KiB
PowerShell
Raw Normal View History

2021-04-21 19:53:52 +02:00
<#
2021-07-13 21:10:02 +02:00
.SYNOPSIS
list-weather.ps1 [<location>]
.DESCRIPTION
2021-08-29 17:50:03 +02:00
Lists the hourly weather.
2021-07-13 21:10:02 +02:00
.EXAMPLE
PS> .\list-weather.ps1
2021-08-29 17:50:03 +02:00
.NOTES
Author: Markus Fleschutz · License: CC0
2021-07-13 21:10:02 +02:00
.LINK
https://github.com/fleschutz/PowerShell
2021-04-19 11:12:21 +02:00
#>
2021-07-15 15:51:22 +02:00
param([string]$Location = "") # empty means determine automatically
2021-04-19 11:12:21 +02:00
try {
2021-05-13 10:37:19 +02:00
$Weather = (invoke-webRequest -uri http://wttr.in/${Location}?format=j1 -userAgent "curl" -useBasicParsing).Content | ConvertFrom-Json
2021-04-19 11:12:21 +02:00
$Area = $Weather.nearest_area.areaName.value
$Region = $Weather.nearest_area.region.value
2021-04-20 16:38:04 +02:00
$Country = $Weather.nearest_area.country.value
2021-04-19 11:12:21 +02:00
2021-04-20 16:38:04 +02:00
[int]$Day = 0
2021-04-19 11:12:21 +02:00
foreach ($Hourly in $Weather.weather.hourly) {
2021-04-20 16:38:04 +02:00
$Hour = $Hourly.time / 100
2021-04-19 11:12:21 +02:00
$Temp = $Hourly.tempC
$Precip = $Hourly.precipMM
$Humidity = $Hourly.humidity
$Pressure = $Hourly.pressure
$WindSpeed = $Hourly.windspeedKmph
$WindDir = $Hourly.winddir16Point
$UV = $Hourly.uvIndex
$Clouds = $Hourly.cloudcover
$Desc = $Hourly.weatherDesc.value
2021-04-20 16:38:04 +02:00
if ($Hour -eq 0) {
if ($Day -eq 0) {
2021-05-07 14:07:33 +02:00
write-host -foregroundColor green "🕗 🌡°C ☂️ 💧 💨 from ☀UV ☁️ TODAY at $Area ($Region, $Country)"
2021-04-20 16:38:04 +02:00
} elseif ($Day -eq 1) {
2021-05-07 14:07:33 +02:00
write-host -foregroundColor green " TOMORROW"
2021-04-20 16:38:04 +02:00
} else {
2021-05-07 14:07:33 +02:00
write-host -foregroundColor green " DAY AFTER TOMORROW"
2021-04-20 16:38:04 +02:00
}
$Day++
}
2021-05-07 14:07:33 +02:00
"$(($Hour.toString()).PadLeft(2))°° $(($Temp.toString()).PadLeft(2))° $($Precip)mm $(($Humidity.toString()).PadLeft(3))% $(($WindSpeed.toString()).PadLeft(2))km/h $WindDir`t$($UV) $(($Clouds.toString()).PadLeft(3))% $Desc"
2021-04-19 11:12:21 +02:00
$Hour++
}
exit 0
} catch {
2021-09-16 20:19:10 +02:00
"⚠️ Error: $($Error[0]) ($($MyInvocation.MyCommand.Name):$($_.InvocationInfo.ScriptLineNumber))"
2021-04-19 11:12:21 +02:00
exit 1
}