After posting the last update to my PowerShell weather script, I was looking at the sheer awkwardness of the pre-made SOAP request. Basically, in order to send a SOAP request to the web service, I just kept a ready-made SOAP envelope in a file alongside the script, prepped it with some simple search-and-replace for the parameters, and used
Invoke-WebRequest to POST it.
Here's the old code:
$uri = "http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php" $lat = "40.019444" $lon = "-105.292778" $start = Get-Date -format "yyyy-MM-dd" $body = Get-Content .\ndfd.soap $body = $body.Replace("[lat]", $lat).Replace("[lon]", $lon).Replace("[start]", $start) [xml]$envelope = Invoke-WebRequest $uri -Method post -ContentType "text/xml" -Body $body [xml]$weather = $envelope.Envelope.Body.NDFDgenByDayResponse.dwmlByDayOut.'#text'
It's awkward and very lazy, but it works. Still, it was bothering me. After a little more research, it turns out that if you've got a WSDL, you can just generate a proxy straight out of PowerShell (much the same way you'd generate proxy classes to work with a SOAP service from Visual Studio). The cmdlet you need is
New-WebServiceProxy (naturally). Here's the updated code using
New-WebServiceProxy and dumping the pre-made SOAP request:
$uri = "http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl" $lat = "40.0269" $lon = "-105.251"Po $start = Get-Date -format "yyyy-MM-dd" $Proxy = New-WebServiceProxy -uri $URI -namespace WebServiceProxy [xml]$weather = $Proxy.NDFDgenByDay($lat, $lon, $start, 7, "e", "Item24hourly")
Just generate the proxy and call the method. Notice that this also means I get to drop the awkward line of code which extracted the XML I cared about from the return SOAP envelope. Much cleaner.