A web service is generally an XML-based program. Using this, we can exchange data over a network. A web service is nothing but an application component, which works on an open protocol, can be used by other applications, and is reusable.
Web services allow interaction between applications and use standardized XML. Simply put, it's a combination of XML and HTTP. The following components work for all web services:
In this topic, we will demonstrate the use of web services and complex web services using Windows PowerShell.
New-WebServiceProxy
is a cmdlet that allows us to create a proxy object of any given valid web service.
For our example, we will use the following web service:
http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
Run the following command:
$var = New-WebServiceProxy -Uri http://www.webservicex.net/CurrencyConvertor.asmx?WSDL $var.ConversionRate
This returns the overload definitions of the conversion rate method, as shown in the following image:
Points marked in the image we just saw are explained as follows:
FromCurrency
ToCurrency
Here is a simple example of this:
$var = New-WebServiceProxy -Uri http://www.webservicex.net/CurrencyConvertor.asmx?WSDL $var.ConversionRate('Eur','US')
This returns the output as 1.0979
—the current conversion value.
How do we know the currency codes? Just open the following URL in a browser:
http://www.webservicex.net/CurrencyConvertor.asmx?WSDL
This shows the output as illustrated in the following image:
Similarly, we can use the GlobalWeather web service to retrieve information:
http://www.webservicex.net/globalweather.asmx?wsdl
Run the following command:
$weather = New-WebServiceProxy -uri http://www.webservicex.com/globalweather.asmx?WSDL ([XML]$weather.GetWeather('Amsterdam','Netherlands')).CurrentWeather
The output is illustrated in the following image:
Web services are usually limited to XML and SOAP, but they may be in the JSON format as well. So, it's worth considering an example that returns results in the JSON format.
Let's use an Apple iTunes URL, which returns an output in the JSON format:
http://itunes.apple.com/search?term=metallica
If we open this URL in the browser, we will see the output as shown in the following image:
Here, we will not use the Invoke-RestMethod
cmdlet because it formats the data by default. Instead, we will use the Invoke-WebRequest
cmdlet for the ConvertFrom-Json
cmdlet:
$Json = Invoke-WebRequest -Uri "http://itunes.apple.com/search?term=metallica" $Json
Take a look at the following image:
Now, we will play with the PowerShell code to convert this content into a readable format:
$Json = Invoke-WebRequest -Uri "http://itunes.apple.com/search?term=metallica" ($Json | ConvertFrom-Json).Results | Select WrapperType, trackName | Select -First 25
Now, consider the following image:
Alternatively, we can use the Invoke-RestMethod
cmdlet and do the same:
$Json = Invoke-RestMethod -Uri "http://itunes.apple.com/search?term=metallica" $Json.results | Select WrapperType , trackName | Select -First 25
In this exercise, we will take a look at the steps required to build web services. For this, we will use the following environments:
We configured IIS in WMF5Node03 to demonstrate PowerShell web access; now, let's connect to the same box and build a small web service. Perform the following steps:
http://localhost/website
.Visual Studio builds the basic web service. You can run the following command:
Imports System.Web Imports System.Web.Services Imports System.Web.Services.Protocols ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. ' <System.Web.Script.Services.ScriptService()> _ <WebService(Namespace:="http://tempuri.org/")> _ <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _ Public Class Service Inherits System.Web.Services.WebService <WebMethod()> _ Public Function HelloWorld() As String Return "This is a Basic and Default Web Service!" End Function End Class
Take a look at the following image:
So, now we have the URL; we simply changed the Hello World
text to something else, as follows:
http://localhost/website/Service.asmx
Okay, spin up PowerShell now!
Points marked in the figure are explained as follows:
?wsdl
$local
HelloWorld()
Invoke after doing text changes in the code which return your newly updated text
Now, in Service.vb
, add the following code:
<WebMethod()> _ Public Function Add(a, b) As Int32 Return a + b End Function
We can add two values in Windows PowerShell:
$local.Add(2,3)
Create your own web service and consume Windows PowerShell to use it.