Introduction
This article will interduce you to webservices and how you can interact with them using PowerShell. I'll also discuss the differences between web services and full SQL db access and how migrating to web services might affect your query access.
Background
The term Web services describes a standardized way of integrating Web-based applications using the XML, SOAP, WSDL and UDDI open standards over an Internet protocol backbone. Unlike traditional client/server models, such as a Web server/Web page system, Web services do not provide the user with a GUI.
Working for a large coporate entity as I do, we are encouraged to utilize PowerShell where possible. PowerShell is a wonderful tool for scripting within a Windows environment. Not only can you script windows administrative functions you can also generate e-mails, query databases and many other things.
Recently one of our internal SQL databases went through an overhaul and scope of access to the SQL database was narrowed. For us it meant most of the technology support team lost direct access to the database via SQL Management Studio. What the dba's offered us instead where web services access to query certain data as needed. I thought I would share what I have learned about web services.
Using the code
To illustrate how PowerShell can be used with Web services we are going to access data from the website: http://holidaywebservice.com/
If you go to http://holidaywebservice.com/HolidayService_v2/HolidayService2.asmx you'll see a list of available web services. As you can see below, there are 6 methods listed. These methods have been programmed so you can query data from them in specific ways.
To use a method from a webservice requires very little code as compaired to accessing a SQL or Access database through PowerShell.
$URI = "http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx"
$proxy = New-WebServiceProxy -Uri $URI -Class holiday -Namespace webservice
This is the only code I need to make a connection. For the variable $URI I simply provide the link. In the $proxy string the -Class, which is named "holiday" is not important, at least in the sense that I could name it anything I want. Likewise the name $URI is also not important, I could just as easily rename it to $URL or $Link. The -Uri is most important part of the string. This code below would work just as well.
$URL = "http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx"
$szProx = New-WebServiceProxy -Uri $URL -Class nationalholidays -Namespace webservice
With the connection string made, now I need to call one of the 6 methods available in order to pull data. Keep in mind the methods are case sensitive when calling them.
$URI = "http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx"
$szProx = New-WebServiceProxy -Uri $URI -Class holidays -Namespace webservice
$szProx.GetCountriesAvailable()
This is the list you should get when you run the script. This method does not require a variable (or a WHERE type of clause) to be passed to it. Some method will require that you declare what specifically you are searching for. For instance if I used the method GetHolidaysAvaliable() and nothing more I would get error. Instead I have to also specify a country, specifically a country code as listed above.
$URI = "http://www.holidaywebservice.com/HolidayService_v2/HolidayService2.asmx"
$szProx = New-WebServiceProxy -Uri $URI -Class holidays -Namespace webservice
$szProx.GetHolidaysAvailable('Canada')
Running this will return a list of avaliable holidays in Canada.
This example has shown you had to query data from a web service. I don't want to leave you with the impress that is all you can. Methods can be created to allow you to update, delete, modify, and create data as well. The ability to perform those tasks are defined by the method.
Points of Interest
If you encounter a change like this (going from full SQL db access to using web services) its important to remember your scope of access is limited to what the webservice and methods were programmed to offer you. Meaning if you had full access to a database table for "customers" or "servers" where in each table might include many fields the webservice version must be programmed to offer those all of those fields and the method to query them. Otherwise you may find your query access has been very limited.
History
Revision 1.0 7/9/2014