PoshCode Archive  Artifact [ae973dc2d1]

Artifact ae973dc2d1d2312702c51a7fdd62f220dd3905ff1899455634d080469387fe50:

  • File Get-Weather.ps1 — part of check-in [b19c1152a9] at 2018-06-10 13:11:36 on branch trunk — This advanced function will allow you to retrieve the current weather information for a given country/city. (user: Boe Prox size: 3616)

# encoding: ascii
# api: powershell
# title: Get-Weather
# description: This advanced function will allow you to retrieve the current weather information for a given country/city.
# version: 0.1
# type: function
# author: Boe Prox
# license: CC0
# function: Get-Weather
# x-poshcode-id: 2508
# x-archived: 2013-05-26T12:08:33
# x-published: 2013-02-15T20:00:00
#
#
Function Get-Weather {
<#  
.SYNOPSIS  
   Display weather data for a specific country and city.
.DESCRIPTION
   Display weather data for a specific country and city. There is a possibility for this to fail if the web service being used is unavailable.
.PARAMETER Country
    URL of the website to test access to.
.PARAMETER ListCities
    Use the currently authenticated user's credentials  
.PARAMETER City
    Used to connect via a proxy         
.NOTES  
    Name: Get-Weather
    Author: Boe Prox
    DateCreated: 15Feb2011 
.LINK http://www.webservicex.net/ws/default.aspx
.LINK http://boeprox.wordpress.com       
.EXAMPLE  
    Get-Weather -Country "United States" -ListCities
    
Description
------------
Returns all of the available cities that are available to retrieve weather information from.
.EXAMPLE  
    Get-Weather -Country "United States" -City "San Francisco"
    
Description
------------
Retrieves the current weather information for San Francisco

#> 
[cmdletbinding(
	DefaultParameterSetName = 'Weather',
	ConfirmImpact = 'low'
)]
    Param(
        [Parameter(
            Mandatory = $True,
            Position = 0,
            ParameterSetName = '',
            ValueFromPipeline = $True)]
            [string]$Country,
        [Parameter(
            Position = 1,
            Mandatory = $False,
            ParameterSetName = 'listcities')]
            [switch]$ListCities,
        [Parameter(
            Mandatory = $False,
            ParameterSetName = '')]
            [string]$City                       
                        
        )
Begin {
    $psBoundParameters.GetEnumerator() | % {  
        Write-Verbose "Parameter: $_" 
        }     
    Try {
        #Make connection to known good weather service
        Write-Verbose "Create web proxy connection to weather service"
        $weather = New-WebServiceProxy 'http://www.webservicex.net/globalweather.asmx?WSDL'
        }
    Catch {
        Write-Warning "$($Error[0])"
        Break
        }        
    }
Process {
    #Determine if we are only to list the cities for a given country or get the weather from a city
    Switch ($PSCmdlet.ParameterSetName) {
        ListCities {
            Try {
                #List all cities available to query for weather
                Write-Verbose "Listing cities in country: $($country)"
                (([xml]$weather.GetCitiesByCountry("$country")).newdataset).table | Sort City | Select City
                Break
                }
            Catch {
                Write-Warning "$($Error[0])"
                Break
                }
            }
        Weather {
            Try {
                #Get the weather for the city and country
                Write-Verbose "Getting weather for Country: $($country), City $($city)"
                ([xml]$weather.GetWeather("$city", "$country")).CurrentWeather
                }
            Catch {
                Write-Warning "$($Error[0])"
                }
            }
        }
    }
End {
    Write-Verbose "End function, performing clean-up"
    Remove-Variable city -Force
    Remove-Variable country -Force
    }   
}