I came across some fun stuff on the internet, so I wrote a quick wrapper to make a Powershell weather forecasting Function for this blog post.
The idea is to get the weather report in Powershell console using this cURL implementation converted into Powershell function below
PowerShell Weather Forecasting Function:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Get Weather of a City | |
.DESCRIPTION | |
Fetches Weather report of a City from website – http://wttr.in/ courtsey Igor Chubin [Twitter- @igor_chubin] | |
.PARAMETER City | |
Name of City | |
.PARAMETER Tomorrow | |
Switch to include tomorrow's Weather report | |
.PARAMETER DayAfterTomorrow | |
Switch to include Day after tomorrow's Weather report | |
.EXAMPLE | |
Get-Weather "Los Angles" -Tomorrow -DayAfterTomorrow | |
.EXAMPLE | |
'london', 'delhi', 'beijing' | Get-Weather | |
.NOTES | |
Blog – RidiCurious.com | |
#> | |
Function Get-Weather { | |
[Alias('Wttr')] | |
[Cmdletbinding()] | |
Param( | |
[Parameter( | |
Mandatory = $true, | |
HelpMessage = 'Enter name of the City to get weather report', | |
ValueFromPipeline = $true, | |
Position = 0 | |
)] | |
[ValidateNotNullOrEmpty()] | |
[string[]] $City, | |
[switch] $Tomorrow, | |
[switch] $DayAfterTomorrow | |
) | |
Process | |
{ | |
Foreach($Item in $City){ | |
try { | |
# Check Operating System Version | |
If((Get-WmiObject win32_operatingsystem).caption -like "*Windows 10*") { | |
$Weather = $(Invoke-WebRequest "http://wttr.in/$City" -UserAgent curl).content -split "`n" | |
} | |
else { | |
$Weather = (Invoke-WebRequest "http://wttr.in/$City").ParsedHtml.body.outerText -split "`n" | |
} | |
If($Weather) | |
{ | |
$Weather[0..16] | |
If($Tomorrow){ $Weather[17..26] } | |
If($DayAfterTomorrow){ $Weather[27..36] } | |
} | |
} | |
catch { | |
$_.exception.Message | |
} | |
} | |
} | |
} |
Just pass a name of the City as a parameter to the Function to get the weather report of the day.
You can pass values through the pipeline as well.
Use ‘-Tomorrow‘ and ‘-DayAfterTomorrow‘ to check the weather of later than today.
To add this script to your Powershell profile run the below piece of code and you are good to go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add-Content $profile -Value (iwr https://goo.gl/UexVKY); . $Profile |
If you like this article, you may want to take a look at my Web and Native API Blog series for similar and other interesting implementations in PowerShell.
Don’t forget to show off your Powershell-Weather skills to your colleagues, Cheers! 😉
Prateek Singh
Related posts
10 Comments
Leave a ReplyCancel reply
Categories
Author of Books
Awards
Open Sourced Projects
Author at
Blog Roll
Mike F RobbinsDamien Van Robaeys
Stéphane van Gulick
Kevin Marquette
Adam Bertram
Stephanos Constantinou
Francois-Xavier Cat
Ravikanth Chaganti
Roman Levchenko
Blog Stats
- 1,131,822 People Reached
[…] on August 23, 2017 submitted by /u/Prateeksingh1590 [link] [comments] Leave a […]
I have no formatting and it looks messy
PS C:> Get-Weather Paris
Weather report: Paris, France
←[38;5;240;1m .-. ←[0m Light Rain With Thunderstorm
←[38;5;240;1m ( ). ←[0m ←[38;5;154m17←[0m °C←[0m
←[38;5;240;1m (___(__) ←[0m ←[1m?←[0m ←[38;5;154m9←[0m km/h←[0m
←[38;5;21;1m ”←[38;5;228;5m?←[38;5;21;25m”←[38;5;228;5m?←[38;5;21;25m” ←[0m 10 km←[0m
←[38;5;21;1m ””←[38;5;228;5m?←[38;5;21;25m”’ ←[0m 0.2 mm←[0m
┌─────────────┐
┌──────────────────────────────┬───────────────────────┤ Fri 25 Aug ├───────────────────────┬──────────────────────────
────┐
│ Morning │ Noon └──────┬──────┘ Evening │ Night
│
├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────
────┤
│ ←[38;5;226m _`/””←[38;5;250m.-. ←[0m Patchy rain po│ ←[38;5;226m /←[0m Partly cloudy │ ←[38;5;226m _`/”
“←[38;5;250m.-. ←[0m Patchy rain po│ ←[38;5;226m _`/””←[38;5;250m.-. ←[0m Light rain sho│
│ ←[38;5;226m ,_←[38;5;250m( ). ←[0m ←[38;5;226m24←[0m °C←[0m │ ←[38;5;226m _ /””←[38;5;250m.-. ←[0m ←[
38;5;220m27←[0m-←[38;5;214m28←[0m °C←[0m │ ←[38;5;226m ,_←[38;5;250m( ). ←[0m ←[38;5;220m27←[0m-←[38;5;214m28
←[0m °C←[0m │ ←[38;5;226m ,_←[38;5;250m( ). ←[0m ←[38;5;226m24←[0m-←[38;5;220m26←[0m °C←[0m │
│ ←[38;5;226m /←[38;5;250m(___(__) ←[0m ←[1m?←[0m ←[38;5;190m10←[0m-←[38;5;190m12←[0m km/h←[0m │ ←[38;5;226m _←[3
8;5;250m( ). ←[0m ←[1m?←[0m ←[38;5;190m10←[0m-←[38;5;190m11←[0m km/h←[0m │ ←[38;5;226m /←[38;5;250m(___(__) ←[0m
←[1m?←[0m ←[38;5;118m6←[0m-←[38;5;154m8←[0m km/h←[0m │ ←[38;5;226m /←[38;5;250m(___(__) ←[0m ←[1m↓←[0m ←[38;5;154m
9←[0m-←[38;5;190m11←[0m km/h←[0m │
│ ←[38;5;111m ‘ ‘ ‘ ‘ ←[0m 16 km←[0m │ ←[38;5;226m /←[38;5;250m(___(__) ←[0m 16 km←[0m │ ←[38;5;
111m ‘ ‘ ‘ ‘ ←[0m 19 km←[0m │ ←[38;5;111m ‘ ‘ ‘ ‘ ←[0m 19 km←[0m │
│ ←[38;5;111m ‘ ‘ ‘ ‘ ←[0m 0.1 mm | 42%←[0m │ 0.1 mm | 26%←[0m │ ←[38;5;111m ‘ ‘ ‘ ‘ ←[0m 0.6
mm | 42%←[0m │ ←[38;5;111m ‘ ‘ ‘ ‘ ←[0m 0.3 mm | 35%←[0m │
└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────
────┘
Are you using windows 8 or 7 ?
Windows 7.
With your latest change the formatting now is OK. (but without colors)
Yeah, seems like a limitation on console on Lower OS versions
Another reason to upgrade finally to 10 🙂
Yes, There was major console upgrade in Win10, go for it.
Waiting on set-weather
Waiting for God to open the Weather API endpoints
Any way to extend this to say a 10-day forecast?