4

Referencing the help file for `Invoke-RestMethod:

PS /home/nicholas> PS /home/nicholas> $response = Invoke-RestMethod -Uri https://blogs.msdn.microsoft.com/powershell/feed/ PS /home/nicholas> PS /home/nicholas> $json = $response | ConvertTo-Json WARNING: Resulting JSON is truncated as serialization has exceeded the set depth of 2. PS /home/nicholas> PS /home/nicholas> $xml = $response | ConvertTo-Xml PS /home/nicholas> PS /home/nicholas> $xml.OuterXml 

How can I convert the response to xml and output it, as above, in a single line?

3 Answers 3

5

The specific URI you're targeting returns XML content, which Invoke-RestMethod automatically parses into XML DOMs (document object models) of type System.Xml.XmlElement (an array of such instances in this case).

A simple way to visualize the output is to access the .OuterXml property:

(Invoke-RestMethod -Uri https://blogs.msdn.microsoft.com/powershell/feed/).OuterXml 

If you really need a single-line representation:

(Invoke-RestMethod -Uri https://blogs.msdn.microsoft.com/powershell/feed/).OuterXml -replace '\r?\n' 

You can work with these XmlElement instances using OOP techniques, relying on PowerShell's convenient adaptation of the XML DOM; for instance, the following extracts the value of the <title> child element from all array elements, using member-access enumeration:

$entries = Invoke-RestMethod -Uri https://blogs.msdn.microsoft.com/powershell/feed/ $entries.title 

Note:

  • Because the target URI represents an Atom feed, the feed entries are returned as an array of XML elements rather than as a single XML document.

  • It is more common for web services to return XML that is parsed into a single XML document, as a [xml] (System.Xml.XmlDocument) instance.

Sign up to request clarification or add additional context in comments.

Comments

0

Just a slight variant:

PS /home/nicholas> PS /home/nicholas> $url="https://blogs.msdn.microsoft.com/powershell/feed/" PS /home/nicholas> PS /home/nicholas> Invoke-RestMethod -Method Post -Uri $url -Body $body -ContentType 'application/xml' 

which, despite a specified "ContentType" certainly seems to not be XML at least as printed to the console. Still, interesting.

2 Comments

What you're seeing is PowerShell's default formatting of the System.Xml.XmlElement objects it has automatically parsed the XML response text into.
Also, in your call -ContentType 'application/xml' is effectively ignored, because it relates only to the request data being sent; it doesn't request the response format.
0

[xml]$testResult = (Invoke-RestMethod -Uri 'url').OuterXml $testResult.Details.Status

In case xml is 3

1 Comment

Please format your post properly. Also, [xml]$testResult = (Invoke-RestMethod -Uri 'url').OuterXml is unnecessary - $testResult = Invoke-RestMethod -Uri 'url' will do, if the response is XML text, because PowerShell automatically parses it into an [xml] instance. I assume that $testResult.Details.Status is just an example of how to drill down into the [xml] instance, but I suggest you point that out in the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.