I was recently writing a script for a client that had to retrieve data from Active Directory and a WCF service. Getting the data from Active Directory is very easy using the Quest Active Directory cmdlets or using the System.DirectoryServices.DirectoryEntry and System.DirectoryServices.DirectorySearcher classes. Retrieving data from the WCF service in Powershell wasn’t so straightforward. Unfortunately, the client wasn’t running Powershell V 2.0 in production so I had to use Powershell V 1. Version 2 introduces the New-WebServiceProxy cmdlet which creates the web service proxy for you, but in version 1 you have to create the proxy manually. With a lot of help from one of my coworkers and this article posted by Keith Hill, I was able to call the WCF service from Powershell and retrieve data from it. I should also note that you will need to install the .Net Framework SDK on the computer you are running the Powershell script from. Ok, so now I will go through the script I wrote to create the WCF proxy.
The first three lines of the script set all the necessary environment variables for you. You may have to modify these values in your environment.
$env:WCF=”C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin”
$env:CSC=”C:\Windows\microsoft.net\framework\v2.0.50727″
$env:Path=”$env:WCF;$env:CSC”
The next line of the script takes the location of the WCF WSDL as an argument to svcutil.exe. Svcutil basically reads the WSDL and generates a file with the name of your service with a .CS extension.
svcutil.exe <enter the URL to the WSDL here>
Example:
svcutil.exe http://yourserver.yourdomain.com/services/TestService.svc?wsdl
The next line of code calls the C# compiler and takes the file that the svcutil.exe just created for you as its argument. The /r: switch adds any DLL’s that the WCF service needs to reference.
csc /t:library TestService.cs TestService.cs /r:”Path to DLL”
The next lines of code load the the assembly that was just created in the script and a reference assembly. The “$pwd” variable just holds your current working directory.
[Reflection.Assembly]::LoadFrom(“$pwd\TestService.dll”)
[Reflection.Assembly]::LoadWithPartialName(“System.ServiceModel”)
The next lines of the script create the WSHttpBinding object and EndPointAddress object used by WCF. The EndPointAddress object requires the WCF URL as its parameter.
$wsHttpBinding=New-Object system.servicemodel.WSHttpBinding
$endpoint=New-Object System.servicemodel.endpointaddress(“http://yourserver.yourdomain.com/services/TestService.svc”)
The last line of the scripts creates the object that you will use to interact with the WCF service. WHen you create this object you have to supply $wsHttpBinding and $endpoint variables as its parameters.
$testService=New-Object TestSevice($wsHttpBinding, $endpoint)
The complete script is shown below:
$env:WCF=”C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin”
$env:CSC=”C:\Windows\microsoft.net\framework\v2.0.50727″
$env:Path=”$env:WCF;$env:CSC”
svcutil.exe http://yourserver.yourdomain.com/services/TestService.svc?wsdl
csc /t:library TestService.cs TestService.cs /r:”Path to DLL
[Reflection.Assembly]::LoadFrom(“$pwd\TestService.dll”)
[Reflection.Assembly]::LoadWithPartialName(“System.ServiceModel”)
$wsHttpBinding=New-Object system.servicemodel.WSHttpBinding
$endpoint=New-Object System.servicemodel.endpointaddress(“http://yourserver.yourdomain.com/services/TestService.svc”)
$testService=New-Object TestService($wsHttpBinding, $endpoint)
You can now save this script as CreateWCF.ps1 and dot source it into your shell like this
Posh 1> . \CreateWCF.ps1
Now from your shell you will have access to the “$testService” variable. You can now call the WCF methods.
In Version 2.0 of Powershell you could do all this in one line :
$testService=New-WebServiceProxy –Uri “http://yourserver.yourdomain.com/services/TestService.svc?wsdl ”
Image may be NSFW.
Clik here to view.
Clik here to view.
