xml without name attribute

 

i have a file where everything under “edgepolicies” is a unique name, but

$xml.root.edgePolicies.ChildNodes

returns the structure underneath the child nodes, without the names of the actual child nodes.

i’d like an array with a name, level, source, and value column.

 

 

<?xml version="1.0" encoding="UTF-8" ?>
<root>
  <edgeMetadata>
    <OS>Windows 10 OS Version 1909 (Build 18363.997)</OS>
    <application>Microsoft Edge</application>
    <revision>54dee5e1d729e4c188896f1b1156e98e83557fcc</revision>
    <version>85.0.564.63 (Official build) (64-bit)</version>
  </edgeMetadata>
  <edgePolicies>
    <AllowDeletingBrowserHistory>
      <level>mandatory</level>
      <scope>user</scope>
      <source>platform</source>
      <value>false</value>
    </AllowDeletingBrowserHistory>
    <AudioCaptureAllowedUrls>
      <level>mandatory</level>
      <scope>user</scope>
      <source>platform</source>
      <value>[*.]fake.com</value>
    </AudioCaptureAllowedUrls>
    <AuthNegotiateDelegateAllowlist>
      <level>mandatory</level>
      <scope>user</scope>
      <source>platform</source>
 </edgePolicies>
</root>

 

You can use the hidden get_name() method.

$xml.root.edgePolicies.ChildNodes | Select-Object @{n="Name";e={$_.get_name()}},level,source,value

or

$xml.Selectnodes("//edgePolicies").childnodes | Select-Object @{n="Name";e={$_.get_name()}},level,source,value

Output

Name                           level     source   value       
----                           -----     ------   -----       
AllowDeletingBrowserHistory    mandatory platform false       
AudioCaptureAllowedUrls        mandatory platform [*.]fake.com
AuthNegotiateDelegateAllowlist mandatory platform             

You can view the hidden methods/properties by using Get-Member -Force

$xml | Get-Member -Force

well thank heavens they hid it. :slight_smile:

bonus round: what if some objects have more properties than just “level, source, value”, and i don’t necessarily know what those properties are ahead of time?

nevermind on that one. $xml.root.edgePolicies.ChildNodes seems to show me everything.

also: just for grins, is there a json version of get_name? my initial file was json, and about two frustrating hours after reading my first “but convertfrom-json is so easy!!” article, i figured ‘i’m at least familiar with working with xml, can i convert json to xml and go from there?..yes.’

{
   "edgeMetadata": {
      "OS": "Windows 10 OS Version 1909 (Build 18363.997)",
      "application": "Microsoft Edge",
      "revision": "54dee5e1d729e4c188896f1b1156e98e83557fcc",
      "version": "85.0.564.63 (Official build) (64-bit)"
   },
   "edgePolicies": {
      "AllowDeletingBrowserHistory": {
         "level": "mandatory",
         "scope": "user",
         "source": "platform",
         "value": "false"
      },
      "AudioCaptureAllowedUrls": {
         "level": "mandatory",
         "scope": "user",
         "source": "platform",
         "value": "[*.]fake.com"
      },
      "AuthNegotiateDelegateAllowlist": {
         "level": "mandatory",
         "scope": "user",
         "source": "platform"
      }
   }
}

here is a json sample. again i can get to $json.edgepolicies, but i don’t see any promising methods or properties in get-member -force here. all of my ‘nodes’ are listed as noteproperties.

 

AllowDeletingBrowserHistory NoteProperty System.Management.Automation.PSCustomObject AllowDeletingBrowserHistory=@{level=mandatory; scope=user; source=platform; value=False}
AudioCaptureAllowedUrls NoteProperty System.Management.Automation.PSCustomObject AudioCaptureAllowedUrls=@{level=mandatory; scope=user; source=platform; value=System.Object[]}

$json = @"
{
   "edgeMetadata": {
      "OS": "Windows 10 OS Version 1909 (Build 18363.997)",
      "application": "Microsoft Edge",
      "revision": "54dee5e1d729e4c188896f1b1156e98e83557fcc",
      "version": "85.0.564.63 (Official build) (64-bit)"
   },
   "edgePolicies": {
      "AllowDeletingBrowserHistory": {
         "level": "mandatory",
         "scope": "user",
         "source": "platform",
         "value": "false"
      },
      "AudioCaptureAllowedUrls": {
         "level": "mandatory",
         "scope": "user",
         "source": "platform",
         "value": "[*.]fake.com"
      },
      "AuthNegotiateDelegateAllowlist": {
         "level": "mandatory",
         "scope": "user",
         "source": "platform"
      }
   }
}
"@ | ConvertFrom-Json

$json.edgePolicies.PSObject.Properties | foreach {
    [pscustomobject]@{
        Name  = $_.Name
        Level  = $_.Value.Level
        Scope  = $_.Value.Scope
        Source = $_.Value.Source
        Value  = $_.Value.value
    }
}