I have xml that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Physicals>
<Physical>
<Model>6900</Model>
<DefaultId>19</DefaultId>
<IDs>
<DID Size="6x8" ID="19" />
</IDs>
</Physical>
</Physicals>
<\Configuration>
There's repeated Physical tags, within the Physicals tag.
I'm trying to use powershell to match the DefaultID and obtain the size in the DID tag. How can I do that? This is what I have so far.
[xml]$xml = [xml](Get-Content C:\Users\me\configured.xml)
$Physicals = $xml.SelectNodes('//Physicals/Physical')
foreach($p in $Physicals)
{
$p.Model
$id = $p.DefaultId
if($p.IDs.DID.ID=$id) #error here
{
$p.IDs.DID.Size
}
}
It has the error The property ID cannot be found on this object. I just don't know how to use xml notation and powershell to match that ID and return the size. There can be multiple id's and sizes listed in that section, but only one matching the default.
I searched online, and what I'm seeing isn't close enough to what I'm doing.
xml and powrshell foreach powershell xml nodes by attribute name
Update:
@mklement pointed out my error in using = instead of -eq (thank you):
if($p.IDs.DID.ID -eq $id)
{
$p.IDs.DID.Size
}
Running this, it returns the following:
6900
6x8
dl2100
A4
LETTER
8810
8x36
8x30
8x12
8x10
But I only have one default per Model.
There's a 2100 Model, and that one has two DID's:
<IDs>
<DID Size="A4" ID="6" />
<DID Size="LETTER" ID="8" />
</IDs>
For that one, the default is 8, so it should return Letter, but it returns both Letter and A4. Any idea why my if isn't working for that?