Powershell:在单个CSV文档中合并具有不同数据的行

时间:2013-07-26 16:07:08

标签: xml powershell csv

我是Powershell的新手,还在学习,

我有一个如下所示的CVS文件: https://dl.dropboxusercontent.com/u/95418691/example.png

mac地址和vlan与上面的行一致,但我似乎找不到办法,因为不是每行都列出了一个mac地址。我使用以下代码从一个奇怪的XML创建了这个CVS文件:

$XML | 
    select-xml -xpath "/SolarWinds_SwitchPortMap/Interfaces/Interface |
    /SolarWinds_SwitchPortMap/Interfaces/Interface/MACs/MAC" | 
    select -ExpandProperty Node | 
    select -Property Interface, Description, MACAddress, ip, DNS, VLAN, Speed | 
    Export-Csv -NoTypeInformation -Path "c:\Users\$env:username\Desktop\$ip.csv"

因此,无论是修复此代码还是编写其他内容以组合CVS行的方法都会很棒,如果不介意解释您的解决方案,我正在努力学习:)谢谢。

编辑: 这就是XML的样子:

 <SolarWinds_SwitchPortMap Layer2Device="<info>">
     <Interfaces>
         <Interface ifIndex="<info>" CollectionTime="<info>" ConnectorPresent="<info>" Duplex="<info>" HardwareType="<info>" ifAdminStatus="<info>" ifAdminStatustest="<info>" Description="*** Frame 1 ***" ifDescr="<info>" MTU="<info>" Interface="<info>" ifOperStatus="<info>" ifSpeed="<info>" ifType="<info>" ifTypeName="<info>" InBitsSec="<info>" InPktsSec="<info>" LastChange="<info>" LastPacketIn="<info>" LastPacketOut="<info>" ModulePortIndex="<info>" OutBitsSec="<info>" OutPktSec="<info>" Port="<info>" TrunkPort="False">
             <MACs>
                 <MAC MACAddress="<info>" IPAddress="" DNS="" VLAN="1" Manufacturer="<info>" /> 
             </MACs>
         </Interface>
         <Interface ifIndex="<info>" CollectionTime="<info>" ConnectorPresent="<info>" Duplex="<info>" HardwareType="<info>" ifAdminStatus="<info>" ifAdminStatustest="<info>" Description="*** Frame 2 ***" ifDescr="<info>" MTU="<info>" Interface="<info>" ifOperStatus="<info>" ifSpeed="<info>" ifType="<info>" ifTypeName="<info>" InBitsSec="<info>" InPktsSec="<info>" LastChange="<info>" LastPacketIn="<info>" LastPacketOut="<info>" ModulePortIndex="<info>" OutBitsSec="<info>" OutPktSec="<info>" Port="<info>" TrunkPort="False">
             <MACs>
                 <MAC MACAddress="<info>" IPAddress="" DNS="" VLAN="1" Manufacturer="<info>" /> 
             </MACs>
         </Interface>
         <Interface ifIndex="<info>" CollectionTime="<info>" ConnectorPresent="<info>" Duplex="<info>" HardwareType="<info>" ifAdminStatus="<info>" ifAdminStatustest="<info>" Description="*** Frame 3 ***" ifDescr="<info>" MTU="<info>" Interface="<info>" ifOperStatus="<info>" ifSpeed="<info>" ifType="<info>" ifTypeName="<info>" InBitsSec="<info>" InPktsSec="<info>" LastChange="<info>" LastPacketIn="<info>" LastPacketOut="<info>" ModulePortIndex="<info>" OutBitsSec="<info>" OutPktSec="<info>" Port="<info>" TrunkPort="False" /> 
         <Interface ifIndex="<info>" CollectionTime="<info>" ConnectorPresent="<info>" Duplex="<info>" HardwareType="<info>" ifAdminStatus="<info>" ifAdminStatustest="<info>" Description="*** Frame 4 ***" ifDescr="<info>" MTU="<info>" Interface="<info>" ifOperStatus="<info>" ifSpeed="<info>" ifType="<info>" ifTypeName="<info>" InBitsSec="<info>" InPktsSec="<info>" LastChange="<info>" LastPacketIn="<info>" LastPacketOut="<info>" ModulePortIndex="<info>" OutBitsSec="<info>" OutPktSec="<info>" Port="<info>" TrunkPort="False">
             <MACs>
                 <MAC MACAddress="<info>" IPAddress="" DNS="" VLAN="1" Manufacturer="<info>" /> 
             </MACs>
         </Interface>
     </Interfaces>
 </SolarWinds_SwitchPortMap>

1 个答案:

答案 0 :(得分:1)

尝试这样的事情:

$csv  = "$env:USERPROFILE\Desktop\$ip.csv"
$expr = '/SolarWinds_SwitchPortMap/Interfaces/Interface'

$XML | Select-Xml -XPath $expr | select -Expand Node | % {
  New-Object -Type PSCustomObject -Property @{
    "Interface"   = $_.Interface;
    "Description" = $_.Description;
    "Speed"       = $_.ifSpeed;
    "MACAddress"  = $_.MACs.MAC.MACAddress;
    "IPAddress"   = $_.MACs.Mac.ip;
    "DNS"         = $_.MACs.MAC.DNS;
    "VLAN"        = $_.MACs.MAC.VLAN;
  }
} | Export-Csv $csv -NoTypeInformation