1

我有一个包含过期元素以及其他属性的 XML 文件。我必须解析文件并通过 Powershell 脚本找出在未来 50 天内到期的所有元素。

XML 文件中的条目:

<Certificate>
        <Version>3</Version>
        <SignatureAlgorithm>sha1RSA</SignatureAlgorithm>
        <Subject />
        <NotAfter>2017-07-12T09:19:44Z</NotAfter>
        <NotBefore>2016-07-12T09:19:44Z</NotBefore>
        <IsVerified>true</IsVerified>
</Certificate>

现在通过Powershell,我正在尝试做这样的事情:

$ExpiryDate=(GET-DATE).AddDays(50)
$xdoc = [xml] (get-content $InputFile)
$xdoc.SelectNodes("//Certificate/NotAfter[. > $ExpiryDate]")
#$xdoc.Save($ResultsFile)

但这似乎不起作用。我什至不确定是否需要将“NotAfter”字段作为日期或字符串进行比较。任何指针都会有很大帮助。

4

1 回答 1

0

Without a complete example of your xml input, it's quite difficult to give you a proper answer. However, you can get an idea of how to proceed in the example below:

$xml = [xml] @"
    <?xml version="1.0" encoding="UTF-8" ?>
    <CertificateList>
        <Certificate>
                <Version>3</Version>
                <SignatureAlgorithm>sha1RSA</SignatureAlgorithm>
                <Subject />
                <NotAfter>2017-07-12T09:19:44Z</NotAfter>
                <NotBefore>2016-07-12T09:19:44Z</NotBefore>
                <IsVerified>true</IsVerified>
        </Certificate>
        <Certificate>
                <Version>3</Version>
                <SignatureAlgorithm>sha1RSA</SignatureAlgorithm>
                <Subject />
                <NotAfter>2014-05-12T09:19:44Z</NotAfter>
                <NotBefore>2016-03-12T09:19:44Z</NotBefore>
                <IsVerified>true</IsVerified>
        </Certificate>
    </CertificateList>
"@

$ExpiryDate=(Get-Date).AddDays(50)
$xml.CertificateList.Certificate | ?{ [DateTime] $_.NotAfter -gt $ExpiryDate }
于 2016-10-07T07:51:24.400 回答