Powershell Script - Number of days before expiry on logfile

Hello,

I had some great help here last week on writing a small bit of code to count the number of entries in the log file which contains the word expired.

$response1= Invoke-WebRequest -Uri '<logfile URL>'  -TimeoutSec 60
 $JSECertExpired = ($response1.RawContent -split '\n' | Select-String 'expired').Count

I am now looking into getting a count of the number of entries which will expire in the next 20 days or so. I understand it will start with $date = Get-Date but unsure how to do the -20 days. The logs look like the following:

Certificate Unknown:bunn_key.pem:No date/Invalid date:Certificate Date=:Run Date=2021-08-02:Expiry Days=():3
Certificate Unknown:cargosmart_key.pem:No date/Invalid date:Certificate Date=:Run Date=2021-08-02:Expiry Days=():3
Certificate Expired:croatia_cert_10062021.pem::Certificate Date=2021-06-10:Run Date=2021-08-02:Expiry Days=(-53 days):1
Certificate Expired:croatia_key_10062021.pem::Certificate Date=2021-06-10:Run Date=2021-08-02:Expiry Days=(-53 days):1
Certificate Unknown:DBH_key.pem:No date/Invalid date:Certificate Date=:Run Date=2021-08-02:Expiry Days=():3
Certificate Fine:denmark_24032023.pem::Certificate Date=2023-03-24:Run Date=2021-08-02:Expiry Days=(599 days):0
Certificate Fine:denmark_new.pem::Certificate Date=2029-12-08:Run Date=2021-08-02:Expiry Days=(3050 days):0
Certificate Unknown:DHLALBAAD_Privatekey.pem:No date/Invalid date:Certificate Date=:Run Date=2021-08-02:Expiry Days=():3
Certificate Unknown:DHL_key.pem:No date/Invalid date:Certificate Date=:Run Date=2021-08-02:Expiry Days=():3

As some of these are classed as “unknown” my understanding is that they can be ignored so I would be looking for just the one’s with fine.

$date = Get-Date

$response1= Invoke-WebRequest -Uri '<logfile URL>'  -TimeoutSec 60
 $JSECertExpiring = ($response1.RawContent -split '\n' | Select-String 'fine').

The next part is when I am stuck where I just want to count the days where it would be 20 or less?

I was told my explanations last time were all over the place so I hope this one is much better!

Have you tried to search for it first? :wink: For most of the questions about PowerShell it takes only seconds to find a solution online.

$TwentyDaysAgo = (Get-Date).Date.AddDays(-20)

You will have to convert the strings in your log file to actual DateTime objects to be able to compare them against another DateTime object.

But … am I wrong or are there actually already day counters in the log file?

Hi Olaf,

I presume by day counters you are meaning the end of the log? I was thinking of then instead of having to convert strings in the log file is it possible to just get the number on the row for fine and then get a count for those which are less than 20 days?

view log file > search for row for fine > select only the number > count of those which are less than 20 days

You will have to convert anything from text anyway. Even if it looks like a number it is a string when it comes from a text file.

What do you think? :wink: Did you try to find a solution by yourself first? :face_with_raised_eyebrow: What you ask are very common tasks and questions like this have been asked thousand times before and have been answered thousand times before. You are allowed to do your own research and try to solve your problem by yourself first.

Assumed the log file extract you posted last is saved in file you can use something like this to extract the according information:

$result = 
Select-String -Path D:\sample\CertLog.log -Pattern 'Certificate\s+Fine:(.+)::.*Expiry\s+Days=\((\d+)\s+days\)' | 
Foreach-Object {
    [PSCustomObject]@{
        Certificate = $_.matches.Groups[1].Value
        Days        = $_.matches.Groups[2].Value -as [int]
    }
}

$result
1 Like

Thanks for the help Olaf :slight_smile:

I have worked out how to get it to print those showing with minimum 30 days .

$result2a | Where-Object Days -le 30 | Measure-Object

However this prints more than what I need.

**Count : 0
Average :
Sum :
Maximum :
Minimum :
Property : **

Is there a way to get it to print just the 0 not the rest? Or have I used the wrong command

You should stop asking this question. :wink: There is ALWAYS a way.

Please read the help topic about

You should read it completely including the examples to learn how to use it.

Using the following command allowed me to view only the count

$result2a | Where-Object Days -le 30 | Measure-Object |Select -Expand Count

Great. You found it by yourself. I’m proud of you. :+1:t4: :wink: