Click here to Skip to main content
15,882,063 members
Articles / Programming Languages / PowerShell
Tip/Trick

Query Webservices with Powershell

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
10 Jul 2014CPOL3 min read 40.8K   477   9   1
How to use Powershell to query webservices.

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. 

Image 1

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() 

Image 2

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. 

Image 3

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO J2R Scientific
United States United States
I own and operated J2R Scientific. I am the President of The Robotics Club of Yahoo. I have been a robotic enthusiast and robot builder since age 5. I hold several IT certifications. I have over 10 years of manufacturing development and prototyping experience. And I have over 20 years of combined amateur and professional robot building knowledge. I also currently work for United Health - Optum in their enterprise wide Technology Support department.

Learning is a life long passion for me and besides robotics I am drawn towards the subjects of artificial intelligence, how the human mind works, and history.

Comments and Discussions

 
QuestionThanks for promoting the awesomeness that is PowerShell Pin
Paul Cassidy12-Jul-14 17:26
Paul Cassidy12-Jul-14 17:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.