Keith's profileKeith Hill's BlogPhotosBlogListsMore Tools Help
    June 17

    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:DevEnvDir="$env:VSINSTALLDIR\Common7\IDE"
    $env:FrameworkSDKDir="$env:VSINSTALLDIR\SDK\v2.0"
    $FrameworkPath=$([System.Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory())
    $env:FrameworkDir=$(split-path $FrameworkPath -Parent)
    $env:FrameworkVersion=$(split-path $FrameworkPath -Leaf)
    $env:PATH="$env:VSINSTALLDIR\Common7\IDE;$env:VCINSTALLDIR\BIN;$env:VSINSTALLDIR\Common7\Tools;$env:VSINSTALLDIR\Common7\Tools\bin;$env:VCINSTALLDIR\PlatformSDK\bin;$env:FrameworkSDKDir\bin;$env:FrameworkDir\$env:FrameworkVersion;$env:VCINSTALLDIR\VCPackages;$env:PATH"
    $env:INCLUDE="$env:VCINSTALLDIR\ATLMFC\INCLUDE;$env:VCINSTALLDIR\INCLUDE;$env:VCINSTALLDIR\PlatformSDK\include;$env:FrameworkSDKDir\include;$env:INCLUDE"
    $env:LIB="$env:VCINSTALLDIR\ATLMFC\LIB;$env:VCINSTALLDIR\LIB;$env:VCINSTALLDIR\PlatformSDK\lib;$env:FrameworkSDKDir\lib;$env:LIB"
    $env:LIBPATH="$FrameworkPath;$env:VCINSTALLDIR\ATLMFC\LIB"
     
    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 http://www.webservicex.net/WeatherForecast.asmx? /proxy=myproxyserver:8080 
        /proxyusername=myusername /proxypassword=mypassword /proxydomain=USA

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

    Comments (4)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.
    Keith Hill has turned off comments on this page.
    Marco Shawwrote:
    Dunno, but it seems to be working for me now... 
    Sept. 8
    Marco Shawwrote:
    Has anything changed?  Can ya'll still connect to this?
    Saw this come through the other day:
    But I don't have it. 
    Sept. 2
    Steve Walkerwrote:
    I'm behind a corporate firewall... I get this error when I run the sample...
     
    Error: There was an error processing 'http://www.webservicex.net/WeatherForecast.asmx?WSDL'.
      - There was an error downloading 'http://www.webservicex.net/WeatherForecast.asmx?WSDL'.
      - The request failed with HTTP status 407: Proxy Authentication Required.
    Dec. 26

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

    Thanks!

    July 25

    Trackbacks

    Weblogs that reference this entry
    • None