在Powershell中解析XML http发布响应中的单个字段,而无需创建新文件

时间:2018-07-17 18:19:49

标签: xml powershell http post

我正在尝试通过Powershell脚本从http发布响应中获取特定字段值,以便可以在脚本本身中将其用作变量。有没有一种方法可以将响应写到新文件中

代码如下:

$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;

我正在做的是创建票证,我的目标是使用创建票证时返回的值更新该票证。

PowerShell的响应是一个巨大的XML块,我需要的字段看起来像<id>12345</id>

是否有一种方法可以解析该字段并在同一脚本中使用它,而无需将响应导出到新文件中?

1 个答案:

答案 0 :(得分:0)

尝试以下操作:

# 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

请注意,使用PowerShell自动将XML文档的DOM适配到嵌套对象中,该对象使您可以使用点表示法钻取到文档中,这非常方便,并且在这里您可以绕开处理文档的XML名称空间的复杂性。 /> 但是,它有其局限性和陷阱。有关更多信息,请参见我的this answer的最后一部分。