I'm writing a powershell script where i need to get some value of a node based on the parents' info (attribute names, id's..)
Example:
<?xml version="1.0" encoding="UTF-8"?>
<GrandParent>
<Parent id="1">
<foo></foo>
<bar></bar>
<buzz></buzz>
<Child Name="correct">
<Content>Value1</Content>
</Child>
<Child Name="wrong">
<Content>Value2</Content>
</Child>
<Child Name="wrong">
<Content>Value3</Content>
</Child>
</Parent>
<Parent id="2">
<foo></foo>
<bar></bar>
<buzz></buzz>
<Child Name="correct">
<Content>Value4</Content>
</Child>
<Child Name="wrong">
<Content>Value5</Content>
</Child>
<Child Name="wrong">
<Content>Value6</Content>
</Child>
</Parent>
<Parent id="3">
<foo></foo>
<bar></bar>
<buzz></buzz>
<Child Name="correct">
<Content>Value7</Content>
</Child>
<Child Name="wrong">
<Content>Value8</Content>
</Child>
<Child Name="wrong">
<Content>Value9</Content>
</Child>
</Parent>
</GrandParent>
I want to get some output like: Parent 1: value1 Parent 2: value4 Parent 3: value7
Explanation: for each Parent node i want to look at the 'Child' node with the 'Correct' name and get the value of his child
I'm looping with ForEach-Object inside a ForEach-Object, but my context '$_' always refers to the entire xml instead of the current scope:
$total_xml.GrandParent.Parent | ForEach-Oject {
$_.Parent.Child | ForEach-Object{
$_ #This refers back to the total_xml
}
}
PS: I'm a newbie in Powershell scripting :(
Can someone help me out with a simple script for this?
-PipelineVariablecommon parameter. You're also looking at the wrong thing. Your firstforeachshould be$_.Childbecause$_is referring to theParentnode and then your secondforeachwill be pointing at eachChildnode inside thatParent. - Maximilian Burszley