简体   繁体   中英

Using PowerShell to export XML data to CSV. Childnodes with unique names

I've been stuck on this for awhile and cant figure out what I'm missing. I have an XML file which is an export of a camera server configuration. I'd like to match up the users with the cameras that they have permission to but am struggling to figure out how.

Here's a sample of the XML:

<?xml version="1.0" encoding="UTF-8"?>
<ExportedConfiguration verification="-11111111">
    <config timestamp="2017-05-30-08-35-21-533">
        <Users>
            <userlist>
                <user0>
                    <username>Smith, John</username>
                    <cameras>
                        <UserCamera-fd346f0a-362c-4171-b015-b3884db7d60f>
                            <cameraid>Inside-Camera1</cameraid>
                        </UserCamera-fd346f0a-362c-4171-b015-b3884db7d60f>
                    </cameras>
                </user0>
                <user1>
                    <username>Smith, Jane</username>
                    <cameras>
                        <UserCamera-07798ce7-b3fd-4cae-a0cd-339de22f38ab>
                            <cameraid>Outside-Camera1</cameraid>
                        </UserCamera-07798ce7-b3fd-4cae-a0cd-339de22f38ab>
                    </cameras>
                </user1>
              </userlist>
          </Users>

The problem is that everything falls under <user#> and the camera info falls under a random <UserCamera-####> node inside of <user#> . I think I need some sort of foreach loop based off the <user#> but not sure where to start.

[EDIT] Here's what I have so far. When ran it just displays "cameraid" instead of the actual name:

[xml]$file = Get-Content videoserver.xml
$xmlProperties = $file.SelectNodes("//Users/userlist/*")
$o = New-Object Object
foreach ($xmlProperty in $xmlProperties) {
  $username = $xmlproperty.username
  $camera = $xmlProperties |
            Select-Xml -XPath "//cameraid" |
            Select-Object –ExpandProperty “node”

  [PSCustomObject]@{
    username = $username;
    Cameras  = $camera
  }
}

Something like this should work:

$file.SelectNodes("//Users/userlist/*") | ForEach-Object {
  [PSCustomObject]@{
    Username = $_.username
    Cameras  = $_.cameras.ChildNodes.cameraid -join ', '
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM