PowerShell: call XML REST service and print it in

2019-08-26 22:40发布

问题:

I have a call to a REST service through PowerShell.

What the query return is in XML and I need to have it printable in the terminal in a readable manner.

The call is easy:

$Url = "http://api.wunderground.com/auto/wui/geo/GeoLookupXML/index.xml?query=94107"

Invoke-RestMethod -Method 'GET' -Uri $url 

the result is horrible:

As you can see it's more than readable on Postman but not on the terminal.

Is there a way to print the output in a readable manner on the terminal?

I need to indent all that <?xml version="1.0" encoding="UTF-8"?>, I want to see it printed on the terminal.

回答1:

$Url = "http://api.wunderground.com/auto/wui/geo/GeoLookupXML/index.xml?query=94107"
$rest = Invoke-RestMethod -Method 'GET' -Uri $url
$rest.location     #-> readable

# or

$rest.InnerXml    #-> like postman output