【问题标题】:Powershell How to export the XML file into Excel (CSV)Powershell 如何将 XML 文件导出到 Excel (CSV)
【发布时间】:2016-03-29 22:52:56
【问题描述】:

我想将此 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

【问题讨论】:

  • 您能否向我们展示您尝试过但没有真正奏效的方法?现在这读起来像是一个代码编写请求,这不是 SO 的目的。
  • 既然你提到你对PS不太了解,我强烈建议学习如何使用帮助功能。在 PS 中输入:help *CSV* 并从那里开始

标签: xml powershell powershell-3.0


【解决方案1】:

你在正确的轨道上,但做 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 中挑选出我们也想带入的两个额外值。

希望这会有所帮助。

如何处理 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 ','}}

【讨论】:

  • Add-Member 仍然可以占有一席之地。 FWIW OP 显然是在尝试制造物体。就像 PowerShell 中的所有内容一样,有很多方法可以做同样的事情。以您的方式使用 foreach 意味着您必须做一些特别的事情来获取输出管道或保存仅供参考。
  • 感谢您的帮助。它正是我正在寻找的。​​span>
  • 请注意,如果您导出到 csv,您将在导出中得到数组字符(因此地址列将列出 SystemArray[[]]),因为有多个值对于该行,PowerShell 不知道如何处理。如果遇到这种情况,请查看使用 -join 命令。如果你喜欢,我稍后会在我回到电脑上时添加一个示例的链接
  • 添加了如何处理 CSV 中的 System.String[] 字符的示例。
猜你喜欢
  • 2013-07-15
  • 1970-01-01
  • 1970-01-01
  • 2015-02-15
  • 1970-01-01
  • 2023-04-05
  • 2016-02-23
  • 2016-04-20
  • 1970-01-01
相关资源
最近更新 更多