繁体   English   中英

使用powershell比较节点xml文件的一部分

[英]Compare part of node xml files using powershell

我想比较两个文件(来自 GPOReport 的 GPO->Computer)的 xml 树的一部分。

$Xml1 = Get-Object $Path1
$Xml2 = Get-Object $Path2

Compare-Object ($Xml1) -DifferenceObject ($Xml2)

给出与文件的完全差异。 但我只想要节点的一部分。

比较树(即XML文档)要比比较两个列表(如两个文件中的行)要复杂一些。

从您的问题尚不清楚,您在这里寻找的比较范围有多广泛,但是如果您只想测试两个XML文档之间的单个值或属性,最简单的方法可能是使用Select-Xml

$xml1 = [xml]@'
<root>
  <nodegroup>
    <node name="myNode">SomeValue</node>
  </nodegroup>
</root>
'@
$xml2 = [xml]@'
<root>
  <nodegroup>
    <node name="myNode">SomeOtherValue</node>
  </nodegroup>
</root>
'@

$res1,$res2 = $xml1,$xml2 |Select-Xml -XPath 'root/nodegroup/node[@name = "myNode"]'

if($res1.Node.InnerText -eq $res2.Node.InnerText){
    # the inner text value of the selected node is the same in both documents
}
$xml1 = Get-Content -Path "D:\gpo-test\gpo-original.xml"
$xml2 = Get-Content -Path "D:\gpo-test\gpo.xml"

#$XmlActive.GetType().FullName
$res1,$res2 = $xml1,$xml2 |Select-Xml -XPath '//gpo/computer'

if($res1.Node.InnerText -eq $res2.Node.InnerText){
    # the inner text value of the selected node is the same in both documents
}

xml结构是这样的:

<GPO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.microsoft.com/GroupPolicy/Settings">
  <Computer>
   ...
  </Computer>

我得到错误

Select-Xml : Cannot validate argument on parameter 'Content'. The argument is null or empty. Provide an argument that
is not null or empty, and then try the command again.
At D:\gpo-test\compare.ps1:15 char:28
+ $res1,$res2 = $xml1,$xml2 |Select-Xml -XPath '//gpo/computer'
+                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (System.Object[]:Object[]) [Select-Xml], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.SelectXmlCommand

我想要完整的树,并将其与另一个文件中的树进行比较。

暂无
暂无

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

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