Powershell 如何将 XML 文件导出到 Excel (CSV)
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
你走在正确的轨道上,但是做添加成员的事情会让你失败。 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 及更高版本 shorthand,它接受对象 属性 和值的键值对。因此,此时在我们的 for-each 循环中,我们已经定义了变量 $p
,它具有以下值:
type address
---- -------
IPv6 {2a01:13::/4, 2a01:1}
我们在这里新建一个对象,抓取父对象$xmlProperty
的.Name
属性,然后从$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 ','}}
我想将此 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
你走在正确的轨道上,但是做添加成员的事情会让你失败。 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 及更高版本 shorthand,它接受对象 属性 和值的键值对。因此,此时在我们的 for-each 循环中,我们已经定义了变量 $p
,它具有以下值:
type address
---- -------
IPv6 {2a01:13::/4, 2a01:1}
我们在这里新建一个对象,抓取父对象$xmlProperty
的.Name
属性,然后从$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 ','}}