【问题标题】:Extract data from XML file using Powershell使用 Powershell 从 XML 文件中提取数据
【发布时间】:2018-08-13 23:30:48
【问题描述】:

我想从 MBSA 扫描生成的 xml 报告中提取下载 URL。以前我想使用正则表达式,但有些人劝阻我放弃这个想法,所以现在我尝试使用 XmlDocument 类,但我有一个问题要处理。

这是 MBSA 扫描报告的一小部分:

<XMLOut>
    <CatalogInfo>
        <CreationDate>2018-07-10T10:38:04Z</CreationDate>
    </CatalogInfo>
    <Check ID="500" Grade="5" Type="5" Cat="1" Rank="1" Name="SQL Server Security Updates" URL1="Help/Check5311.html" URL2="Help/Check5311fix.html" GroupID="0a4c6c73-8887-4d7f-9cbe-d08fa8fa9d1e" GroupName="SQL Server">
        <Advice>No security updates are missing.</Advice>
        <Detail>
            <UpdateData ID="MS06-061" GUID="07609d43-d518-4e77-856e-d1b316d1b8a8" BulletinID="MS06-061" KBID="925673" Type="1" IsInstalled="true" Severity="4" RestartRequired="false">
                <Title>MSXML 6.0 RTM Security Update  (925673)</Title>
                <References>
                    <BulletinURL>http://www.microsoft.com/technet/security/bulletin/MS06-061.mspx</BulletinURL>
                    <InformationURL>http://support.microsoft.com/kb/925673</InformationURL>
                    <DownloadURL>http://www.download.windowsupdate.com/msdownload/update/v3-19990518/cabpool/msxml6-kb925673-enu-amd64_cc347d98b9fe1e417cb73f0ddf004d1f94a4bfcf.exe</DownloadURL>
                </References>
                <OtherIDs>
                    <OtherID Type="CVE">CVE-2006-4685</OtherID>
                    <OtherID Type="CVE">CVE-2006-4686</OtherID>
                </OtherIDs>
            </UpdateData>
        </Detail>
    </Check>
    <Check ID="500" Grade="2" Type="5" Cat="1" Rank="1" Name="Windows Security Updates" URL1="Help/Check5311.html" URL2="Help/Check5311fix.html" GroupID="6964aab4-c5b5-43bd-a17d-ffb4346a8e1d" GroupName="Windows">
        <Advice>141 security updates are missing. 4 service packs or update rollups are missing.</Advice>
        <Detail>
            <UpdateData ID="MS15-118" GUID="a0c35f05-9153-439f-a8e9-109d4324c750" BulletinID="MS15-118" KBID="3097989" Type="1" IsInstalled="false" Severity="3" RestartRequired="false">
                <Title>Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 and Windows Server 2008 R2 SP1 for x64-based Systems (KB3097989)</Title>
                <References>
                    <BulletinURL>http://www.microsoft.com/technet/security/bulletin/MS15-118.mspx</BulletinURL>
                    <InformationURL>http://support.microsoft.com/kb/3097989</InformationURL>
                    <DownloadURL>http://download.windowsupdate.com/d/msdownload/update/software/secu/2015/10/windows6.1-kb3097989-x64_892eaa91fcd986ed1249fceb875118bd63646266.cab</DownloadURL>
                </References>
            </UpdateData>
            <UpdateData ID="MS14-046" GUID="ee136505-4841-4e95-9e60-ca2f84f60c12" BulletinID="MS14-046" KBID="2943357" Type="1" IsInstalled="false" Severity="3" RestartRequired="false">
                <Title>Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 and Windows Server 2008 R2 SP1 for x64-based Systems (KB2943357)</Title>
                <References>
                    <BulletinURL>http://www.microsoft.com/technet/security/bulletin/MS14-046.mspx</BulletinURL>
                    <InformationURL>http://support.microsoft.com/kb/2943357</InformationURL>
                    <DownloadURL>http://download.windowsupdate.com/c/msdownload/update/software/secu/2014/07/windows6.1-kb2943357-x64_dc59f4f51d16484d7b72cb38d8b8931f7e38e524.cab</DownloadURL>
                </References>
            </UpdateData>
            <UpdateData ID="MS15-029" GUID="322e5906-e2ae-4fb0-b297-876725555c09" BulletinID="MS15-029" KBID="3035126" Type="1" IsInstalled="false" Severity="3" RestartRequired="false">
                <Title>Security Update for Windows 7 for x64-based Systems (KB3035126)</Title>
                <References>
                    <BulletinURL>http://www.microsoft.com/technet/security/bulletin/MS15-029.mspx</BulletinURL>
                    <InformationURL>http://support.microsoft.com/kb/3035126</InformationURL>
                    <DownloadURL>http://download.windowsupdate.com/d/msdownload/update/software/secu/2015/02/windows6.1-kb3035126-x64_adbc52e8abd005e2e8b9e02325cfe45717a2b0ee.cab</DownloadURL>
                </References>
            </UpdateData>
            MANY MANY MORE...
            <UpdateData ID="MS11-100" GUID="57260dfe-227c-45e3-9ffc-2fc77a67f95a" BulletinID="MS11-100" KBID="2656356" Type="1" IsInstalled="false" Severity="4" RestartRequired="false">
                <Title>Security Update for Microsoft .NET Framework 3.5.1 on Windows 7 and Windows Server 2008 R2 SP1 for x64-based Systems (KB2656356)</Title>
                <References>
                    <BulletinURL>http://www.microsoft.com/technet/security/bulletin/MS11-100.mspx</BulletinURL>
                    <InformationURL>http://go.microsoft.com/fwlink/?LinkID=237378</InformationURL>
                    <DownloadURL>http://download.windowsupdate.com/msdownload/update/software/secu/2011/12/windows6.1-kb2656356-x64_01b0f5428ef6eb2782e6f2c617f06fba8bbf4460.cab</DownloadURL>
                </References>
            </UpdateData>
        </Detail>
    </Check>
    <Check ID="180" Grade="4" Type="1" Cat="1" Rank="10" Name="Incomplete Updates" URL1="Help/Check5340.html" URL2="Help/Check5340fix.html" >
        <Advice>No incomplete software update installations were found.</Advice>
    </Check>
