使用PowerShell进行自定义csv转换的xml

时间:2017-04-06 12:44:57

标签: xml powershell csv

以下xml:

<references bit="-1" pointname="1-ANN-01_CMD">
    <function drop-id="1" function-id="01A7" number="103" originating="1" title="HR-1 ANNUNCIATION-4" unit-id="1" />
</references>
<references bit="-1" pointname="1-ANN-01_KB">
    <function drop-id="2" function-id="01A7" number="104" originating="0" title="HR-1 ANNUNCIATION-5" unit-id="1" />
    <function drop-id="3" function-id="01B7" number="105" originating="0" title="HR-1 ANNUNCIATION-DUPLICATE" unit-id="1" />
</references>
<references bit="-1" pointname="test">
    <function drop-id="5" function-id="01A7" number="107" originating="1" title="HR-1 ANNUNCIATION-4" unit-id="1" />
    <function drop-id="6" function-id="01A8" number="108" originating="0" title="HR-1 ANNUNCIATION-5" unit-id="1" />
</references>

我正在使用的powershell脚本是

$MainXmlFile=([xml]"<root>$(gc BigShtFile.xml)</root>").root.references | % {
foreach ($item in $_) {
    $Obj = New-Object Object


    Add-Member -InputObject $Obj -MemberType NoteProperty -Name PName -Value $_.pointname | ? {originating -eq "0"}
    if ($_.function.originating -eq 0) {
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name Drop   -Value ($_.function | ? -Property originating -EQ 0 | Select -Property "drop-id" -Exp "drop-id")
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name SHEET  -Value ($_.function | ? -Property originating -EQ 0 | Select -Property number -Exp number)
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name DWG   -Value ($_.function | ? -Property originating -EQ 0 | Select -Property "function-id" -Exp "function-id")
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name TITLE  -Value ($_.function | ? -Property originating -EQ 0 | Select -Property title -Exp title)
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name UNIT   -Value ($_.function | ? -Property originating -EQ 0 | Select -Property "unit-id" -Exp "unit-id")
   } else {                                                            
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name Drop  -Value $_.function."drop-id" 
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name SHEET -Value $_.function.number
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name DWG -Value $_.function."function-id"
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name TITLE -Value $_.function.title
    Add-Member -InputObject $Obj -MemberType NoteProperty -Name UNIT -Value $_.function."unit-id"
           }
    #Add-Member -InputObject $Obj -MemberType NoteProperty -Name BIT -Value $_.bit
    $Obj
  }
}
$MainXmlFile | Format-Table -AutoSize | Export-Csv Output.csv -NoTypeInformation

我得到的输出是: - output of the powershell script

而所需的输出应包含4行,其中条目1-ANN-01-KB应重复两次,后续信息为单行。 请帮忙。

1 个答案:

答案 0 :(得分:2)

我无法将此添加到您的代码中;我不习惯那种做法。请参阅下面的使用.索引的方法,就像您在代码的某些部分中所做的那样。 如果需要,您可以替换$obj哈希表中分配值的部分。

$MainXmlFile=([xml]"<root>$(gc BigShtFile.xml)</root>").root.references | % {
    foreach ($item in $_) {

        if($item.function.originating -eq 0){

            # the second loop, which is missing from your original code
            foreach ($func in $item.function){

                # casting as an array is essential otherwise you will get an error:
                # Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.

                [array]$obj += New-Object psobject -Property @{
                    PName = $item.pointname
                    Drop  = $func."drop-id"
                    SHEET = $func.number
                    DWG   = $func."function-id" 
                    TITLE = $func.title
                    UNIT  = $func."unit-id"
                }
            }

        }else{

            <# I'm not sure what behaviour you're expect this else to perform.
             # So I'm leaving here for completeness for now
            foreach ($func in $item.function){
                [array]$obj += New-Object psobject -Property @{
                    PName = $item.pointname
                    Drop  = $func."drop-id"
                    SHEET = $func.number
                    DWG   = $func."function-id" 
                    TITLE = $func.title
                    UNIT  = $func."unit-id"
                }
            }
            #>
        }

    }
}

# Use Format-Table to output to host. Don't use it or any of the Format- functions with Export-CSV; they won't do what you want
$obj |  Format-Table -AutoSize

# A PSObject will export without order by default. TO get the order you want, use Select and the required columns
$obj | Select-Object PName, Drop, SHEET, DWG, TITLE, UNIT | Export-Csv Output.csv -NoTypeInformation

输出(到屏幕,无序。订购了CSV) Output