Check weather using public API

In this article I want to show you how to use API for getting weather information. In this example I used metaweather API where additional authentication is not required.

First we need to understand a little bit what REST API is.

REST API – compliant Web services allow requesting systems to access and manipulate textual representations of Web resources using a uniform and predefined set of stateless operations.

In simple words you can access URL of API using Invoke-WebRequest or Invoke-RestMethod command and extract all information that you need.

For example to get Chuck Norris jokes you can run the following commands:


$joke = Invoke-RestMethod -Method Get -Uri https://api.chucknorris.io/jokes/random?category=dev -UseBasicParsing
$joke.value 

Below you can find Get-Weather function where you can specify name of a city and get weather statistics like:

– Temperature
– Wind Speed
– Wind Direction
– Air Pressure
– Predictability

Usage:

Get-Weather -City Chicago

Output:

Get-Weather
Get-Weather

Final script:


Function Get-Weather {
    [CmdletBinding()]
             
    # Parameters used in this function
    Param
    (
        [Parameter(Position=0, Mandatory = $True, HelpMessage="Provide city name", ValueFromPipeline = $true)] 
        $City
    ) 
     
    $ErrorActionPreference = "Stop"
    $WeatherResults = @()
    $Uri = "https://www.metaweather.com/api/location/search/?query=" + "$City"
    
    Try
    {
        $Location = Invoke-WebRequest -URI $Uri
    }
    Catch
    {
        $_.Exception.Message
        Break
    }

    If($Location)
    {
        $Location = $Location.content | ConvertFrom-Json 
        $Woeid = $Location.woeid
        $WeatherLink = "https://www.metaweather.com/api/location/" + $Woeid + "/"
    
        Try
        {
            $Weather = Invoke-WebRequest -URI $WeatherLink
        }
        Catch
        {
            $_.Exception.Message
            Break
        }
    
        If($Weather)
        {
            $Weather = $Weather.content  | ConvertFrom-Json 
            $Results = $Weather.consolidated_weather[0]

            $Object = New-Object PSObject -Property @{ 
    
                State                  = $Results.weather_state_name
                Date                   = $Results.created
                "Temperature(Min)"     = $Results.min_temp  
                "Temperature(Max)"     = $Results.max_temp          
                WindSpeed              = $Results.wind_speed
                WindDirection          = $Results.wind_direction_compass
                AirPressure            = $Results.air_pressure
                Predictability         = $Results.predictability     
            }

                $WeatherResults += $Object 
        }
    }

    If($WeatherResults)
    {
        Write-Host "`nWeather for " -NoNewline
        Write-Host "$City" -ForegroundColor Green -NoNewline

        $WeatherResults | select State,Date,"Temperature(Min)","Temperature(Max)",WindSpeed,WindDirection,AirPressure,Predictability
    }

}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.