Sunday, July 8, 2012

PowerShell (v3) - Get Exported Objects from New WebServiceProxy

An old work project forced me to revisit the New-WebServiceProxy cmdlet. After talking to Trevor Sullivan (@pcgeek86) a bit I threw a post on Twitter and then the PowerShell Technet forums. My goal? Try to figure out what objects (classes) are exposed by my newly instantiated WebService proxy. After hacking at a bit, I came up with this (wrong) approach, but, it still showcases some fun usage of -replace and regex with the pipe (|) to allow multiple options to be matched/replaced.
$webservice = New-WebServiceProxy -Uri 'https://www.super.com/secret/classes.asmx' -Namespace WillsWorld -Class IGotClass
This imports the classes exposed by the .asmx page, and, allows you to call from the current session. My initial take faildd to take advantage of the -Namespace and -Class parameters, so, I was fighting ugly, dynamic type names on top of everything. Hint: use these options.

Once I have this loaded I figured I could parse the method signatures, and, extract the objects names. The following command got me a good bit along the way, but, again, still was lacking:
$webservice |
gm -membertype method |
select definition |
% {
      ($_ -replace "@|{|}|Definition=",'') -split ' |\(|\)'
} |
where {$_ -match 'WillsWorld'} |
select -Unique |
sort
Chris Duck, fellow DFW-ite, gave me this alternative which is more to the point and accurate:
 $webservice.GetType().Assembly.GetExportedTypes()
I had played with the Assembly object, but, didn't know about the .GetExportedTypes() method. Thanks to these gentleman, I can now look at a new WebService proxy and know exactly what objects/classes I can use in addition to my methods, properties and events identified by Get-Member.