I want to compare a part of the xml-tree of two files (GPO->Computer from a GPOReport).
$Xml1 = Get-Object $Path1
$Xml2 = Get-Object $Path2
Compare-Object ($Xml1) -DifferenceObject ($Xml2)
gives the complete differences from the file. But I want only part of the node.
Comparing trees (ie. an XML document) is a bit more complicated than comparing two lists (like the lines in two files).
It's unclear from your question how extensive a comparison you're looking for here, but if you're only interested in testing a single value or attribute between two XML documents, the easiest is probably with 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
}
The xml strcuture is this:
<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>
I get the error
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
I want the complete Tree under and compare it with the one in the other file.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.