0

我对 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>

电源外壳代码:

[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"
4

2 回答 2

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
于 2013-02-20T15:38:02.553 回答
0

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

[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"
于 2013-02-20T15:23:17.707 回答