Powershell XML Export-csv不附加哈希

时间:2013-02-20 13:58:16

标签: c# .net powershell powershell-v2.0 powershell-remoting

我是Powershell的新手,根据我的说法,我有相当奇怪的xml,我的主要目标是将XML转换为CSV,然后将其进一步用于同步到数据库。 我使用Powershell代码将xml提取为哈希。 现在我有问题将它们转换为CSV。 Export-csv仅写入散列中的最后一个条目。不知道为什么这个,我查了几个其他论坛,他们说Export-csv中的-append功能只有PowerShell 3.0,我使用1.0 ..

你可以帮我解决这个问题吗?

XML:

<catalog>
<segment>
<segmentname>Batch</segmentname>
<hosts>
<host>
<hostname>cglist17</hostname>
</host>
<host>
<hostname>cglist18</hostname>
</host>
<host>
<hostname>cglist19</hostname>
</host>
<host>
<hostname>cglist20</hostname>
</host>
</hosts>
</segment>
<segment>
<segmentname>Custom S2</segmentname>
<hosts>
<host>
<hostname>cglist21</hostname>
</host>
<host>
<hostname>cglist22</hostname>
</host>
</hosts>
</segment>
<segment>
<segmentname>eCommerce</segmentname>
<hosts>
<host>
<hostname>cglist09</hostname>
</host>
<host>
<hostname>cglist10</hostname>
</host>
</hosts>
</segment>
</catalog>

Powershell代码:

[xml]$xd=gc "C:\Users\sxa8869\Desktop\Webserv\segmentcat.xml"
$nodelist = $xd.selectnodes("/catalog/segment")

$MasterArray = @()
$MasterArray = "" | Select segmentname,hosts

$file="C:\Users\sxa8869\Desktop\Webserv\sha.csv"

foreach ($node in $nodelist) {

  $hostsNode = $node.selectSingleNode("hosts")
  $segmentname = $node.selectSingleNode("segmentname")
  $MasterArray.segmentname=$segmentname.get_InnerXml()
  foreach ($hostitem in $hostsNode) {
    $hostnamenodes = $hostitem.selectnodes("host")
    foreach ($hostname in $hostnamenodes) {
      $MasterArray.hosts=$hostname.selectSingleNode("hostname").get_InnerXml()

#- Displays the hash in table format, want this format in csv
         $MasterArray        

# - displaying only the last element
      $MasterArray | export-csv "file.csv" -notype      

# - this works, but i want to use export-csv to get it in this format
 '"'+$MasterArray.segmentname+'"'+","+'"'+$MasterArray.hosts+'"'    

    }     
  } 
} 


Required output :-
 -----------------
"segmentname","hosts"
"Batch","cglist17"
"Batch","cglist18"
"Batch","cglist19"
"Batch","cglist20"
"Custom S2","cglist21"
"Custom S2","cglist22"
"eCommerce","cglist09"
eCommerce,"cglist10"

2 个答案:

答案 0 :(得分:1)

我认为你的事情太复杂了。试试这个:

$myXML = [xml](Get-Content "C:\Users\sxa8869\Desktop\Webserv\segmentcat.xml")
$file="C:\Users\sxa8869\Desktop\Webserv\sha.csv"

$myArray = @()
foreach ($s in $myXML.catalog.segment)
{
    foreach ($h in $s.hosts.host)
    {
        $myArray += New-Object -TypeName PSObject -Property @{"Segment Name" = $s.segmentname; "Host Name" = $h.hostname}
    }
}

$myArray | Export-Csv $file -NoTypeInformation

答案 1 :(得分:0)

这里有多个问题。 1,创建数组MasterArray,然后将其设置为等于使用Select创建的自定义对象。现在MasterArray不再是一个数组了,而是一个pscustomobject。 2,将值设置为单个对象属性,每次都会覆盖最后一个条目。数组需要包含多个对象。我重写了它。我没有通过使用管道等改进它,但只是修复了你的错误;-)脚本:

[xml]$xd = gc "C:\Users\sxa8869\Desktop\Webserv\segmentcat.xml"
$nodelist = $xd.selectnodes("/catalog/segment")

$MasterArray = @()

$file="C:\Users\sxa8869\Desktop\Webserv\sha.csv"

foreach ($node in $nodelist) {

    $hostsNode = $node.SelectSingleNode("hosts")
    $segmentname = $node.SelectSingleNode("segmentname")
    #Store static value for all objects in $node
    $segname = $segmentname.InnerText

    foreach ($hostitem in $hostsNode) {
        $hostnamenodes = $hostitem.SelectNodes("host")
        foreach ($hostname in $hostnamenodes) {
            #Add hostrecord to array
            $MasterArray += New-Object psobject -Property @{
            "Segment Name" = $segname
            "Host Name" = $hostname.InnerText
            }
        }
    }
}

#Output to console
$MasterArray        

#Save to file (path in $file variable  =  "...\sha.csv").
#Using Select-Object to specify order of columns
$MasterArray | Select-Object "Segment Name", "Host Name" | Export-Csv $file -NoTypeInformation

“sha.csv”中的输出:

"Segment Name","Host Name"
"Batch","cglist17"
"Batch","cglist18"
"Batch","cglist19"
"Batch","cglist20"
"Custom S2","cglist21"
"Custom S2","cglist22"
"eCommerce","cglist09"
"eCommerce","cglist10"