xml - 使用Powershell以表格形式从XML元素获取数据

标签 xml powershell

我尝试从XML emement获取数据作为表。下面的XML文件。 XML文件已导出防火墙配置。

<?xml version="1.0" encoding="UTF-8"?>
<fpc4:Root xmlns:fpc4="http://schemas.microsoft.com/isa/config-4" xmlns:dt="urn:schemas-microsoft-com:datatypes" StorageName="FPC" StorageType="0">
    <fpc4:Build dt:dt="string">7.0.9193.500</fpc4:Build>
    <fpc4:Comment dt:dt="string">0</fpc4:Comment>
    <fpc4:Edition dt:dt="int">32</fpc4:Edition>
    <fpc4:EnterpriseLevel dt:dt="int">2</fpc4:EnterpriseLevel>
    <fpc4:ExportItemClassCLSID dt:dt="string">{59740B3A-8771-492C-AF59-7764F4F939EF}</fpc4:ExportItemClassCLSID>
    <fpc4:ExportItemCompatibilityVersion dt:dt="int">4</fpc4:ExportItemCompatibilityVersion>
    <fpc4:ExportItemScope dt:dt="int">0</fpc4:ExportItemScope>
    <fpc4:ExportItemStorageName dt:dt="string">{A86DF49D-0078-48D5-8A58-A970482FA6D6}</fpc4:ExportItemStorageName>
    <fpc4:IsaXmlVersion dt:dt="string">9.0</fpc4:IsaXmlVersion>
    <fpc4:OptionalData dt:dt="int">0</fpc4:OptionalData>
    <fpc4:Upgrade dt:dt="boolean">0</fpc4:Upgrade>
    <fpc4:ConfigurationMode dt:dt="int">0</fpc4:ConfigurationMode>
    <fpc4:Arrays StorageName="Arrays" StorageType="0">
        <fpc4:Array StorageName="{7149EEA4-E267-43EA-A7C8-25C3FF1EC368}" StorageType="0">
            <fpc4:AdminMajorVersion dt:dt="int">0</fpc4:AdminMajorVersion>
            <fpc4:AdminMinorVersion dt:dt="int">0</fpc4:AdminMinorVersion>
            <fpc4:Components dt:dt="int">-1</fpc4:Components>
            <fpc4:DNSName dt:dt="string"/>
            <fpc4:Name dt:dt="string"/>
            <fpc4:Version dt:dt="string">0</fpc4:Version>
                <fpc4:Name dt:dt="string"/>
            <fpc4:RuleElements StorageName="RuleElements" StorageType="0">
                <fpc4:ComputerSets StorageName="ComputerSets" StorageType="0">
                    <fpc4:ComputerSet StorageName="{F51D0A66-40CA-4E1E-9454-A1F73834CEFC}" StorageType="2">
                        <fpc4:Name dt:dt="string">ScanSafe</fpc4:Name>
                        <fpc4:Computers StorageName="Computers" StorageType="2">
                            <fpc4:Computer StorageName="{9A946B36-12C6-484F-9729-97D51C53017D}" StorageType="2">
                                <fpc4:IPAddress dt:dt="string">100.93.231.200</fpc4:IPAddress>
                                <fpc4:Name dt:dt="string">1363.scans.net</fpc4:Name>
                            </fpc4:Computer>
                        </fpc4:Computers>
                    </fpc4:ComputerSet>
                    <fpc4:ComputerSet StorageName="{0598ED7C-CA3E-4461-A230-B2669B35872F}" StorageType="2">
                        <fpc4:Name dt:dt="string">Subscribers</fpc4:Name>
                        <fpc4:Computers StorageName="Computers" StorageType="2">
                            <fpc4:Computer StorageName="{1FF8B477-7DF2-4117-978A-044377B5958A}" StorageType="2">
                                <fpc4:IPAddress dt:dt="string">111.111.111.111</fpc4:IPAddress>
                                <fpc4:Name dt:dt="string">Client1</fpc4:Name>
                            </fpc4:Computer>
                            <fpc4:Computer StorageName="{43F2C2EB-BB08-4B2E-A05C-9D61E299820D}" StorageType="2">
                                <fpc4:IPAddress dt:dt="string">222.222.222.333</fpc4:IPAddress>
                                <fpc4:Name dt:dt="string">Client2</fpc4:Name>
                            </fpc4:Computer>
                            <fpc4:Computer StorageName="{6F1CA232-9C17-48B8-A794-65037301C24C}" StorageType="2">
                                <fpc4:IPAddress dt:dt="string">111.111.111.112</fpc4:IPAddress>
                                <fpc4:Name dt:dt="string">Client3</fpc4:Name>
                            </fpc4:Computer>
                        </fpc4:Computers>
                        <fpc4:AddressRanges StorageName="AddressRanges" StorageType="2">
                            <fpc4:AddressRange StorageName="{C7CD37CF-788B-45A0-9A44-2C0DA7C00D37}" StorageType="2">
                                <fpc4:IPFrom dt:dt="string">222.222.222.338</fpc4:IPFrom>
                                <fpc4:IPTo dt:dt="string">222.222.222.340</fpc4:IPTo>
                                <fpc4:Name dt:dt="string">Range 1 </fpc4:Name>
                            </fpc4:AddressRange>
                            <fpc4:AddressRange StorageName="{BC77F5FD-1CBF-4EDB-9968-2DD5ADA9090B}" StorageType="2">
                                <fpc4:IPFrom dt:dt="string">222.222.222.390</fpc4:IPFrom>
                                <fpc4:IPTo dt:dt="string">222.222.222.400</fpc4:IPTo>
                                <fpc4:Name dt:dt="string">Range 2</fpc4:Name>
                            </fpc4:AddressRange>
                        </fpc4:AddressRanges>
                    </fpc4:ComputerSet>
                </fpc4:ComputerSets>
            </fpc4:RuleElements>
        </fpc4:Array>
    </fpc4:Arrays>
