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 = <span class="str">"https://path/to/Service.asmx"</span>
$parameters = <span class="str">'&lt;?xml version="1.0" encoding="utf-8"?&gt;'</span> + <span class="str">"`n"</span> 
   + <span class="str">'&lt;soap12:Envelope...&gt;<em>(your request body here)</em>&lt;/soap12:Envelope&gt;'</span>

$http_request = New-Object -ComObject Msxml2.XMLHTTP
$<span class="str">'POST'</span>, $url, $<span class="kwrd">false</span>)
$http_request.setRequestHeader(<span class="str">"Content-type"</span>, <span class="str">"application/soap+xml"</span>)
$http_request.setRequestHeader(<span class="str">"Content-length"</span>, $parameters.length)
$http_request.setRequestHeader(<span class="str">"Connection"</span>, <span class="str">"close"</span>)


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:


This entry was posted in PowerShell, REST. Bookmark the permalink.
  • Gregory M Lancaster

    I am a noobie with web services and powershell. How would I handle authentication to web service using powershell

  • Pingback: XML Broker ‘Ping’ | Desktops & Apps()

  • Alen

    Thanks for leaving this up. This was exactly what I needed.