In this tutorial, I show you the basics of the parse XML file and access website.
Also, I show that with the graphical user interface (GUI) using Windows PowerShell.
It is very easy to use a Web service in PowerShell V2.
We can use that with the new Cmdlet New-WebServiceProxy.
The PowerShell knows how to handle XML. We just need to cast it to an XML object.
Let’s try to get the weather from the internet.
This is the source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | # Load the Winforms assembly [reflection.assembly]::LoadWithPartialName( "System.Windows.Forms") # Create the windows orm $form = New-Object Windows.Forms.Form #Set the dialog title $form.height = 300 $form.width = 500 $form.text = "PowerShell WinForms WEATHER " # Create the labels control and set text, size and location $label = New-Object Windows.Forms.Label $label.Location = New-Object Drawing.Point 50,30 $label.Size = New-Object Drawing.Point 400,15 $label.text = "Location" $label1 = New-Object Windows.Forms.Label $label1.Location = New-Object Drawing.Point 50,60 $label1.Size = New-Object Drawing.Point 400,15 $label1.text = "Wind :" $label2 = New-Object Windows.Forms.Label $label2.Location = New-Object Drawing.Point 50,90 $label2.Size = New-Object Drawing.Point 400,15 $label2.text = "Temperature :" $label3 = New-Object Windows.Forms.Label $label3.Location = New-Object Drawing.Point 50,120 $label3.Size = New-Object Drawing.Point 400,15 $label3.text = "Pressure :" # Create Button and set text , location and size $button = New-Object Windows.Forms.Button $button.text = "Click to check weather !" $button.Location = New-Object Drawing.Point 50,150 $button.Size = New-Object Drawing.Point 400,30 $weather = New-WebServiceProxy -uri http://www.webservicex.com/globalweather.asmx?WSDL # Button function click $button.add_click({ $label.Text = "Location :" + ([xml]$weather.GetWeather('London City Airport','United Kingdom')).CurrentWeather.Location $label1.Text = "Wind : " + ([xml]$weather.GetWeather('London City Airport','United Kingdom')).CurrentWeather.Wind $label2.Text = "Temperature :" + ([xml]$weather.GetWeather('London City Airport','United Kingdom')).CurrentWeather.Temperature $label3.Text = "Pressure :" + ([xml]$weather.GetWeather('London City Airport','United Kingdom')).CurrentWeather.Pressure }) $form.controls.add($button) $form.controls.add($label) $form.controls.add($label1) $form.controls.add($label2) $form.controls.add($label3) # Display the Form $form.ShowDialog() |
Run this script with PowerShell or with Windows PowerShell Integrated Scripting Environment (ISE).
Now that we see weather info from London City Airport – United Kingdom.