0

我是 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>
4

1 回答 1

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
于 2013-07-26T18:08:41.543 回答