使用Powershell从XML文件提取数据

使用Powershell从XML文件提取数据,xml,powershell,Xml,Powershell,我想从MBSA扫描生成的xml报告中提取下载URL。以前我想使用正则表达式,但有些人不让我这么做,所以现在我尝试使用XmlDocument类,但我有一个问题要处理 以下是MBSA扫描报告的一小部分: <XMLOut> <CatalogInfo> <CreationDate>2018-07-10T10:38:04Z</CreationDate> </CatalogInfo> <Check ID

我想从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>
我试着用这样的东西:

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

我也试过了

$WindowsSecuritUpdatesNode.SelectNodes("//DownloadURL")
但这让我从所有“检查”节点下载URL,而不仅仅是从这个名为“Windows安全更新”的节点


我陷入困境,我缺乏解决这个问题的方法

有时候,Select-Xmlcmdlet在解析Xml时更灵活,甚至可能更快(但我还没有对此进行验证)


$XmlOut包含您的XML。

定义“不起作用”
$WindowsSecurityUpdateSnode.Detail.UpdateData.References.DownloadURL
提供了我所期望的
节点的值列表<代码>选择节点(“//DownloadURL”)在您从文档根目录中选择的b/c中不起作用。您需要从当前节点中进行选择(
SelectNodes('.//DownloadURL')
,请注意前导点)。@AnsgarWiechers非常感谢您$WindowsSecurityUpdateSnode.Detail.UpdateData.References.DownloadURL现在可以工作了(我不知道以前键入的内容,但它没有返回任何值。很抱歉造成混淆)。SelectNodes('.//DownloadURL')也可以完美工作@Kubaba这样做有可能会悄悄地破坏xml文档中的数据,因为没有可靠的方法让
Get Content
猜测文件编码。请始终像这样加载XML文档
$doc=新对象XML$加载文档($path)
。XML解析器具有自动文件编码检测功能,请使用它。
$WindowsSecuritUpdatesNode.SelectNodes("//DownloadURL")
(Select-Xml -Content $XmlOut -XPath "XMLOut/Check[@Name='Windows Security Updates']/descendant::DownloadURL").node."#text"