在PowerShell中将SOAP XML行转换为列
我一直在编写一个脚本,希望将以下SOAP xml响应元素从行转换到列,但我没有成功地找到正确的解决方案。我正在使用PowerShell 5.1.1 以下是支持问题证据的XML元素:在PowerShell中将SOAP XML行转换为列,xml,powershell,soap,Xml,Powershell,Soap,我一直在编写一个脚本,希望将以下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.d
<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>
我怎样才能使某个东西看起来像样,以便在PSCustomObject
方法中使用它
编辑1:在使用Invoke WebRequest方法时,我确实声明了XML变量。下面使用以下命令将web服务响应中的
元素转换为单个[pscustomobject]
实例的属性:
- 提取感兴趣的元素
- 结合一个调用,该调用从感兴趣的子元素中建立一个(n顺序的)
- 之后将其转换为
实例:[pscustomobject]
#来自web服务的简化示例响应。
$xmlText=@'
device.deviceid
123456789
device.deviceid
123456789
device.uri
127.0.0.1
device.uri
127.0.0.1
device.longname
桌面-123ABC456
device.longname
桌面-123ABC456
'@
#初始化将收集键值对的有序哈希表
#从XML
$oht=[有序]@{}
#循环所有元素并添加它们的and
#子元素作为输出哈希表的键值对。
选择Xml-Content$xmlText//ns:items-Namespace@{ns='1]http://SERVERURL.domain/'} |
ForEach对象{
$oht[$\.Node.key]=$\.Node.value
}
#如有必要,将有序哈希表转换为customobject。
$customObj=[pscustomobject]$oht
上面在$customObj
中生成以下[pscustomobject]
实例:
device.deviceid device.uri device.longname
--------------- ---------- ---------------
123456789 127.0.0.1桌面-123ABC456
我的回答中有什么东西没有达到预期效果吗?如果有,请提供反馈;悄悄地拒绝不会解决问题。@mklement0在意识到我遗漏了一个我应该披露的关键细节后,我在问题中添加了更多细节。如果我不够清楚,不能以更详细的方式提出问题,我对此表示歉意。我已经看过你的更新,但没有让我更清楚。IDs是如何发挥作用的?一般来说,在给出答案后,请不要扩大问题的范围或更改要求,因为这可能会使后者无效,并肯定会导致混淆。相反,请提出一个新问题,重点是扩展/更改的需求。恢复到原始版本,我将发布一个新问题,其中包含更新的需求。
$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>
"@