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.
$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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With