Blade1 Blade1 - 3 months ago 9
PowerShell Question

Get element depending on value of attribute within

I have a XML document laid out like below:

<?xml version="1.0" encoding="utf-8"?>
<mailmanager xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<locations>
<store id="57fb3dc3-8716-4a71-ab5c-d8c76e20640b">
<type>msg</type>
<description>01038 - Wood Wharf - Fire</description> <folder>SharePoint:https://site.domain.com/legal/01038/documents/Forms/CRMView.aspx</folder>
</store>
<store id="6873a00e-e49c-4602-af27-49d2900563d3">
<type>msg</type>
<description>01038 - Wood Wharf - Fire</description>
<folder>\\site.domain.com\legal\01038\documents\</folder>
<fileable>0</fileable>
</store>
<store id="d5e3af58-bc1d-4e45-a2dc-a9ceed803456">
<type>msg</type>
<description>05515 - IRELAND - Dart Underground   -  DART UNDERGROUND INTERCONNECTOR PROJECT</description> <folder>SharePoint:https://site.domain.com/legal/05515/documents/Forms/CRMView.aspx</folder>
</store>


With thousands of
store
elements.

With PowerShell, is it possible to get an entire parent element (i.e. store with everything within) based on the
folder
value? So if
folder
value contains
site.domain.com/legal/02323
for example, I get the parent element.

Answer

Sure, just select all stores and filter the one which folder child contain a specific value using the Where-Object cmdlet:

[xml]$content = Get-Content 'your_xml'
$content.mailmanager.locations.store | 
    Where-Object { $_.folder -eq '\\site.domain.com\legal\01038\documents\' }

Output:

id          : 6873a00e-e49c-4602-af27-49d2900563d3
type        : msg
description : 01038 - Wood Wharf - Fire
folder      : \\site.domain.com\legal\01038\documents\
fileable    : 0
Comments