Calling a Web Service from PowerShell

Before starting this you need to setup PS environment to enable the Visual Studio 2005 tools.  Copy the following into a file called something like vs80vars.ps1:
$env:VSINSTALLDIR="$env:ProgramFiles\Microsoft Visual Studio 8"
$env:VCINSTALLDIR="$env:ProgramFiles\Microsoft Visual Studio 8\VC"
$env:FrameworkDir=$(split-path $FrameworkPath -Parent)
$env:FrameworkVersion=$(split-path $FrameworkPath -Leaf)
Then "dot" the attached PS1 script into your shell like so"
PoSH 1> . .\vs80vars.ps1
Now here’s how to acces one particular web service.  Note that the first two lines below create a proxy assembly which takes care of the communication to the web service.  On line 3 and 4 we load the assembly and then create the WeatherForecast proxy object.  Then on line 5 we call one of its methods.  It returns an XML document which we store in $forecast.  BTW, you can use Reflector or Visual Studio’s Object Browser to see the other methods in the assembly (or you can open the WeatherForecast.cs file to see the nitty gritty proxy details).
PoSH 2> csc /t:library WeatherForecast.cs
PoSH 3> [Reflection.Assembly]::LoadFrom("$pwd\WeatherForecast.dll")
PoSH 4> $weatherService = new-object WeatherForecast
PoSH 5> $forecast = $weatherService.GetWeatherByZipCode(80526)
PoSH 6> $forecast

Latitude         : 40.54729
Longitude        : 105.1076
AllocationFactor : 0.008857
FipsCode         : 08
PlaceName        : FORT COLLINS
StateCode        : CO
Status           :
Details          : {WeatherData, WeatherData, WeatherData, WeatherData…}
PoSH> $forecast.Details |select Day, MaxT*F, MinT*F

Day                        MaxTemperatureF            MinTemperatureF
—                        —————            —————
Thursday, June 15, 2006    84                         54
Friday, June 16, 2006      80                         50
Saturday, June 17, 2006    85                         55
Sunday, June 18, 2006      91                         58
Monday, June 19, 2006      95                         59
Tuesday, June 20, 2006     94                         58
Wednesday, June 21, 2006   90                         56
See it gets a bit warmer here in Colorado than some folks would suspect.  🙂
Updated 12/27/2006: If you are behind a corporate firewall you might need to run WSDL.exe with additional options:
wsdl.exe /proxy=myproxyserver:8080 
    /proxyusername=myusername /proxypassword=mypassword /proxydomain=USA

Thanks to sundanceca for figuring the proxy options to do this.

This entry was posted in PowerShell. Bookmark the permalink.

7 Responses to Calling a Web Service from PowerShell

  1. Ivan says:

    Great! Just what I was looking for. This makes Powershell great simple testing site for web services.

  2. Steve says:

    I\’m behind a corporate firewall… I get this error when I run the sample…
    Error: There was an error processing \’\’.  – There was an error downloading \’\’.  – The request failed with HTTP status 407: Proxy Authentication Required.

  3. Marco says:

    Has anything changed?  Can ya\’ll still connect to this?
    Saw this come through the other day:;en-us;940959&sd=rss&spid=8291
    But I don\’t have it. 

  4. Marco says:

    Dunno, but it seems to be working for me now… 

  5. Jānis Veinbergs says:

    That looks like you must have visual studio installed to use those methods?

    Its easyer to use a powershell script that acts as a webservice wrapper and needs no dependencies:
    Look at the get-webservice2.ps1 – and you can use webservice methods like you do here.

  6. Matthias says:

    @Jānis Veinberg
    Great link, since I do not have visual Studio as well that is a perfect alternative!!

  7. Pingback: Calling a WCF Service from PowerShell | Keith Hill's Blog

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s