-1

I have this type of XML schema of User/Roles

<Department/>
<JobTitle/>
<Language/>

<Roles>
  <UserRole public-id="pc:2001">
    <Role public-id="superuser"/>
    <User public-id="system:44"/>
  </UserRole>
  <UserRole public-id="usrrol:48">
    <Role public-id="underwriter_supervisor"/>
    <User public-id="system:44"/>
  </UserRole>
  <UserRole public-id="usrrol:49">
    <Role public-id="user_admin"/>
    <User public-id="system:44"/>
  </UserRole>
</Roles>
  1. I want to search on nodes UserRole.User on the attribute public-id >> Like system:44 which I have in a variable (this identifies the user).

  2. Then return all text values for UserRole.Role attribute public-id >> Like "user_admin". The UserRole is the associated parent of the first search.


I tried dot notation like below, but powershell seems to not like the reference to $user.public-id. I get a parsing error 'FullyQualifiedErrorId'

foreach ($user in $xml.import.User.Roles.UserRole.User) { 
    if ($user.public-id -eq 'system:44') {
    $userRole = $user.GetParent
    write-host "Role - " $userRole.Role.publid-id
    }
}

2 Answers2

1

You need to quote the property since it has a dash. Also I didn't see a GetParent() method but get_ParentNode() works.

foreach ($user in $xml.import.User.Roles.UserRole.User) {
    if ($user.'public-id' -eq 'system:44') {
    $userRole = $user.Get_ParentNode()
    write-host "Role -" $userRole.Role.'public-id'
    }
}

Plus you have a typo in the line with write-host (publid-id)

Output

Role - superuser
Role - underwriter_supervisor
Role - user_admin
Doug Maurer
  • 8,090
  • 3
  • 12
  • 13
0

You can probably simplify it using Select-Xml and xpath (this assumes that your xml above is property wrapped in and opening and closing tag:

foreach ($user in $xml | Select-Xml ".//UserRole[./User[@public-id='system:44']]//Role/@public-id") {    
    write-host "Role - " $user
    }
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45