Monday, March 1, 2010

Calling a Web Service with PowerShell

I often need to be notified of a certain condition when a scheduled script executes.
With PowerShell V2, I can take advantage of our internal paging web service by using the New-WebServiceProxy cmdlet to take care of this notification.

Check out the help on this cmdlet to see a full list of capabilities. Help New-WebServiceProxy -Full

Before we dive into the actual call, lets interrogate the web service to see what it can do.
# Create a proxy for the Paging web service
$page = New-WebServiceProxy -Uri 'http://InternalPagingService/pageservice.asmx'

#List the methods
$page Get-Member -MemberType Method
You should see something like this:

The method we are going to use is RequestSinglePage.

Enter the following to see the expected parameters:
($page Get-Member -Name RequestSinglePage).definition

Looking at the end of the definition we see 2 parameters:
- string PagerId
- string NumericOrAlphaMessage

We now have all we need to page from script!
$page.RequestSinglePage('3141','Testing Page Web Service from PowerShell')

If you need confirmation of the Web Service call, you can look at the FunctionStatus property of the executed Web Service.
($page.RequestSinglePage('3141','Test Page from PowerShell')).FunctionStatus

This returns - SUCCESS

Wrapping this web service call around some return code if fairly straight forward and is left as an exercise for the reader.

If you don't have an internal Web Service handy, try out http://www.webservicex.net/WeatherForecast.asmx

Enjoy!

3 comments:

Unknown said...

The syntax I had to use for getting the list of members was:

> Get-Member -InputObject $page -MemberType Method

Anonymous said...

Thanks for the writeup!

I put a pipe '|' between $page and Get-Member; probably got swallowed up by html rendering.

StockTrader said...

Hello,

have you tried to invoke web service methods that require as parameter a datatype that is defined by the WebService itself?
I found out that it is a bit tricky...
regards,
StockTrader