Skip to content

Instantly share code, notes, and snippets.

@PrateekKumarSingh
Last active August 8, 2020 21:19
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save PrateekKumarSingh/cf641670f89be6c8e0c3c4af73caf914 to your computer and use it in GitHub Desktop.
Save PrateekKumarSingh/cf641670f89be6c8e0c3c4af73caf914 to your computer and use it in GitHub Desktop.
<#
.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
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment