2015-12-23 48 views
0

我想將此xml文件導出爲Excel(CSV)。我在網上搜索了一些例子,但我似乎找不到任何可以幫助我的東西。Powershell如何將XML文件導出爲Excel(CSV)

我不擅長PowerShell。

<?xml version="1.0" encoding="UTF-8"?> 
<products updated="12/16/2015"> 
    <product name="office"> 
    <addresslist type="IPv6"> 
     <address>2</address> 
     <address>256</address> 
     <address>434</address> 
    </addresslist> 
    <addresslist type="IPv4"> 
     <address>13.107</address> 
     <address>13.14/24</address> 
    </addresslist> 
    <addresslist type="URL"> 
     <address>*.google</address> 
     <address>*.yahoo</address> 
     <address>*.some other link</address> 
    </addresslist> 
    </product> 
    <product name="LYO"> 
    <addresslist type="URL"> 
     <address>*.rtrt</address> 
     <address>eever</address> 
    </addresslist> 
    <addresslist type="IPv4"> 
     <address>23.178</address> 
     <address>23.18</address> 
     <address>23.19</address> 
    </addresslist> 
    <addresslist type="IPv6"> 
     <address>2a01:13::/4</address> 
     <address>2a01:1</address> 
    </addresslist> 
    </product> 
</products> 

這就是我已經wrtten,但它沒有給我需要什麼。

[xml]$file = get-content ./O365IPAddresses.xml 
$xmlProperties = $file.SelectNodes("/products/product") 

    Foreach ($xmlProperty in $xmlProperties) { 
     $o = New-Object Object 

     Add-Member -InputObject $o -MemberType NoteProperty -Name Name -Value $xmlProperty.name 

     foreach ($p in $xmlProperty.addresslist) 
     { 
      $type += $p.type 
      $add += $p.address 
     } 

     Add-Member -InputObject $o -MemberType NoteProperty -Name Type -Value $type 
     Add-Member -InputObject $o -MemberType NoteProperty -Name Address -Value $add 

     $type=""; 
     $add=""; 

     #Add-Member -InputObject $o -MemberType NoteProperty -Name Name -Value $xmlProperty.name 
     $o 
    } 

    $o=""; 

這就是我需要輸出的代碼。

Name Type V 
Office IPv4 12111,12121,12,12,1,2,12,1,2, 
Office IPv6 12111,12121,12,12,1,2,12,1,2, 
Office URL google, yahoo 
lyo IPv4 12111,12121,12,12,1,2,12,1,2, 
lyo IPv6 12111,12121,12,12,1,2,12,1,2, 
lyo URL some lyn, yahoo 
+1

你能告訴我們你試過的東西不是真的有用嗎?現在這看起來像是寫代碼的請求,而不是這裏的代碼。 – Matt

+0

請參閱編輯 – maj

+0

既然你提到你不知道PS得非常好,我會強烈建議學習如何使用幫助功能。在PS:'help * CSV *'中輸入以下內容 – dfundako

回答

1

你是在正確的軌道上,但做添加成員的東西會導致你失敗​​。 PowerShell是一種面向對象的語言,因此在您瀏覽大量代碼時只需發射和對象會更好,並讓PowerShell爲您準備好所有這些內容。

我修改並截斷了您的代碼。有了這個代碼:

$xmlProperties = $file.SelectNodes("/products/product") 
$o = New-Object Object 
    Foreach ($xmlProperty in $xmlProperties) { 

     foreach ($p in $xmlProperty.addresslist) 
     { 
      [pscustomobject]@{Name=$xmlProperty.Name;Type=$p.type;Address=$p.address} 

     } 

    } 

你會得到這樣的輸出:

Name Type Address        
---- ---- -------        
office IPv6 {2, 256, 434}       
office IPv4 {13.107, 13.14/24}      
office URL {*.google, *.yahoo, *.some other link} 
LYO URL {*.rtrt, eever}      
LYO IPv4 {23.178, 23.18, 23.19}     
LYO IPv6 {2a01:13::/4, 2a01:1}  

,你可以管到Export-Csv製作成電子表格。

我想提請注意[pscustomobject]表示法。這是一個PowerShell v3,可以快速創建一個新對象,該對象接受對象屬性和值的鍵值對。因此,在我們的for-each循環這一點上,我們已經有了定義的變量$p,它具有以下值:

type address    
---- -------    
IPv6 {2a01:13::/4, 2a01:1} 

我們正在做一個新的對象這裏,抓住了父母的財產.Name對象$xmlProperty,然後從$p中挑選出我們想要帶來的兩個額外值。

希望這會有所幫助。

該怎麼辦System.String []

如果你的屬性之一包含一個以上的值,那麼你會得到你的CSV文件中一個奇怪的輸出。本質上,PowerShell將會抓取輸出,並以逗號分隔值格式重新渲染。當一個屬性有兩個值時,PowerShell會將其列爲System.String []或在末尾附加[]的完整對象名稱,這是數組(對象包含多個項目)的表示法。

$xmlProperties = $file.SelectNodes("/products/product") 
$o = New-Object Object 
Foreach ($xmlProperty in $xmlProperties) { 

    foreach ($p in $xmlProperty.addresslist) 
    { 
     [pscustomobject]@{Name=$xmlProperty.Name;Type=$p.type;Address=$p.address} 

    } 

} | select Name, Type, @(N={Address};exp={$_.Address -join ','}} 
+0

'Add-Member'仍然有效。 OP顯然是在嘗試製作物體。就像PowerShell中的所有東西都有很多方法可以做同樣的事情。使用'foreach'就意味着你必須做一些特殊的事情來獲得輸出管道或節省的FYI。 – Matt

+0

謝謝你的幫助。它正好在尋找。 – maj

+0

不客氣!很高興爲您提供幫助 – FoxDeploy