I’ve been getting some questions about how to consume web services from PowerShell – so I wanted to give a quick overview. PowerShell offers some great tools for working with web services, but first, you must load the Visual Studio tools into your PS Environment. To do so, copy the following code into a script file:
$env:VSINSTALLDIR="C:\Program Files\Microsoft Visual Studio 8"
$env:VCINSTALLDIR="C:\Program Files\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"
*IMPORTANT: on a 64 bit machine you need to put the first variable ($env:VSINSTALLDIR) to be "C:\Program Files (x86)\Microsoft Visual Studio 8"
I put this in my Profile (“notepad $profile”) so that it loads every time I open PowerShell. You can load the script into your active Shell by “Dot Source” the script:
PS>. $profile
(Notice there is a space between the period and the name of the file).
Now we are ready to consume a web service. The following steps will consume the web service an create local .CS and .DLL files (in your active directory) which you can reuse later to communicate with the service. In this example, I’m getting the “Lists” web service from http://intranet, compiling the Lists.cs file, and then loading the Lists.dll into the shell:
PS>wsdl http://intranet/_vti_bin/Lists.asmx
PS>csc /t:library Lists.cs
PS>[Reflection.Assembly]::LoadFrom("Lists.dll")
The above steps do not need to be repeated (unless the web service code has changed) so you can add the following line to your $profile to always have the web service available (but you should put the full path to the “Lists.dll” file):
[void][Reflection.Assembly]::LoadFrom("Lists.dll")
Now we have the dll loaded (you can browse the .dll file using the .NET reflector to see all the available classes) and we can consume the web service. First we need to create a new “Lists” object:
PS> $list = New-Object Lists
We need to be authenticated to contact the web service, so type:
PS> $list.Credentials=[System.Net.CredentialCache]::DefaultCredentials
Now we are ready to use the web service – so call any member of the $list command to get the service data. The resulting objects can be managed just like any other PowerShell variable, so for example:
PS> $docs = $list.GetLists(“Shared Documents”)
PS> $docs.Title
Shared Documents
PS> $docs.Description
Share a document with the team by adding it to this document library.
PS> $docs.ItemCount
27
PS> $docs | Select Title,Description,ItemCount
Title Description ItemCount
----- ----------- ---------
Shared Documents Share a document with the team by ad... 27
If you want to know all the methods and properties available off an individual object, use the Get-Member command:
PS> $docs | Get-Member
That’s it! Here’s a list of some of the available web services:
Administration Service : http://<server-url:port-number>/_vti_adm/admin.asmx
Alerts Service : http://<server-url>/_vti_bin/alerts.asmx
Document Workspace Service : http://<server-url>/_vti_bin/dws.asmx
Forms Service : http://<server-url>/_vti_bin/forms.asmx
Imaging Service : http://<server-url>/_vti_bin/imaging.asmx
List Data Retrieval Service : http://<server-url>/_vti_bin/dspsts.asmx
Lists Service : http://<server-url>/_vti_bin/lists.asmx
Meetings Service : http://<server-url>/_vti_bin/meetings.asmx
Permissions Service : http://<server-url>/_vti_bin/permissions.asmx
Site Data Service : http://<server-url>/_vti_bin/sitedata.asmx
Site Service : http://<server-url>/_vti_bin/sites.asmx
Users and Groups Service : http://<server-url>/_vti_bin/usergroup.asmx
Versions Service : http://<server-url>/_vti_bin/versions.asmx
Views Service : http://<server-url>/_vti_bin/views.asmx
Web Part Pages Service : http://<server-url>/_vti_bin/webpartpages.asmx
Webs Service : http://<server-url>/_vti_bin/webs.asmx
Area Service : http://<server-url>/_vti_bin/areaservice.asmx
Query Service : http://<server-url>/_vti_bin/search.asmx
User Profile Service : http://<server-url>/_vti_bin/userprofileservice.asmx
SPS Crawl Service : http://<server-url>/_vti_bin/spscrawl.asmx
Outlook Adapter Service : http://<server-url>/_vti_bin/outlookadapter.asmx