Working With Web Services (Post CTP2 V2 Feature)

Working With Web Services (Post CTP2 V2 Feature)

  • Comments 8

Let me start by first apologizing for sharing some information that you are not going to be able to go out and try right away.  During one of the interactive Q&A sessions at TechEd, people asked about working with Web Services.  I referred to a feature that we were working on and then Hal Rottenberg asked me to blog about some of the details.  I'm not sure that this is a good idea because while we have it coded up and in the nightly builds, that is not a guarantee that it will make it into the next version (we often have to cut features in order to create bandwidth to increase the quality of other features).  That said, I'm going to go out on the limb a bit and share with you what we are working on.

First let's start with motivation (you should always start with motivation.  If you know WHY someone  is doing something, it is easier to get things in focus and provide meaningful feedback).  PowerShell is ALL about helping you get your job done with the minimal amount of effort (I've been thinking about calling this the "Cheap EATs" model where EAT stands for "Effort to Accomplish Task").   With that comes the observation that we live in an evolving world.  To accomplish your task, you need to easily access a wide range of technologies.  That is why we invest in giving you great access to WMI, ADSI, COM, XML, .NET, ADO, etc etc.  More and more things are now available as Web Services so we want to make it easy for you to get at them as well. 

As such, we have speced and implemented the cmdlet:  New-WebServiceProxy.    This command connects to a Web Service and generates a .NET proxy to that service.  Let me show you how it works using a web service which works with USZIP codes (http://www.webservicex.net/uszip.asmx ):

PS> $ws=New-WebServiceProxy -uri http://www.webservicex.net/uszip.asmx?WSDL
PS> # NOTE - the output of the next command was edited for clarity
PS> Get-Command New-WebServiceProxy -Syntax
New-WebServiceProxy [-Uri] <Uri> [[-Class] <String>] [[-Namespace] <String>]

New-WebServiceProxy [-Uri] <Uri> [[-Class] <String>] [[-Namespace] <String>]
[-Credential <PSCredential>]

New-WebServiceProxy [-Uri] <Uri> [[-Class] <String>] [[-Namespace] <String>]
[-UseDefaultCredential]


PS> $ws=New-WebServiceProxy -uri http://www.webservicex.net/uszip.asmx?WSDL
PS> $ws | get-Member


   TypeName: Microsoft.PowerShell.Commands.NewWebserviceProxy.Autogenerated
Types.WebServiceProxy4979207ebservicex_net_uszip_asmx_WSDL.USZip

Name                                 MemberType Definition
----                                 ---------- ----------
Disposed                             Event      System.EventHandler Disp...
Abort                                Method     System.Void Abort()
BeginGetInfoByAreaCode               Method     System.IAsyncResult Begi...
BeginGetInfoByCity                   Method     System.IAsyncResult Begi...
BeginGetInfoByState                  Method     System.IAsyncResult Begi...
BeginGetInfoByZIP                    Method     System.IAsyncResult Begi...
CreateObjRef                         Method     System.Runtime.Remoting....
Discover                             Method     System.Void Discover()
Dispose                              Method     System.Void Dispose()
EndGetInfoByAreaCode                 Method     System.Xml.XmlNode EndGe...
EndGetInfoByCity                     Method     System.Xml.XmlNode EndGe...
EndGetInfoByState                    Method     System.Xml.XmlNode EndGe...
EndGetInfoByZIP                      Method     System.Xml.XmlNode EndGe...
Equals                               Method     System.Boolean Equals(Ob...
GetHashCode                          Method     System.Int32 GetHashCode()
GetInfoByAreaCode                    Method     System.Xml.XmlNode GetIn...
GetInfoByCity                        Method     System.Xml.XmlNode GetIn...
GetInfoByState                       Method     System.Xml.XmlNode GetIn...
GetInfoByZIP                         Method     System.Xml.XmlNode GetIn...
GetLifetimeService                   Method     System.Object GetLifetim...
GetType                              Method     System.Type GetType()
InitializeLifetimeService            Method     System.Object Initialize...
ToString                             Method     System.String ToString()
AllowAutoRedirect                    Property   System.Boolean AllowAuto...
ClientCertificates                   Property   System.Security.Cryptogr...
ConnectionGroupName                  Property   System.String Connection...
Container                            Property   System.ComponentModel.IC...
CookieContainer                      Property   System.Net.CookieContain...
Credentials                          Property   System.Net.ICredentials ...
EnableDecompression                  Property   System.Boolean EnableDec...
PreAuthenticate                      Property   System.Boolean PreAuthen...
Proxy                                Property   System.Net.IWebProxy Pro...
RequestEncoding                      Property   System.Text.Encoding Req...
Site                                 Property   System.ComponentModel.IS...
SoapVersion                          Property   System.Web.Services.Prot...
Timeout                              Property   System.Int32 Timeout {ge...
UnsafeAuthenticatedConnectionSharing Property   System.Boolean UnsafeAut...
Url                                  Property   System.String Url {get;s...
UseDefaultCredentials                Property   System.Boolean UseDefaul...
UserAgent                            Property   System.String UserAgent ...


PS> $ws.GetInfoByZIP(98072)

xmlns                                 Table
-----                                 -----
                                      Table


PS> $ws.GetInfoByZIP(98072).Table


CITY      : Woodinville
STATE     : WA
ZIP       : 98072
AREA_CODE : 425
TIME_ZONE : P

 

PS> $ws.GetInfoByAreaCode(425).table |ft -auto

CITY              STATE ZIP   AREA_CODE TIME_ZONE
----              ----- ---   --------- ---------
Snoqualmie        WA    98065 425       P
Woodinville       WA    98072 425       P
Bellevue          WA    98004 425       P
Bellevue          WA    98005 425       P
Bellevue          WA    98006 425       P
Bellevue          WA    98007 425       P
Bellevue          WA    98008 425       P
Bellevue          WA    98009 425       P
Bellevue          WA    98015 425       P
Bothell           WA    98011 425       P
Bothell           WA    98012 425       P
Bothell           WA    98021 425       P
Bothell           WA    98041 425       P
Bothell           WA    98082 425       P
Carnation         WA    98014 425       P
Duvall            WA    98019 425       P
Edmonds           WA    98020 425       P
Edmonds           WA    98026 425       P
Everett           WA    98201 425       P
Everett           WA    98203 425       P
Everett           WA    98204 425       P
Everett           WA    98205 425       P
Everett           WA    98206 425       P
Everett           WA    98207 425       P
Everett           WA    98208 425       P
Hobart            WA    98025 425       P
Issaquah          WA    98027 425       P
Issaquah          WA    98029 425       P
Kirkland          WA    98033 425       P
Kirkland          WA    98034 425       P
Kirkland          WA    98083 425       P
Lake Stevens      WA    98258 425       P
Lynnwood          WA    98036 425       P
Lynnwood          WA    98037 425       P
Lynnwood          WA    98046 425       P
Maple Valley      WA    98038 425       P
Medina            WA    98039 425       P
Mountlake Terrace WA    98043 425       P
Mukilteo          WA    98275 425       P
North Bend        WA    98045 425       P
Preston           WA    98050 425       P
Ravensdale        WA    98051 425       P
Redmond           WA    98052 425       P
Redmond           WA    98053 425       P
Redmond           WA    98073 425       P
Renton            WA    98055 425       P
Renton            WA    98056 425       P
Renton            WA    98057 425       P
Renton            WA    98058 425       P
Renton            WA    98059 425       P


Enjoy!

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 8 and 2 and type the answer here:
  • Post
  • I think not only are CTPs a nice look into the future, but also these little bits of information are also nice to know.

    I'm glad to know this is something being strongly considered for a future release...

  • I need it now! Wondering if you could post something along the same lines which makes use of HTTP POST to communicate with POX based services on WCF.

  • This is great! As a heavy Exchange 2007 admin, I have already searched for snippets that work with web services. It is nice to have this finally built-in. I could have used this months ago. When can we get the updated code?

  • Great stuff and I can think of umpteen ways to leverage this.

    As the 1st comment/reply states, "these little bits of information are also nice to know."

    Could not agree more.

    L in IL

  • very interesting feature! we are awaiting the next powershell update!

  • It hasn't been normal practice for the PowerShell team to announce CTPs ahead of time.  CTP1 and CTP2 were publically announced only a few days before release.

    If I could offer you a *guess* based on the CTP1 and CTP2 timelines, I'd *guess* we might see CTP3 just before the end of 2008.

    Santa might be coming early...

  • All,

    Check this post for info on how one can use the .NET Framework (and packaged C# compiler) to call a webservice:

    http://keithhill.spaces.live.com/blog/cns!5A8D2641E0963A97!512.entry

    (You don't have to setup all of the Visual Studio environment variables actually.)

  • Please make sure to support WCF out of the box. In particular, you need to support metadata exchange (mex).

    Also, please be sure to test scenarios where the WSDL and schemas are being read off of disk, and not being fetched over the Internet.

Page 1 of 1 (8 items)