我有两个格式相同的 xml 文件,我可以在 powershell 中获取输出以正确显示每个文件,但我需要将 StatusErrorResultsFiltered.xml 与 StatusErrorResults.xml 进行比较,并输出一个文件中存在的唯一条目,而不是两个文件中存在的唯一条目,不包括“描述”字段。
当前代码:
$XMLPath = "C:\Users\John.Doe\Desktop\Projects\Powershell\backend library conversion\TEST\"
# Exporting the unique results as undefined errors to an XML file
$filteredResults = [xml](Get-Content "$XMLPath/Output/StatusErrorResultsFiltered.xml")
$unitResults = [xml](Get-Content "$XMLPath/StatusErrorResults.xml")
$originalEntries = $unitResults.Objs.obj.ms.s | Where-Object {$_.N -eq "Device" -or $_.N -eq "Mstatus"}
$filteredEntries = $filteredResults.Objs.obj.ms.s | Where-Object {$_.N -eq "Device" -or $_.N -eq "Mstatus"}
# Using Compare-Object to find unique entries
$uniqueEntries = Compare-Object $originalEntries $filteredEntries -Property Device,Mstatus |
Select-Object -Property Device,Mstatus |
Sort-Object -unique -Property Device,Mstatus
$uniqueEntries | Export-Clixml -Path "$XMLPath\Output\UndefinedErrors.xml"
当前,undefinedErrors.xml 显示一个带有设备和 Mstatus 的节点,但实际上并未包含应有的数据。
<Objs xmlns="http://schemas.microsoft.com/powershell/2004/04" Version="1.1.0.1">
<Obj RefId="0">
<TN RefId="0">
<T>Selected.System.Management.Automation.PSCustomObject</T>
<T>System.Management.Automation.PSCustomObject</T>
<T>System.Object</T>
</TN>
<MS>
<Nil N="Device"/>
<Nil N="Mstatus"/>
</MS>
</Obj>
</Objs>
StatusErrorResultsFiltered.xml
<Objs xmlns="http://schemas.microsoft.com/powershell/2004/04" Version="1.1.0.1">
<Obj RefId="0">
<TN RefId="0">
<T>System.Management.Automation.PSCustomObject</T>
<T>System.Object</T>
</TN>
<MS>
<S N="Device">DeviceName5</S>
<S N="Mstatus">5</S>
<S N="Description">Generic Text for example</S>
</MS>
</Obj>
<Obj RefId="1">
<TNRef RefId="0"/>
<MS>
<S N="Device">DeviceName4</S>
<S N="Mstatus">38</S>
<S N="Description">Generic Text for example</S>
</MS>
</Obj>
<Obj RefId="2">
<TNRef RefId="0"/>
<MS>
<S N="Device">DeviceName3</S>
<S N="Mstatus">18</S>
<S N="Description">Generic Text for example</S>
</MS>
</Obj>
<Obj RefId="3">
<TNRef RefId="0"/>
<MS>
<S N="Device">DeviceName2</S>
<S N="Mstatus">16</S>
<S N="Description">Generic Text for example</S>
</MS>
</Obj>
<Obj RefId="4">
<TNRef RefId="0"/>
<MS>
<S N="Device">DeviceName1</S>
<S N="Mstatus">49</S>
<S N="Description">Generic Text for example</S>
</MS>
</Obj>
</Objs>
StatusErrorResults.xml
<Objs xmlns="http://schemas.microsoft.com/powershell/2004/04" Version="1.1.0.1">
<Obj RefId="0">
<TN RefId="0">
<T>System.Management.Automation.PSCustomObject</T>
<T>System.Object</T>
</TN>
<MS>
<S N="Device">DeviceName5</S>
<S N="Mstatus">5</S>
</MS>
</Obj>
<Obj RefId="1">
<TNRef RefId="0"/>
<MS>
<S N="Device">DeviceName4</S>
<S N="Mstatus">38</S>
</MS>
</Obj>
<Obj RefId="2">
<TNRef RefId="0"/>
<MS>
<S N="Device">DeviceName3</S>
<S N="Mstatus">18</S>
</MS>
</Obj>
<Obj RefId="3">
<TNRef RefId="0"/>
<MS>
<S N="Device">DeviceName2</S>
<S N="Mstatus">16</S>
</MS>
</Obj>
<Obj RefId="4">
<TNRef RefId="0"/>
<MS>
<S N="Device">DeviceName1</S>
<S N="Mstatus">49</S>
</MS>
</Obj>
<Obj RefId="5">
<TNRef RefId="0"/>
<MS>
<S N="Device">DeviceName6</S>
<S N="Mstatus">16</S>
</MS>
</Obj>
<Obj RefId="6">
<TNRef RefId="0"/>
<MS>
<S N="Device">DeviceName7</S>
<S N="Mstatus">49</S>
</MS>
</Obj>
</Objs>
最佳答案
由于您正在处理 CLIXML 文件 - 即包含由 PowerShell 序列化的 .NET 对象的基于 XML 的文件 - 您可以使用 Import-Clixml
导入它们,这使得将结果对象与 Compare-Object
进行比较更容易:
$XMLPath = 'C:\Users\John.Doe\Desktop\Projects\Powershell\backend library conversion\TEST'
$filteredFile = "$XMLPath/Output/StatusErrorResultsFiltered.xml"
$resultFile = "$XMLPath/StatusErrorResults.xml"
Compare-Object (Import-Clixml $filteredFile) (Import-Clixml $resultFile) -Property Device, MStatus |
Select-Object * -ExcludeProperty SideIndicator |
Sort-Object -Unique -Property Device, Mstatus |
Export-Clixml "$XMLPath\Output\UndefinedErrors.xml"
请注意,需要从输出对象中删除 SideIndicator
属性,该 Compare-Object
总是会添加到输出对象中。
关于xml - 比较两个 XML 文件 - 输出唯一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75234610/