Wednesday, 18 December 2024

Certificate Expiry Email Notification

$Output = [System.Collections.ArrayList]@()
$Output.Clear()
$Thumbprint = import-csv "C:\Users\Documents\Thumbprint.txt"
ForEach($t in $Thumbprint){
    $Cert = Get-ChildItem cert:\localmachine\root\* |?{$_.Thumbprint -eq $($t.Thumbprint)} |`
    Select @{n="Subject";e={(($_.Subject -split '[,]')[0]).Replace("CN=","")}},Thumbprint,@{n="NotAfter";e={($_.NotAfter -split '[" "]')[0]}}
    #$Cert
 
    foreach($c in $cert){
        $NumberOfDaysLeft = [math]::Ceiling((([DateTime]$($c.notafter))- (Get-Date)).TotalDays)
        If($NumberOfDaysLeft -lt 60){
            $Property=[pscustomobject][ordered]@{
                Subject    =  $C.Subject
                Thumbprint =  $C.Thumbprint
                NotAfter   =  $C.NotAfter
                DaysLeft   =  $NumberOfDaysLeft
                Status     =  "About To EXPIRE"
            }
           $Output.add($Property)
        }
        else{
            $Property=[pscustomobject][ordered]@{
                Subject    =  $C.Subject
                Thumbprint =  $C.Thumbprint
                NotAfter   =  $C.NotAfter
                DaysLeft   =  $NumberOfDaysLeft
                Status     =  "Pass"
            }
            $Output.add($Property)
        }
    }
}
$Output
$Result = $Output|Where-Object{$_.Status -match "About"}
If($Result.Count -gt 0){
    Write-Host "Kindly Check Email !" -ForegroundColor RED
    $Output
}
else{
    Write-Host "All Good - Nothing to Worry !" -ForegroundColor Green
}


--------------------------------- End --------------------------------- 

No comments:

Post a Comment