Get-Weather.ps1


Description

Purpose

Get the weather information for a specific city.

Detailed Description

The Get-Weather function retrieves the current weather information for a specified city using the wttr.in service.

Back to Top

Usage

Example 1

Get-Weather -City 'Southend-on-Sea'

Retrieves the weather information for the city of Southend-on-Sea.

Back to Top

Notes

This function requires an internet connection to retrieve the weather information.

Back to Top


Script

function Get-Weather {
    <#
    .SYNOPSIS
    Get the weather information for a specific city.

    .DESCRIPTION
    The Get-Weather function retrieves the current weather information for a specified city using the wttr.in service.

    .PARAMETER City
    The name of the city for which to retrieve the weather information.

    .EXAMPLE
    Get-Weather -City 'Southend-on-Sea'
    Retrieves the weather information for the city of Southend-on-Sea.

    .NOTES
    This function requires an internet connection to retrieve the weather information.
    #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [string]$City
    )

    Begin {
        $Protocols = [Net.SecurityProtocolType]::Tls12
        [Net.ServicePointManager]::SecurityProtocol = $Protocols
    }

    Process {
        try {
            $Weather = Invoke-RestMethod -Uri "http://wttr.in/$City" -ErrorAction Stop
            if ($Weather) {
                Write-Output $Weather
            }
            else {
                Write-Warning "No weather information found for city: $City"
            }
        }
        catch {
            Write-Error "Failed to retrieve weather information for city: $City. Error: $_"
        }
    }

    End {}
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top