Use Powershell To Get Weather Using Wttr.in

1 minute read

Description:

Wttr.in has a neat API for the site that generates cmdline views of weather for your city. I especially like the moon phases as well.

Note: This doesn’t always play well with the ISE, it’s best to just put it in a ps1 function and call it that way.

To Resolve:

  1. Script:
[Cmdletbinding(DefaultParametersetName = 'Default')]
    Param
    (
        [String]$City,
		
        [Switch]$JustToday,
		
        [Switch]$TwoDays,
		
        [ValidateLength(5, 5)]
        [String]$ByZip,
		
        [Switch]$Moon,
		
        [ValidateLength(10, 10)]
        [String]$MoonOnDate,
			
        [String]$Logfile = "$PSScriptRoot\..\Logs\Get-Weather.log"
    )
    
    
    Process
    {   
        
        If ($City)
        {
            Log "Getting weather for $City"
            (curl http://wttr.in/$City -UserAgent "curl" ).Content
            # Cannot substitue "curl" for "Invoke-WebRequest -URI" ....
        }
        ElseIf ($JustToday)
        {
            Log "Getting weather for today's current location"
            (curl http://wttr.in/?0 -UserAgent "curl" ).Content
        }
        ElseIf ($TwoDays)
        {
            Log "Getting two days weather forcast for current location"
            (curl http://wttr.in/?2 -UserAgent "curl" ).Content
        }
        ElseIf ($ByZip)
        {
            Log "Getting weather for zipcode $ByZip"
            (curl http://wttr.in/$ByZip -UserAgent "curl" ).Content
        }
        ElseIf ($Moon)
        {
            Log "Getting today's moon phase"
            (curl http://wttr.in/moon -UserAgent "curl" ).Content
        }
        ElseIf ($MoonOnDate)
        {
            Log "Getting moon phase for $MoonOnDate"
            (curl http://wttr.in/moon@$MoonOnDate -UserAgent "curl" ).Content
        }
        Else
        {
            Log "Getting weather for current location"
            (curl http://wttr.in -UserAgent "curl" ).Content
        }
    
    }

 

This can be found in my gwMisc section on Github.