Retrieving Weather Information From A Web Service Provider

I noticed on my twitter feed about a PowerTip from powershell.com that showed you how to retrieve the weather information from a given web service. This is a pretty cool 3 lines of code that used the New-WebServiceProxy cmdlet that connects to the web service allows you to create a proxy object to manage the web service.

In short, a web service is defined as being:

A Web service is an XML-based program that exchanges data over a network, particularly over the Internet.

Here is the code example that was provided from powershell.com’s power tips section.

$a = New-WebServiceProxy 'http://www.webservicex.net/globalweather.asmx?WSDL'
$a.GetCitiesByCountry('United States')
$a.GetWeather('San Francisco','United States')

Capture

The GetCitiesByCountry() method will list all of the possible cities available to query in a given county. In this case, all of the cities in the United States was listed.

The GetWeather() method requires a string value for City and Country and returns the weather information. Now, the format returned is a string format that displays the xml content and isn’t the most presentable format. This is easily resolved by using [xml], which is an accelerator for the System.Xml.XmlDocument class to convert it into a more user friendly format. But just adding the [xml] type at the beginning of the code will not do anything.

[xml]$a.GetWeather('San Francisco','United States')

Capture

Ok, so now that we have it converted into an xml object, we just need to view the CurrentWeather property.

([xml]$a.GetWeather('San Francisco','United States')).CurrentWeather

Capture

Now that’s more like it. You can now easily view all of the weather information for a given city without trying to sift through all of the xml syntax.

Of course I couldn’t resist making this into an Advanced Function to call whenever I needed to.  I called it Get-Weather and with this function, you have two possible ways of using it.  You can use Get-Weather –Country “United States” –ListCities to list all of the available cities in the United States or any other respective country. To get the weather information, you use Get-Weather –Country “United States” –City “Omaha” and with that, you will get the weather information for the city of Omaha, Nebraska.

Capture

The link http://www.webservicex.net/ws/default.aspx contains many other services besides the weather.  I would encourage you to check these out and have fun making your own different functions or code snippets that use these other services and share them with others.

Script Download

Script Repository

PoshCode

Code

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  
    https://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 
    }    
}
This entry was posted in powershell, scripts and tagged , , . Bookmark the permalink.

1 Response to Retrieving Weather Information From A Web Service Provider

  1. Pingback: More Web Services with PowerShell: Geo IP Location | Learn Powershell | Achieve More

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s