简体   繁体   English

比较两个 XML 文件 - output 唯一

[英]Compare two XML files - output unique

I have two xml files with the same format, I can get the output in powershell to display each file correctly but I need to compare StatusErrorResultsFiltered.xml to StatusErrorResults.xml and output the unique entries that exist in one but not both files,excluding the "description" field.我有两个格式相同的 xml 文件,我可以在 powershell 中获取 output 以正确显示每个文件,但我需要比较 StatusErrorResultsFiltered.xml 和 StatusErrorResults.xml 和 output “描述”字段。

Current Code:当前代码:

$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"

Currently the undefinedErrors.xml is showing one node with Device and Mstatus but does not actually include the data that should be there.目前 undefinedErrors.xml 显示了一个具有 Device 和 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 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 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>

Since you're dealing with CLIXML files - ie XML-based files that contain .NET objects serialized by PowerShell - you can use Import-Clixml to import them, which makes comparing the resulting objects with Compare-Object easier:由于您正在处理 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"

Note the need to remove the SideIndicator property from the output objects, which Compare-Object invariably adds to the output objects.请注意,需要从 output 对象中删除SideIndicator属性, Compare-Object总是将其添加到 output 对象中。

Use Import-CliXMl, it will convert the XML to PS Objects and then you can can work on the unique part, then again Export-CliXml使用 Import-CliXMl,它将 XML 转换为 PS 对象,然后您可以处理独特的部分,然后再次使用 Export-CliXml

$unitResults   = Import-Clixml "$XMLPath/StatusErrorResults.xml"
$filteredResults  = $(Import-Clixml "$XMLPath/Output/StatusErrorResultsFiltered.xml" | Select-Object -Property "Device", "Mstatus")

$unitResults += $filteredResults
$endResult = $unitResults | Sort-Object -Property Device,Mstatus -Unique
$endResult | Export-Clixml -Path "$XMLPath\Output\UndefinedErrors.xml"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM