在 PowerShell 中将 SOAP XML 行转换为列 [英] Transpose SOAP XML rows into columns in PowerShell
问题描述
我一直在编写一个脚本,我想将以下 SOAP xml 响应元素从行转换为列,但我没有成功找到正确的解决方案.我使用的是 PowerShell 5.1.1.
I've been working on a script that I want to transpose the following SOAP xml response elements from row to columns, but I have not been successful in finding the correct solution for this. I am using PowerShell 5.1.1.
以下是支持问题证据的 XML 元素:
Here are the XML elements to support the question's evidence:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
<deviceListResponse xmlns="http://SERVERURL.domain/">
<return>
<items>
<first>device.deviceid</first>
<second>123456789</second>
<key>device.deviceid</key>
<value>123456789</value>
</items>
<items>
<first>device.uri</first>
<second>127.0.0.1</second>
<key>device.uri</key>
<value>127.0.0.1</value>
</items>
<items>
<first>device.longname</first>
<second>DESKTOP-123ABC456</second>
<key>device.longname</key>
<value>DESKTOP-123ABC456</value>
</items>
...
</return>
</deviceListResponse>
</soap:Body>
</soap:Envelope>
我使用 Invoke-WebRequest 到 HTTP POST 通过 -Body
参数获取设备信息.正文被视为以下内容:
I use a Invoke-WebRequest to HTTP POST to get device information via the -Body
parameter. The body is considered the following:
$body = @"
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:ei2="http://SERVERURL.domain/">
<soap:Header/>
<soap:Body>
<ei2:deviceList>
<ei2:username>example@domain.com</ei2:username>
<ei2:password>$($env:password)</ei2:password>
<ei2:settings>
<ei2:key>customerID</ei2:key>
<ei2:value>123456</ei2:value>
</ei2:settings>
</ei2:deviceList>
</soap:Body>
</soap:Envelope>
"@
如何制作可展示的东西,以便我可以在 PSCustomObject
方法中使用它?
How can I make to something presentable so that I can use it in a PSCustomObject
methods?
编辑 1:我确实在使用 Invoke-WebRequest 方法时声明了 XML 变量.
Edit 1: I do have the XML variable declared when using the Invoke-WebRequest method.
推荐答案
以下内容将 Web 服务响应中的
元素转换为单个 [pscustomobject] 的属性
实例,使用:
The following transforms the <items>
elements in your web-service response into properties of a single [pscustomobject]
instance, using:
Select-Xml代码>
提取感兴趣的元素
Select-Xml
to extract the elements of interest
结合 ForEach-对象
调用构建一个(n 有序)哈希表来自感兴趣的子元素
combined with a ForEach-Object
call that builds up a(n ordered) hash table from the child elements of interest
之后转换为 [pscustomobject]
实例:
# Simplified sample response from the web service.
$xmlText = @'
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body>
<deviceListResponse xmlns="http://SERVERURL.domain/">
<return>
<items>
<first>device.deviceid</first>
<second>123456789</second>
<key>device.deviceid</key>
<value>123456789</value>
</items>
<items>
<first>device.uri</first>
<second>127.0.0.1</second>
<key>device.uri</key>
<value>127.0.0.1</value>
</items>
<items>
<first>device.longname</first>
<second>DESKTOP-123ABC456</second>
<key>device.longname</key>
<value>DESKTOP-123ABC456</value>
</items>
</return>
</deviceListResponse>
</soap:Body>
</soap:Envelope>
'@
# Initialize the ordered hashtable that will collect the key-value pairs
# from the XML
$oht = [ordered] @{}
# Loop over all <items> elements and add their <key> and <value>
# child elements as key-value pairs to the output hashtable.
Select-Xml -Content $xmlText //ns:items -Namespace @{ ns = 'http://SERVERURL.domain/'} |
ForEach-Object {
$oht[$_.Node.key] = $_.Node.value
}
# If necessary, convert the ordered hashtable to a customobject.
$customObj = [pscustomobject] $oht
以上在$customObj
中产生以下[pscustomobject]
实例:
The above yields the following [pscustomobject]
instance in $customObj
:
device.deviceid device.uri device.longname
--------------- ---------- ---------------
123456789 127.0.0.1 DESKTOP-123ABC456
这篇关于在 PowerShell 中将 SOAP XML 行转换为列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!