</fpc4:Root>

我对以下元素内的数据感兴趣。
<fpc4:ComputerSet StorageName="{0598ED7C-CA3E-4461-A230-B2669B35872F}" StorageType="2">

到目前为止,我已经尝试过了。以下

PS H:\> $XMLfile = 'C:\tmp\asd.xml'
[XML]$xml = Get-Content $XMLfile

$xml.Root.Arrays.Array.RuleElements.ComputerSets.ComputerSet.Item(1)

哪个返回值后面,我如何使其显示在Name / Computer元素内部的数据?
StorageName   : {0598ED7C-CA3E-4461-A230-B2669B35872F}
StorageType   : 2
Name          : Name
Computers     : Computers
AddressRanges : AddressRanges
$xml.Root.Arrays.Array.RuleElements.ComputerSets.InnerText | Format-Table

以不可读的单字符串形式返回所有数据。 |格式表不起作用。

我想在这样的表中获取数据
Name            IPAddress             
Client1         111.111.111.111
Client2         222.222.222.333
Name            AddressRanges
Range 1         222.222.222.338 
                222.222.222.340
Range 2         222.222.222.390
                222.222.222.400

我认为foreach循环/周期会有所帮助,但目前我不知道如何执行此操作。

谢谢。

最佳答案

您需要两个循环,在其中您可以仅使用感兴趣的属性构造自定义对象,这有助于以后的程序处理和输出感兴趣的表格格式:

# Read the XML file into a System.Xml.XmlDocument DOM ([xml]).
$xml = [xml] (Get-Content -Raw 'C:\tmp\asd.xml')

# Get a reference to the common parent element of the elements of interest.
$parentEl = $xml.Root.Arrays.Array.RuleElements.ComputerSets.ComputerSet[1]

# Loop over computers and create custom objects.
$computers = foreach ($computer in $parentEl.Computers.Computer) { 
  [pscustomobject] @{ 
    Name = $computer.Name.InnerText
    IPAddress = $computer.IPAddress.InnerText 
  } 
}

# Loop over address ranges.
$ranges = foreach ($range in $parentEl.AddressRanges.AddressRange) { 
  [pscustomobject] @{ 
     Name = $range.Name.InnerText
     AddressRange = $range.IPFrom.InnerText, $range.IPTo.InnerText
  } 
}

# For display, print the computers and range separately.
$computers | Format-Table
$ranges | Format-Table

注意需要单独使用Format-Table;如果您将两个集契约(Contract)时发送到Format-Table($computer, $ranges | Format-Table),则仅第一个集合中对象的属性将决定表的列,因此您将看不到第二个集合中对象唯一的属性;也就是说,输出$computer, $ranges将显示预期的计算机,但会丢失范围的AddressRange属性值。

请记住,Format-* cmdlet只能用于创建显示表示,而不能用于进一步的程序处理;在这种情况下,您可以将$computers$ranges用于后者。

请注意,Out-File / >同样适用:它们使用隐式使用Format-* cmdlet将用于显示的表示形式保存到文件中。
在当前情况下,由于输入对象的属性少于4个,因此隐式应用了Format-Table

因此,如果要在单个输出文件中捕获两个用于显示的表表示形式,则必须再次使用单独的命令:
$computers | Out-File -FilePath $out_file          # or: $computers >  $out_file
$ranges    | Out-File -FilePath $out_file -Append  # or: $ranges    >> $out_file

以上 yield :

Name    IPAddress
----    ---------
Client1 111.111.111.111
Client2 222.222.222.333
Client3 111.111.111.112


Name     AddressRange
----     ------------
Range 1  {222.222.222.338, 222.222.222.340}
Range 2  {222.222.222.390, 222.222.222.400}

关于xml - 使用Powershell以表格形式从XML元素获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57290287/

相关文章:

java - JAXB自定义命名空间前缀问题

java - 跳过 Android 布局 xml 资源

c# - 我如何动态引用一个程序集来寻找另一个程序集?

html - 如何使用PowerShell的选择字符串从.htm文件中提取文件名?

regex - 使用 RegEx 查找字符串中特定文本后的数字

java - 我无法向 android studio 中的 color.xml 文件添加颜色

python - XPath:仅获取具有特定子元素的元素

function - 支持词法范围 ScriptBlock 参数(例如Where-Object)

Powershell 断言管道返回的不是数组

android - Phonegap 全屏应用