[英]How to export XML Powershell system.object[] arrays to CSV file
[英]Powershell How to export the XML file into Excel (CSV)
我想將此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>
這是我所寫的,但沒有給出我所需要的。
[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
您的工作方向正確,但是執行Add-Member會導致您失敗。 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}
我們在這里創建一個新對象,獲取父對象$xmlProperty
的.Name
屬性,然后從我們也想帶來的$p
選擇兩個額外的值。
希望這可以幫助。
如果您的一個屬性包含多個值,那么您將在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 ','}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.