Powershell select and edit node

indydk used Ask the Experts™

How do I select/Edit:  <member key="1" operator="eq" /> under database "000 name" and "HR Huston" (need to change key from 1 to 2)

Using Powershell
   <role id="{1234}" name="Unit12">
    <databases permission="allow">
      <database name="000 Name" inherit="true">
        <cube name="HR" inherit="true">
          <dimension name="HR Huston" inherit="true">
            <hierarchy name="HR Huston" inherit="true">
                <member key="1" operator="eq" />
          <dimension name="HR Boston" inherit="true">
            <hierarchy name="HR Boston" inherit="true">
                <member key="2" operator="eq" />
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
ste5anSenior Developer

Please rephrase your question. Add the code you have so far. Cause there are too many open questions, e.g. is that XML, where does it come from (file, variable or pipeline).. etc.

But: changing a key means deleting the old row and adding a new row. Keys should be stable and optimally static. So what is you use-case here?
Most Valuable Expert 2018
Distinguished Expert 2018
It all comes down to the XPath (note that XML is cAsE sEnSiTiVe, including attribute values!):
This works for your sample (with the missing closing "</databases>" node added!).
$xmlPath = '.\test.csv'

$fullPath = (Resolve-Path -Path $xmlPath).Path
$xml = [xml](Get-Content -Path $xmlPath)
$node = $xml.SelectSingleNode('roles/role/databases/database[@name="000 Name"]/cube/dimension[@name="HR Huston"]/hierarchy/forcedcriteria/member')
$node.SetAttribute('key', '2')

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial