Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Parsing from a response of Invoke-WebRequest

Tags:

powershell

xml

I am trying to parse a specific value from a response to an Invoke-WebRequest I have made in a PowerShell script, but I am not able to get it.

If I used postman for example to get the content I would get the below:

<?xml version="1.0" encoding="UTF-8"?>
<globalInfo>
    <currentLoggedInUser>admin</currentLoggedInUser>
    <versionInfo>
        <majorVersion>6</majorVersion>
        <minorVersion>2</minorVersion>
        <patchVersion>4</patchVersion>
        <buildNumber>4292526</buildNumber>  <!-- this is what I need -->
    </versionInfo>
</globalInfo>

Below is the command I am using within the script and I get nothing:

$r = Invoke-WebRequest -Uri "$uriP/api/1.0/appliance-management/global/info" -Body $body -Method:Get -Headers $head -ContentType "application/xml" -ErrorAction:Stop -TimeoutSec 60
$bn = ($r.Content.globalInfo.versionInfo.buildNumber)

Also, if I used $bn = ($r.Content) I will get the full content like below:

<?xml version="1.0" encoding="UTF-8"?>
<globalInfo><currentLoggedInUser>admin</currentLoggedInUser><versionInfo><majorVersion>6</majorVersion><minorVersion>2</minorVersion><patchVersion>4</patch
Version><buildNumber>4292526</buildNumber></versionInfo></globalInfo>

I can see the format of the response from the postman is different from the one I get in PowerShell using only ($r.Content), but I am not sure what is the issue here.

like image 754
Taher Shaker Avatar asked Sep 02 '25 09:09

Taher Shaker


1 Answers

$r.Content is a string. You need to actually parse the XML in it before you can access the individual nodes:

$bn = ([xml]$r.Content).globalInfo.versionInfo.buildNumber
like image 93
Ansgar Wiechers Avatar answered Sep 05 '25 00:09

Ansgar Wiechers