4

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.

1 Answer 1

6

$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
Sign up to request clarification or add additional context in comments.

4 Comments

Ansgar, Thank you so much for your help. This works fine now.
this not work for me for some reason, I get error saying the specified node cannot be inserted as the valid child of this node, because the specified node is the wrong type - any advice?
@GorodeckijDimitrij The code I posted doesn't insert anything anywhere. Please post a new question with your code, sample input, and the actual error message.
@AnsgarWiechers I use exactly your code, provide error that return by powershell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.