Call a web service with PowerShell

I recently used a PowerShell script like the following to troubleshoot the details of a third-party web service our code was using. I thought the script was something worth noting here for future reference.

$url = "https://path/to/Service.asmx"
$parameters = '<?xml version="1.0" encoding="utf-8"?>' + "`n"
   + '<soap12:Envelope...>(your request body here)</soap12:Envelope>'

$http_request = New-Object -ComObject Msxml2.XMLHTTP
$http_request.open('POST', $url, $false)
$http_request.setRequestHeader("Content-type", "application/soap+xml")
$http_request.setRequestHeader("Content-length", $parameters.length)
$http_request.setRequestHeader("Connection", "close")
$http_request.send($parameters)
$http_request.statusText
$http_request.responseText

The (your request body here) part can be obtained easily if the service you're talking to is running on .NET with the metadata information turned on. If you point your regular web browser at the URL in this case, it will return a list of operations the service supports. Clicking on any operation will show you what to put in for $parameters – something like this – very handy:

image

Comments !

links

social