我一直致力于创建一个powershell脚本,该脚本查找包含证书过期的“expiry-epoch”的行,将值与现在30天后的日期进行比较,然后发送主机名。它通过1个文件夹中的多个文件查找这些行。这些行列为:
set shared certificate TEST1 expiry-epoch 1533121779
set shared certificate TEST2 expiry-epoch 1525700960
问题是如果一个文件有多行包含expiry-epoch,并且第一行的证书在某个日期超过30天过期,例如6个月或3个月,那么我没有输出。以下是我的代码,请告诉我是否有遗漏的东西,因为我很难过。
$c = Get-Date (Get-Date).ToUniversalTime() -UFormat %s
$epochroundedtimes=[math]::Round($c)
$epochtimes=$epochroundedtimes + 2592000
Get-ChildItem -Path "C:\PA-CERT1" -File -Recurse |
ForEach-Object {$epochtimes } {
$epochMatch = $_ | Select-String -pattern "expiry-epoch"
if ($epochMatch){
$certexp = ([double]($_ | Select-String -pattern "expiry-epoch\(\d+)$").Matches.Groups[1].Value)
if($certexp -le $epochtimes) {
$_.Name
}
}
}
答案 0 :(得分:1)
使用更好的RegEx(正面观察)Select-String
只需一步即可获得所有内容:
Get-ChildItem -File |
Select-String -Pattern "(?<=expiry-epoch\s+)(\d+)" |
ForEach-Object {
"File {0} Line# {1} Expiry {2}" -F $_.FileName,$_.LineNumber,$_.Matches.Value
}
基于以上数据的示例输出:
File CERT.TXT Line# 1 Expiry 1533121779
File CERT.TXT Line# 3 Expiry 1525700960
只需使用$_.Matches.Value
修改总有另一种方式:
Get-ChildItem -File |
Select-String -Pattern "certificate (.*?) expiry-epoch (\d+)" |
ForEach-Object {
"File {0} Cert {1} Expiry {2}" -F $_.FileName,
$_.MATCHES[0].GROUPS[1].VALUE,
$_.MATCHES[0].GROUPS[2].VALUE
}
File CERT.TXT Cert TEST1 Expiry 1533121779
File CERT.TXT Cert TEST2 Expiry 1525700960
答案 1 :(得分:0)
我已将previous answer编辑到您的问题中,以便考虑您的新信息。这应该从每个文件中出现,并将其显示在易于阅读的表中:
Get-ChildItem -Path "C:\Users\richm\Desktop\TextFiles" -File -Recurse |
Select-String -Pattern "expiry-epoch (\d+)" |
ForEach-Object {$threshold = [Math]::Round((Get-Date ((Get-Date).AddDays(30)).ToUniversalTime() -UFormat %s))} {
$certEpochTime = $_.Matches.Groups[1].Value
$certExpiryTime = (Get-Date "1/1/1970").AddSeconds($certEpochTime)
New-Object -TypeName PsCustomObject|
Add-Member -MemberType NoteProperty -Name ExpiresSoon -Value ($certEpochTime -le $threshold) -PassThru |
Add-Member -MemberType NoteProperty -Name DaysUntilExpiry -Value ([Math]::Round(($certExpiryTime - (Get-Date)).TotalDays, 2)) -PassThru |
Add-Member -MemberType NoteProperty -Name CertExpiryTime -Value $certExpiryTime -PassThru |
Add-Member -MemberType NoteProperty -Name CertEpochTime -Value $certEpochTime -PassThru |
Add-Member -MemberType NoteProperty -Name FilePath -Value $_.FileName -PassThru |
Add-Member -MemberType NoteProperty -Name LineNumber -Value $_.LineNumber -PassThru
} | Sort-Object DaysUntilExpiry | Format-Table -AutoSize
示例输出:
ExpiresSoon DaysUntilExpiry CertExpiryTime CertEpochTime FilePath LineNumber
----------- --------------- -------------- ------------- -------- ----------
True 3.95 21/04/2018 18:36:13 1524335773 cert3.txt 5
True 14.92 02/05/2018 17:56:15 1525283775 cert1.txt 3
True 14.92 02/05/2018 17:56:15 1525283775 cert1.txt 5
True 14.92 02/05/2018 17:56:15 1525283775 cert2.txt 3
True 14.92 02/05/2018 17:56:15 1525283775 cert3.txt 3
False 39.92 27/05/2018 18:02:15 1527444135 cert1.txt 1
False 39.92 27/05/2018 18:02:15 1527444135 cert2.txt 1
False 39.92 27/05/2018 18:02:15 1527444135 cert3.txt 1
False 99.95 26/07/2018 18:36:41 1532630201 cert2.txt 5