In this posting I'm going to show how you can generate a Webservice-Proxy on the fly in PowerShell. After that we'll use it with a SharePoint WebService. To generate the Webservice-Proxy Source Code we will use the Windows SDK Tool wsdl.exe. Then we'll compile the generated code with the .NET Framework Compiler csc.exe and load it into the PowerShell.
Let's go:
- Create a Script. Name it "Get-WebServiceProxy.ps1"
- First define the Path to the Tools as an Environment Variable
$env:WinSDK="$env:ProgramFiles\Microsoft SDKs\Windows\v6.0A\Bin"
$env:Net35="$env:SystemRoot\Microsoft.NET\Framework\v3.5"
- Build a Function to generate the Proxy
#Generates a Webservice-Proxy for the specified URL
function global:Get-WebServiceProxy([String]$url=$(throw
'Parameter -url is missing!'))
{
#generate a tempfile path
$tempFileName=[System.IO.Path]::GetTempFileName()
#generate the proxy code with wsdl.exe
$null=& $env:WinSdk\wsdl.exe $url /n:Proxy /out:"$tempFileName"
#comile the code
$null=& $env:Net35\csc.exe /t:library /out:"$tempFileName.dll"
"$tempFileName"
#load the assembly
$assembly=[System.Reflection.Assembly]::LoadFrom("$tempFileName.dll")
#find the proxy within the assembly
$proxyType=$assembly.GetTypes() Where-Object { $_.IsSubclassOf([System.Web.Services.Protocols.SoapHttpClientProtocol]) –eq
$true}
#instantiate the proxy
$proxy=New-Object
–TypeName
$proxyType
#return the proxy
return
$proxy;
}
- Save
- You can download the script on i Love SharePoint.
Now we will use it to get all Lists of a SharePoint Site
- Run the Script: PS> . .\Get-WebServiceProxy.ps1
- PS>$proxy = Get-WebServiceProxy http://localhost/_vti_bin/Lists.asmx
- PS>$proxy.UseDefaultCredentials = $true
- PS>$proxy.Url = http://localhost/JW/_vti_bin/Lists.asmx
- PS>$Lists = $proxy.GetListCollection()
- PS>$lists.List format-table -property Name, Title
- Now you should see something like this:
Name Title
-----------------------------------------------------------------------------------------------------
{3C851AD3-C988-43C5-8BC2-4141B1960AEF} Tasks
{169C2080-59B2-480E-A2F7-FD983D7CB759} Announcements
{FB4769F3-955D-42A5-A91F-D12BA5E5CAC9} Doucments
That's all! Create as many proxies as you want...
Find more SharePoint WebServices on MSDN.
There's a better script from Oisin Grehan for using WebServices with
PowerShell:http://poshcode.org/538
No comments:
Post a Comment