-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-CrlExpiration.ps1
73 lines (66 loc) · 2.41 KB
/
Get-CrlExpiration.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<#
.Synopsis
Retrieve CRL and verify ThisUpdate and NextUpdate is satisfactory
.DESCRIPTION
Uses certutil -dump <crlFile> to determine CRL date and verify it is greater than X minutes
.EXAMPLE
Get-CrlExpiration -Uri "http://pki.example.com/pki/CA1.crl" -AlertThreshold 90
Get-CrlExpiration -Uri "http://pki.example.com/pki/CA1+.crl" -AlertThreshold 90
#>
function Get-CrlExpiration
{
[CmdletBinding()]
[Alias()]
param (
# number of minutes remaining until NextUpdate for triggering alert
[int]$AlertThreshold = 90,
# URL to CRL
[Parameter(Mandatory=$true)]
[string]$Uri
)
Function Remove-InvalidFileNameChars {
param(
[Parameter(Mandatory=$true,
Position=0,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)]
[String]$Name
)
$invalidChars = [IO.Path]::GetInvalidFileNameChars() -join ''
$re = "[{0}]" -f [RegEx]::Escape($invalidChars)
return ($Name -replace $re)
}
try
{
# download CRL file
$uriFilename = Remove-InvalidFileNameChars ($Uri -split "\/")[-1]
$outFile = "$env:TEMP\$uriFilename-$([DateTime]::Now.ToString("yyyyMMdd-HHmmss"))"
Invoke-WebRequest -Uri $Uri -OutFile $outFile -UseBasicParsing
# parse certutil output
$crlDate = certutil -dump $outFile
$thisUpdate = (($crlDate -match "ThisUpdate\:") -split "\: ")[1]
$nextUpdate = (($crlDate -match "NextUpdate\:") -split "\: ")[1]
# determine number of minutes remaining
$timeLeft = New-TimeSpan -Start (Get-Date) -End (Get-Date $nextUpdate)
# determine pass/fail
If ($timeLeft.TotalMinutes -gt $AlertThreshold){
$Healthy = $True # pass
} else {
$Healthy = $False # fail
}
# Output
Write-Verbose "Uri: $Uri. OutFile: $outFile. ThisUpdate: $thisUpdate. NextUpdate: $NextUpdate. TotalMinutes Remaining: $($timeLeft.TotalMinutes). AlertThreshold: $AlertThreshold. Healthy: $Healthy"
New-Object -TypeName PSObject -Property ([ordered]@{
Uri = $Uri
ThisUpdate = $thisUpdate
NextUpdate = $nextUpdate
TotalMinutesLeft = $($timeLeft.TotalMinutes)
Healthy = $Healthy
})
}
catch
{
Write-Error $_
continue
}
}