在 PowerShell 中将 SOAP XML 行转置为列
Transpose SOAP XML rows into columns in PowerShell
我一直在编写一个脚本,我想将以下 SOAP xml 响应元素从行转置到列,但我没有成功找到正确的解决方案。我正在使用 PowerShell 5.1.1.
以下是支持问题证据的 XML 要素:
<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 to HTTP POST 通过 -Body
参数获取设备信息。正文被认为是以下内容:
$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
方法中使用它?
编辑 1:我确实在使用 Invoke-WebRequest 方法时声明了 XML 变量。
以下将网络服务响应中的 <items>
元素转换为单个 [pscustomobject]
实例的属性,使用:
Select-Xml
提取感兴趣的元素
结合一个ForEach-Object
call that builds up a(n ordered) hash table来自感兴趣的子元素
之后转换为 [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]
实例:
device.deviceid device.uri device.longname
--------------- ---------- ---------------
123456789 127.0.0.1 DESKTOP-123ABC456
我一直在编写一个脚本,我想将以下 SOAP xml 响应元素从行转置到列,但我没有成功找到正确的解决方案。我正在使用 PowerShell 5.1.1.
以下是支持问题证据的 XML 要素:
<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 to HTTP POST 通过 -Body
参数获取设备信息。正文被认为是以下内容:
$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
方法中使用它?
编辑 1:我确实在使用 Invoke-WebRequest 方法时声明了 XML 变量。
以下将网络服务响应中的 <items>
元素转换为单个 [pscustomobject]
实例的属性,使用:
Select-Xml
提取感兴趣的元素结合一个
ForEach-Object
call that builds up a(n ordered) hash table来自感兴趣的子元素之后转换为
[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]
实例:
device.deviceid device.uri device.longname
--------------- ---------- ---------------
123456789 127.0.0.1 DESKTOP-123ABC456