PowerShell Function: Get-LocalWeather

Just a quick note here, you NEED to read the Comment Based Help for this function (especially if you are querying sites outside the US).

    ##--------------------------------------------------------------------------
    ##  FUNCTION.......:  Get-LocalWeather
    ##  PURPOSE........:  Queries the nearest airport for local weather 
    ##                    conditions.
    ##  REQUIREMENTS...:  Internet connection
    ##  NOTES..........:  
    ##--------------------------------------------------------------------------
    Function Get-LocalWeather {
        ##----------------------------------------------------------------------
        ##  Comment Based Help for this function.
        ##----------------------------------------------------------------------

        <#
        .SYNOPSIS
         Queries the nearest airport for local weather conditions.
        .DESCRIPTION
         This function uses webservicex.com to query data from airports and 
         weather observatories for local weather conditions.
        .PARAMETER Site
         Name of the Airport or weather observatory to query.

         ALIAS:  -s
        .PARAMETER ListAll
         This optional switch returns a list of all available airports and 
         weather observatories available in the selected country (default is the
         United States).

         ALIAS:  -l
        .PARAMETER TempF
         This optional switch returns only the temperature (in Fahrenheit) for 
         the specified location.

         ALIAS:  -tf
        .PARAMETER TempC
         This optional switch returns only the temperature (in Celsius) for the
         specified location.

         ALIAS:  -tc
        .Parameter Humidity
         This optional switch returns only the relative humidity for the 
         specified location.

         ALIAS:  -h
        .PARAMETER CountryName
         This optional parameter changes the Country queried (the default is the
         United States). 

         NOTE:
         If you are querying a data source outside the US, you MUST use this 
         parameter.

         ALIAS:  -c
        .EXAMPLE
         C:\PS>Get-LocalWeather "Knoxville"

         This will return the current weather conditions at the Knoxville 
         Airport.

        .EXAMPLE
         C:\PS>"Knoxville" | Get-LocalWeather

         This will return the current weather conditions at the Knoxville 
         Airport using pipelining.

        .EXAMPLE
         C:\PS>Get-LocalWeather -l | More

         This Example will display a list of all available Airports / Weather 
         Observatories in the US (I piped it to More because there are many).

        .EXAMPLE
         C:\PS>Get-LocalWeather -l -c "Canada" | More

         This Example will display a list of all available Airports / Weather 
         Observatories in Canada (I piped it to More because there are many).

        .EXAMPLE 
         C:\PS>Get-LocalWeather "Wyton" -c "United Kingdom"

         This example will return weather data for the Wyton airport in the UK.

        .NOTES
         NAME......:  Get-LocalWeather
         AUTHOR....:  Joe Glessner
         LAST EDIT.:  25JAN13
         CREATED...:  03DEC12
        .LINK
         https://joeit.wordpress.com/
        #>

        ##----------------------------------------------------------------------
        ##  Function Parameters.
        ##----------------------------------------------------------------------
        Param([Parameter(Mandatory = $False,
            ValueFromPipeLine = $True,
            Position = 0)]
            [Alias('s')]
            [String]$Site,
            [Parameter(Mandatory = $False)]
            [Alias('c')]
            [String]$CountryName,
            [Parameter(Mandatory = $False)]
            [Alias('l')]
            [Switch]$ListAll,
            [Parameter(Mandatory = $False)]
            [Alias('tf')]
            [Switch]$TempF,
            [Parameter(Mandatory = $False)]
            [Alias('tc')]
            [Switch]$TempC,
            [Parameter(Mandatory = $False)]
            [Alias('h')]
            [Switch]$Humidity
        )#END: Param

        Begin {
            $URL = 'http://www.webservicex.com/globalweather.asmx?WSDL'
            $weather = New-WebServiceProxy -Uri $URL
            If ($CountryName) {
                $Co = $CountryName
            } #END: If ($CountryName)
            Else {
                $Co = 'United States'
            } #END: Else
            Write-Verbose "$Site"
            Write-Verbose "$Co"
        } #END: Begin
        Process {
            If ($ListAll) {
                $List = `
                ([xml]$weather.GetCitiesByCountry($Co)).NewDataSet.Table | 
                Select-Object -ExpandProperty City
                $List
            } #END: If ($ListAll)
            If (!(($Site) -or ($ListAll))) {
                Write-Warning "You must enter the exact location name for your"
                Write-Warning "desired Airport/observatory."
                Write-Warning ""
                Write-Warning "Use the -l switch to get a list of valid site"
                Write-Warning "names."
            } #END: If (!($Site))
            Else {
                Write-Verbose "If you see a Type conversion error here, either"
                Write-Verbose "your query was incorrect, or there is no data"
                Write-Verbose "for the site you queried."
                $Data = ([xml]$weather.GetWeather($Site,$Co)).CurrentWeather  
            } #END: Else
            If ($TempF) {
                $Temp = ($Data.Temperature -split '\(')[0]
                $Temp.TrimStart()
                Break;
            } #END: ($TempF)
            If ($TempC) {
                $Temp = ($Data.Temperature -split '[\(\)]')[1]
                $Temp.TrimStart()
                Break;
            } #END: If ($TempC)
            If ($Humidity) {
                $Data.RelativeHumidity.TrimStart()
                Break;
            } #END: If ($Humidity)
            ElseIf ($Site) {
                $Data
            } #END: If ($Site)
        }#END: Process
    } #END: Function Get-LocalWeather

end.jpg

Advertisements
%d bloggers like this: