Keith's profileKeith Hill's BlogPhotosBlogListsMore ![]() | Help |
|
June 17 Calling a Web Service from PowerShellBefore 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 1> wsdl.exe http://www.webservicex.net/WeatherForecast.asmx?WSDL
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)
Keith Hill
has turned off comments on this page.
TrackbacksWeblogs that reference this entry
|
|
|