</XMLOut>

我只想从名为“Windows 安全更新”的“检查”节点下载 URL。

到目前为止,我有以下代码:

[xml]$MBSAscann = Get-Content -Path "C:\Users\AAA\Desktop\results1.xml"
$WindowsSecuritUpdatesNode = $MBSAscann.SelectNodes("//Check") | ?{$_.Name -eq "Windows Security Updates"}

我试过用这样的东西:

$WindowsSecuritUpdatesNode.Detail.UpdateData.References.DownloadURL

但这不起作用(此命令仅适用于“UpdateData”:$WindowsSecuritUpdatesNode.Detail.UpdateData)。

我也试过了

$WindowsSecuritUpdatesNode.SelectNodes("//DownloadURL")

但这让我返回了所有“检查”节点的下载 URL,而不仅仅是来自名为“Windows 安全更新”的节点。

我卡住了,我不知道如何解决这个问题。

【问题讨论】:

  • 定义“不起作用”。 $WindowsSecuritUpdatesNode.Detail.UpdateData.References.DownloadURL 给了我一个 &lt;DownloadURL&gt; 节点的值列表,正如我所期望的那样。 SelectNodes("//DownloadURL") 不起作用 b/c 您从文档根目录中选择。您需要从当前节点中进行选择(SelectNodes('.//DownloadURL'),注意前导点)。
  • @AnsgarWiechers 非常感谢! $WindowsSecuritUpdatesNode.Detail.UpdateData.References.DownloadURL 现在可以工作了(我不知道我之前输入了什么,但它没有返回任何值。很抱歉造成混淆)。 SelectNodes('.//DownloadURL') 也很完美!
  • @Kubaba 做 this [xml]$doc = Get-Content -Path $path 承担着默默破坏 XML 文档中数据的风险,因为 Get-Content 没有可靠的方法来猜测文件编码.请始终像 $doc = New-Object xml; $doc.Load($path) 这样加载 XML 文档。 XML解析器有自动文件编码检测,使用吧。

标签: xml powershell


【解决方案1】:

有时 Select-Xml cmdlet 更灵活地解析 XML,甚至可能更快(但我尚未验证)

(Select-Xml -Content $XmlOut -XPath "XMLOut/Check[@Name='Windows Security Updates']/descendant::DownloadURL").node."#text"

$XmlOut 包含您的 XML。

【讨论】:

    猜你喜欢
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 2010-12-04
    • 2013-07-28
    • 2013-03-20
    相关资源
    最近更新 更多