Francesco Mantovani
Francesco Mantovani

Reputation: 12377

PowerShell: call REST service and write to host

@Nas was very helpful to write an answer for me here but unfortunately I cannot query my server and write to the terminal the XML response of my call:

$body = @{
 "UserSessionId"="12345678"
 "OptionalEmail"="MyEmail"
} | ConvertTo-Json

$header = @{
 "Accept"="application/json"
 "connectapitoken"="97fe6ab5b1a640909551e36a071ce9ed"
 "Content-Type"="application/json"
} 

$Url = "http://MyServer/WSVistaWebClient/RESTLoyalty.svc/member/search"

$XMLObject = Invoke-RestMethod -Method 'POST' -Uri $url -Headers $header -Body $body     

Write-Host "First try"
$XMLObject.location     #-> readable

Write-Host "Second try"
$XMLObject.InnerXml    #-> like postman output

Write-Host "Third try"
$XMLObject

enter image description here

not sure why I can print the raw XML that I receive in response to the call but I cannot format it.

But the stream of data is infidelity there

Upvotes: 0

Views: 1542

Answers (2)

postanote
postanote

Reputation: 16116

ArcSet has given you an succinct answer, for the node level you say you are trying to reference, but FYI, this is all about basic parsing XML using PowerShell.

PS has XML cmdlets ...

Get-Command -Name '*xml*' | Format-Table -AutoSize

CommandType Name                      Version Source                      
----------- ----                      ------- ------                      
...          
Cmdlet      ConvertTo-Xml             3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet      Convert-XMLtoJSON         5.0.0.1 Sorlov.PowerShell           
Cmdlet      Export-Clixml             3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet      Import-Clixml             3.1.0.0 Microsoft.PowerShell.Utility
Cmdlet      Merge-XMLFile             5.0.0.1 Sorlov.PowerShell           
Cmdlet      New-XSDfromXML            5.0.0.1 Sorlov.PowerShell           
Cmdlet      Select-Xml                3.1.0.0 Microsoft.PowerShell.Utility

... specifically for this or you can use .NET xml namespace to parse it. There is a ton of docs, eBooks and videos on this topic. XML and JSON are a very big deal in PS.

PowerShell Simply Put: Parsing Through XML

Mastering everyday XML tasks in PowerShell

For example, using the .Net xml namespace, change this ...

$XMLObject = Invoke-RestMethod -Method 'POST' -Uri $url -Headers $header -Body $body

... to say this...

[xml]$XMLObject = Invoke-RestMethod -Method 'POST' -Uri $url -Headers $header -Body $body 

... Then parse as needed.

$XMLObject.Object

For Example (since there is no way for me to use what you actually have):

# download currency exchange rates in XML format and parse for currency rates:

$url = 'http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml'
[xml]$result = (Invoke-WebRequest -Uri $url -UseBasicParsing).Content

$result

xml                            Envelope
---                            --------
version="1.0" encoding="UTF-8" Envelope


$result.Envelope

gesmes  : http://www.gesmes.org/xml/2002-08-01
xmlns   : http://www.ecb.int/vocabulary/2002-08-01/eurofxref
subject : Reference rates
Sender  : Sender
Cube    : Cube


$result.Envelope.Cube

Cube
----
Cube


$result.Envelope.Cube.Cube

time       Cube                                                                                                                                                                                      
----       ----                                                                                                                                                                                      
2018-09-28 {Cube, Cube, ...



$result.Envelope.Cube.Cube.Cube

currency rate    
-------- ----    
USD      1.1576  
JPY      131.23  
BGN      1.9558  
...

Upvotes: 1

ArcSet
ArcSet

Reputation: 6860

Looks like the property you want to get is LoyaltyXML.

$XMLObject.LoyaltyXML

Upvotes: 1

Related Questions