1

I'm trying to get a specific field value from a http post response via a powershell script so i can use it as a variable in the script itself. Is there a way to do this without writing the response to a new file

The code looks like this:

$HEADERS

$RequestBody = @"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Header>
    <AutotaskIntegrations xmlns="http://autotask.net/ATWS/v1_5/">
    </AutotaskIntegrations>
  </soap:Header>
  <soap:Body>
   Create Ticket Function for API I'm using
  </soap:Body>
</soap:Envelope>
"@

Invoke-WebRequest -Uri $AUTOTASK -Method Post -Body $RequestBody -ContentType $ContentType -UseBasicParsing -Headers $Headers;

$UpdateBody = @"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AutotaskIntegrations xmlns="http://autotask.net/ATWS/v1_5/">
    </AutotaskIntegrations>
  </soap:Header>
  <soap:Body>
    <update xmlns="http://autotask.net/ATWS/v1_5/">
      <Entities>
        <Entity xsi:type="Ticket" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <id>field i need to populate</id>
      </Entities>
    </update>
  </soap:Body>
</soap:Envelope>
"@

Invoke-WebRequest -Uri $AUTOTASK -Method Post -Body $UpdateBody -ContentType $ContentType -UseBasicParsing -Headers $Headers;

What I'm doing is creating a ticket, and my goal is to update that ticket with a value that is returned from the creation of the ticket.

The response from PowerShell is a giant block of XML and the field I need from it looks something like <id>12345</id>.

Is there a way to parse that one field and use it in the same script without exporting the response to a new file?

mklement0
  • 382,024
  • 64
  • 607
  • 775
Mr Bees
  • 11
  • 3
  • Please update the question with a (simplified) example of the response XML. Note that `12345` is not a valid XML element name. Is the response really malformed? – mklement0 Jul 17 '18 at 19:53
  • Sorry its not actually called 12345 thats just the value of it the field in question would be called – Mr Bees Jul 17 '18 at 20:25
  • I now see that your Markdown source contained `12345`, but without enclosing that in `\`...\``, the tag names didn't show. I've fixed that for you. In general, please add any clarifications _directly to the question_, not in comments. Also, to respond to a specific user and have them _notified_ of that response, `@`-mention them as part of the comment; e.g., @mklement0 – mklement0 Jul 17 '18 at 21:14
  • Also, please don't ask the same question multiple times. I've closed this one as a duplicate. – mklement0 Jul 17 '18 at 21:31

1 Answers1

0

Try the following:

# Sample response from the Invoke-WebRequest that created the object:
#   $response = (Invoke-WebRequest -Uri $AUTOTASK -Method Post -Body $RequestBody -ContentType $ContentType -UseBasicParsing -Headers $Headers).ResponseXml
$response = @'
<?xml version="1.0"?>
<soap:response xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:body>
        <id>12345</id>
    </soap:body>
</soap:response>
'@

# The body for the 2nd Invoke-WebRequest call that updates the new object:
# '???' indicates where the new object's ID must be placed.
$UpdateBody = @"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <AutotaskIntegrations xmlns="http://autotask.net/ATWS/v1_5/">
    </AutotaskIntegrations>
  </soap:Header>
  <soap:Body>
    <update xmlns="http://autotask.net/ATWS/v1_5/">
      <Entities>
        <Entity xsi:type="Ticket" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <id>???</id>
        </Entity>
      </Entities>
    </update>
  </soap:Body>
</soap:Envelope>
"@                                                                                                    #"

# Parse the response and the update-request body XML into XML documents
$docResponse = [xml] $response
$docUpdateBody = [xml] $UpdateBody

# Extract the <id> element's text (child node) from the response.
# Note the use of regular dot notation, which is enabled by PowerShell's
# mapping of the XML DOM onto the document object's (nested) properties.
# Note that the namespace qualifier (prefix, i.e., "soap:") is NOT used.
$newId = $docResponse.response.body.id


# Update the update-request body with the new ID, again using dot notation.
$docUpdateBody.Envelope.Body.update.Entities.Entity.id = $newId

# Now make the call to update the object, using the .OuterXml property
# to convert the XML document object back into as string.
Invoke-WebRequest -Uri $AUTOTASK -Method Post -Body $docUpdateBody.OuterXml -ContentType $ContentType -UseBasicParsing -Headers $Headers

Note that using PowerShell's automatic adapting of an XML document's DOM into a nested object that enables drilling into a document with dot notation is very convenient and here allows you to bypass the complexity of dealing with your documents' XML name spaces.
However, it has its limitations and pitfalls; for more, see the last section of this answer of mine.

mklement0
  • 382,024
  • 64
  • 607
  • 775