Calling a SOAP web service from PowerShell 3.0

The other day, I wrote a longer blog post on some of the highlights of PowerShell. In there, I included an example of how to call a RESTful web service and how to make a HTTP call to an RSS feed.

Another common scenario might be that you want to get data from a SOAP-based web service. From the PowerShell 3.0 Step-By-Step book, they use www.webservicex.net in several places to showcase this. That is a website which has some free web services which you can use for testing.

So, how do you call a web service? Well, you need to create a proxy, then make the call (letting PowerShell know that the response will be XML), and then walk through or display the results. For example:

$proxy = New-WebServiceProxy -Uri http://www.webservicex.net/globalweather.asmx?WSDL
[xml]$response = $proxy.GetWeather("Tampa", "United States")
$response.CurrentWeather | Format-List

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

This results in something like this:

mx3C338

What’s even cooler is that if I write this code in ISE, I get intellisense for what the data structure looks like:

mx3D92D

So, I can dump the results out on the screen like we did above, or I also have direct access to each one of the properties which were returned.

It’s pretty cool I can do this with exactly 3 lines of code – very simple! For anyone not familiar with PowerShell, this isn’t some part of a bigger program. What I mean is, you can launch PowerShell on your computer right now, paste in those 3 lines of code – with nothing else – and you should see the same response on your own computer. Pretty cool stuff…

Posted in PowerShell, Uncategorized, Web Services
11 comments on “Calling a SOAP web service from PowerShell 3.0
  1. Neeraj_Pandey says:

    Hi Robert ,
    Greetings.
    Need your suggestions for the following scenario-
    My network allows internet access through web proxy server. While writing script to make SOAP calls to a server which is in internet i have to specify URL,Port and credentials of web proxy server, and the application I am making SOAP calls to requires https authentication.
    Need your suggestions to specify both credentials (web proxy server and application ) in the script.
    Thanks and regards.

    Like

  2. Matt Murphy says:

    Hi Neeraj_Pandey,

    You can use these one of these two switches after creating the proxy object -UseDefaultCredential or -Credential

    UseDefaultCredential will attempt to pass your currently logged in Windows domain user to the site. The other switch allows you to specify credentials.

    if you need HTTPS then change the protocol in the URI string to https. SOAP allows RPC to traverse a firewall so normally uses port 80, but if you use HTTPS then it will use port 443.

    For example:
    $proxy = New-WebServiceProxy -Uri http://www.webservicex.net/globalweather.asmx?WSDL -UseDefaultCredential

    Like

  3. Shubham Garg says:

    I have a web service for which wsdl is not in pord environemnt exposed. how can i call that service. i tried by creating a wsdl file from qa but it takes qa xsd’s. please suggest it is critical

    Like

    • Robert Seder says:

      I don’t know of a way you can. This is part of the the security of SOAP services, that you MUST know the interface and data structures in order to use the service. If you don’t, I don’t know of any way around that. Sorry!

      Like

  4. Shubham Garg says:

    I did below and it worked :
    1. Created proxy from qa .
    2. Create a class library project and added the proxy class there.
    3. COnsumed the above created dll in powershell .

    Like

  5. Jorge Rial says:

    Hi Robert,

    I’m new in these matters and I need to call a SOAP WebService, that service search certain parameters, I need to pass these parameters but the the service that you are using doesn`t accept input parameters, how you specify the parameters? which service in Powershell should I use?

    I neew to execute something like the following code:

    BROADBAND
    OLT
    OLT_SYNC_checkONTport

    filemaker

    I use the ISE as you recommend, but I don’t know how to proceed, any advice will be welcome.

    Thank you

    Like

  6. Matt Murphy says:

    I use WCFStorm to expose methods exposed by SOAP. You can download a trail copy from here (http://www.wcfstorm.com/wcf/home.aspx)

    Tbh, the PowerShell IDE should expose the methods that are available like Robert described in his post. I would recommend recreating Robert’s example in the post to prove that you have the IDE setup correctly and then test with your SOAP API to see if it exposes the methods.

    Like

  7. […] Calling a SOAP web service from PowerShell 3.0 […]

    Like

  8. […] Source: Calling a SOAP web service from PowerShell 3.0 | Rob Seder […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Archives
Categories

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 5 other followers

%d bloggers like this: