Learn about Windows PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use a text file when making a SOAP request via Windows PowerShell 3.0.
Microsoft Scripting Guy, Ed Wilson, is here. Today, I spent the day playing around with Windows PowerShell 3.0. It is a lot of fun. One of the things I enjoy doing is working with web services. Windows PowerShell 2.0 vastly simplified the code that is required to call web services from within a script, and Windows PowerShell 3.0 makes it even easier.
Note For a great introduction to using Windows PowerShell with web services, refer to Hey, Scripting Guy! How Can I Use Web Services? That blog continues with Hey, Scripting Guy! How Can I Use a Web Service to Find Weather for a Specific City? For another example of using web services, you may want to read Hey, Scripting Guy! How Can I Quickly Check Stocks with Windows PowerShell? For more fun working with web services, read Weekend Scripter: Download lyrics for Your Favorite Song with PowerShell.
So, while playing around with Windows PowerShell today, I decide that because I have written several blogs this week using Invoke-WebRequest, I may as well continue with that. It just sort of turned out that way. In this particular instance, I did not plan to have a weeks’ worth of blogs using the Invoke-WebRequest cmdlet, but as I began writing, it seemed like a lot of fun, so I continued. The remainder of the blogs that I had originally planned for this week got shoved forward by a couple of weeks (as the weeks unfold, you will see why).
I decided to play around with the SunSetRiseService web service. It exposes a method called GetSunSetRiseTime that requires a LatLonDate object (in Windows PowerShell terms) as the argument for the method. Unfortunately, it does not really understand Windows PowerShell objects, and therefore creating a LatLonDate object inside the script itself is a bit problematic. To see the contract, I pasted the url to the WSDL into Internet Explorer and examined the following page that displayed.
To create the LatLonDate object for the web service SOAP request, I decided to create a SOAP Envelope to simplify creating the LatLonDate object. I put the SOAP envelope in a text file called LatLonDate.txt. I need to supply the latitude, longitude, time zone, day, month, and year to obtain the sunset information. The file is shown here with the appropriate values entered. (It would not take too much of a change to create this file on the fly based on parameters supplied to a function. In fact, the file could be created as a temporary file in the temporary directory and deleted following the completion of the web request.)
The complete script is only two lines. The first line stores the URI to the WSDL, and the second line uses the Invoke-WebRequest cmdlet to call the Post method. The web response is stored in XML format. This command is shown here.
$uri = "http://www.webservicex.net/sunsetriseservice.asmx?WSDL"
[xml]$sun = Invoke-WebRequest $uri -Method post -ContentType "text/xml" `
When I began to examine the object stored in the $sun variable, I found an XML document with many layers. This is shown here.
PS C:\> $sun.Envelope.Body
PS C:\> $sun.Envelope.Body.GetSunSetRiseTimeResponse
PS C:\> $sun.Envelope.Body.GetSunSetRiseTimeResponse.GetSunSetRiseTimeResult
Latitude : 35.13
Longitude : 80.5
SunSetTime : 7.03526
SunRiseTime : -5.677144
TimeZone : -6
Day : 23
Month : 9
Year : 2012
Because I am only interested in the sunset time, I call it directly as shown here.
The complete Get-SunSetTime script is shown here.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
Unreal! Just started working on sending SOAP request from POSH today now this.... Yippie!
@Ericcourville WOOHOO! Hopefully I can save you a bit of time as you work on your script. Thanks for the feedback.