xml - 比较两个 XML 文件 - 输出唯一

标签 xml powershell compare

我有两个格式相同的 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/

相关文章:

java - 获取 xs :choice maxOccurs and minOccurs attributes with XSOM

java - 如何为我用 Java 编写的 XML 解析器编写单元测试

java - 处理混合内容的 XPath

powershell - 根据用户输入加载脚本

powershell - 在 PowerShell 中连接到 ForEach-Object

c# - 比较同一列表中的多个项目

python - 在python中,比较元素中的两个数组并将较低的值更改为零

java - jasper 报告 - 如何向条形码数字输出添加星号?

sql - Microsoft.SqlServer.BatchParser 问题

sql-server - 在两个表的同一列上查找缺失值