在PowerShell中读取XML结构并将其导出到XLS

时间:2014-08-05 11:12:47

标签: xml powershell

我正在努力用Powershell阅读以下XML文件的内容。我想获得关联数组,每个<value>的CN / OU部分作为标识符,每个<roll><group>作为属性。

<?xml version="1.0" encoding="UTF-8"?>
<ldap>
    <host>x.x.x.x
        <users>
            <user>DC=Example,DC=internal</user>
        </users>
        <rights>
            <var>distinguishedName
                <value>CN=...,OU=user,OU=...
                    <roll>2</roll>
                </value>
                <value>CN=...,OU=user,OU=...
                    <roll>5</roll>
                    <roll>18</roll>
                </value>
                <value>CN=John Doe*
                    <roll>9</roll>
                </value>
                <value>CN=Domain Admin*
                    <group>Administrator</group>
                    <roll>1</roll>
                </value>
                <value>CN=...,OU=user,...
                    <group>Example Group</group>
                    <roll>8</roll>
                    <roll>12</roll>
                    <roll>14</roll>
                    <roll>15</roll>
                </value>
            </var>
            <var>search:member=&lt;userdn&gt;
                <value>CN=group1*
                    <group>01 Group XY</group>
                    <roll>1</roll>
                </value>
                <value>CN=Client1,OU=*
                    <roll>3</roll>
                </value>
                <value>CN=...,OU=*
                    <roll>5</roll>
                </value>
                <value>CN=ImportantClient06*
                    <group>06ImportantGroup</group>
                    <roll>8</roll>
                    <roll>11</roll>
                    <roll>12</roll>
                </value>
            </var>
        </rights>
    </host>
</ldap>

这是我的第一个方法:

$d3Xml = [XML] (Get-Content -Path $d3ConfigPath)
$XmlValues = $d3Xml.ldap.host.rights.var

Write-Host "Xml Value: " $XmlValues

我无法在这种xml结构上找到任何帮助。我感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

您可以从这里开始:

$xmlSource = @"
<?xml version="1.0" encoding="UTF-8"?>
<ldap>
<host>x.x.x.x
    <users>
        <user>DC=Example,DC=internal</user>
    </users>
    <rights>
        <var>distinguishedName
            <value>CN=...,OU=user,OU=...
                <roll>2</roll>
            </value>
            <value>CN=...,OU=user,OU=...
                <roll>5</roll>
                <roll>18</roll>
            </value>
            <value>CN=John Doe*
                <roll>9</roll>
            </value>
            <value>CN=Domain Admin*
                <group>Administrator</group>
                <roll>1</roll>
            </value>
            <value>CN=...,OU=user,...
                <group>Example Group</group>
                <roll>8</roll>
                <roll>12</roll>
                <roll>14</roll>
                <roll>15</roll>
            </value>
        </var>
        <var>search:member=&lt;userdn&gt;
            <value>CN=group1*
                <group>01 Group XY</group>
                <roll>1</roll>
            </value>
            <value>CN=Client1,OU=*
                <roll>3</roll>
            </value>
            <value>CN=...,OU=*
                <roll>5</roll>
            </value>
            <value>CN=ImportantClient06*
                <group>06ImportantGroup</group>
                <roll>8</roll>
                <roll>11</roll>
                <roll>12</roll>
            </value>
        </var>
    </rights>
</host>
</ldap>
"@

$xml = [xml] $xmlSource

$xml | select-xml -xpath "//value" | %{ 
        New-Object -TypeName PSObject -Property @{
                CNOU = $_.Node."#text" 
                Group = $_.Node.group
                Roll = $_.Node.roll
        }
    } | select CNOU, group, Roll