This script gets current weather reports from 25 Colorado airports using a Web Service call to WebServiceX.net and presents the output in a customized DataGridView grid. Info collected includes wind speed and direction, visibility, sky conditions, temperature, humidity, pressure and dew point.
 
ColoradoWeather
 
I defined the Main WinForms Form and the Title Label.
 

Add-Type -AssemblyName System.Windows.Forms

# Main Form 
$mainForm = New-Object System.Windows.Forms.Form
$mainForm.Font = "Comic Sans MS,10"
$mainForm.Text = " Colorado Weather"
$mainForm.ForeColor = "White"
$mainForm.BackColor = "DarkBlue"
$mainForm.Width = 1200
$mainForm.Height = 520

# Title Label
$titleLabel = New-Object System.Windows.Forms.Label
$titleLabel.Font = "Comic Sans MS,14"
$titleLabel.Location = "45,10"
$titleLabel.Height = 30
$titleLabel.Width = 400
$titleLabel.Text = "Colorado Weather Info from 25 Airports"
$mainForm.Controls.Add($titleLabel)

 
I filled an array with the airport names using a Here-String. This will be used in the Web Service call. There are actually two calls associated with The Global Weather Web Service from WebServiceX.net. You can get a list of Cities (airports) by Country. And you can request current weather by city name. Unfortunately the state name is not included in the Cities by Country List data and so it is difficult to pull a particular states’ cities from an alphabetic list by country. I ended up Google’ing the Colorado airports list to make sure I had them all.
 

$locations = @(
    ("Air Force Academy")
    ("Akron, Akron-Washington County Airport")
    ("Alamosa, San Luis Valley Regional Airport")
    ("Aspen-Pitkin County Airport")
    .
    .
    .
    ("Rifle, Garfield County Regional Airport")
    ("Steamboat Springs")
    ("Telluride Regional")
    ("Trinidad / Animas Co.")
)

 
I setup the DataGridView Grid. You can just pipe the input array into the control and get a very nice grid output. However, I wanted to control colors, column width, column headers, etc., so I defined the grid characteristics manually.
 

# DataGrid Output
$dataGridViewOutput = New-Object System.Windows.Forms.DataGridView 
$dataGridViewOutput.Location = "45,50"
$dataGridViewOutput.Height = 400
$dataGridViewOutput.Width = 1100
$dataGridViewOutput.Font = "Comic Sans MS,8.25"
$dataGridViewOutput.ForeColor = "DarkBlue"
$dataGridViewOutput.BackColor = "White"
$dataGridViewOutput.GridColor = "Black"
$dataGridViewOutput.ColumnHeadersVisible = $true
$dataGridViewOutput.RowHeadersVisible = $false
$dataGridViewOutput.AllowUserToAddRows = $false
$dataGridViewOutput.ColumnCount = 8
$dataGridViewOutput.Columns[0].Name = "Colorado Airport"
$dataGridViewOutput.Columns[1].Name = "Wind Direction and Speed"
$dataGridViewOutput.Columns[2].Name = "Visibility"
$dataGridViewOutput.Columns[3].Name = "Sky Conditions"
$dataGridViewOutput.Columns[4].Name = "Temperature"
$dataGridViewOutput.Columns[5].Name = "Dew Point"
$dataGridViewOutput.Columns[6].Name = "Relative Humidity"
$dataGridViewOutput.Columns[7].Name = "Pressure"
$dataGridViewOutput.Columns[0].width = 290
$dataGridViewOutput.Columns[1].width = 230
$dataGridViewOutput.Columns[2].width = 80
$dataGridViewOutput.Columns[3].width = 100
$dataGridViewOutput.Columns[4].width = 100
$dataGridViewOutput.Columns[5].width = 100
$dataGridViewOutput.Columns[6].width = 110
$dataGridViewOutput.Columns[7].width = 70
$mainForm.Controls.Add($dataGridViewOutput) 

 
I call the WeatherLookup Function to populate the Form. I use New-WebServiceProxy to setup the proxy object to make the web service call. I loop through the locations calling the GetWeather service for each city. The response is the cities weather info as XML. I load this XML into an array and convert the wind system.string into a string. Then the values are loaded in the DataGridView grid.
 

Function WeatherLookup {
    $weather = New-WebServiceProxy -uri "http://www.webservicex.net/globalweather.asmx?WSDL"
    for ($i=0; $i -lt $locations.Count; $i++) {
        $weatherXML = [xml]$weather.GetWeather($locations.Item($i) , "United States")
        $selection  = $weatherXML.CurrentWeather | Select Location, Wind, Visibility, `
            SkyConditions, Temperature, DewPoint, RelativeHumidity, Pressure
        $wind = Out-String -InputObject $selection.Wind 
        $dataGridViewOutput.Rows.Add($locations.Item($i), `
            $wind, $selection.Visibility, $selection.SkyConditions, `
            $selection.Temperature, $selection.DewPoint, `
            $selection.RelativeHumidity, $selection.Pressure)
    } 
}

 
Here is the XML returned from the Web Service call.
 

# Output XML
#<?xml version="1.0" encoding="utf-16"?>
#<CurrentWeather>
#  <Location>DENVER CENTENNIAL AIRPORT, CO, United States (KAPA) 39-34N 104-51W 1775M</Location>
#  <Time>Oct 04, 2013 - 06:53 PM EDT / 2013.10.04 2253 UTC</Time>
#  <Wind> from the N (360 degrees) at 14 MPH (12 KT):0</Wind>
#  <Visibility> 10 mile(s):0</Visibility>
#  <SkyConditions> overcast</SkyConditions>
#  <Temperature> 37.9 F (3.3 C)</Temperature>
#  <DewPoint> 26.1 F (-3.3 C)</DewPoint>
#  <RelativeHumidity> 61%</RelativeHumidity>
#  <Pressure> 30.15 in. Hg (1020 hPa)</Pressure>
#  <Status>Success</Status>
#</CurrentWeather>

 
 
Download the complete script here – ColoradoWeather.ps1

Advertisements