How to check if a certificate is going to Expire and Create a Event in Event Viewer

This script will check for the certificates and then will create a event if the certificates is going to expire within 10 days period. Please note that this requires  capicom.dll  so please register this using command regsvr32 capicom.dll. You can furtehr integrate this with SCOM and generate alert\event for the event which you create.

'*****************************************************************************************************************************************************
'* Copy this file and save it as CertCheck.vbs
'* Enumerate certificates with day left for expiry From local store and create event 351 in case any certificate is going to expiry within 10 days.
'Creates event 351 warning on application event viewer with Certificate deatils
'*******************************************************************************************************************************************************

 Dim Store, Certificates, Certificate
Const CAPICOM_LOCAL_MACHINE_STORE = 1
Const CAPICOM_CERTIFICATE_FIND_TIME_VALID = 9       
Const CAPICOM_STORE_OPEN_READ_ONLY = 0
Dim value
Set Store = CreateObject("CAPICOM.Store")
Store.Open CAPICOM_LOCAL_MACHINE_STORE, "MY" ,CAPICOM_STORE_OPEN_READ_ONLY
Set Certificates = Store.Certificates.Find(CAPICOM_CERTIFICATE_FIND_TIME_VALID,0)
Set WshShell = WScript.CreateObject("WScript.Shell")
If Certificates.Count >0 Then
   For Each Certificate in Certificates
 value=DateDiff("d",now(),Certificate.ValidToDate)
Dis=""
   
if value < 10 Then
Dis = Dis + "Serial Number : " & Certificate.SerialNumber & vbcrlf
Dis=Dis + "Subject Name : " & Certificate.SubjectName & vbcrlf
Dis=Dis +"Issued by : " & Certificate.IssuerName & vbcrlf
Dis=Dis +"Valid from " & Certificate.ValidFromDate & "  to  " & Certificate.ValidToDate

    strCommand = "eventcreate /T Warning /ID 351 /L Application /SO Certinfo /D " & _
    Chr(34) & Dis & Chr(34)
 WshShell.Run strcommand

    End If
   Next
End If
Set Certificates = Nothing
Set Store = Nothing

=====================================================================================

Sudheesh Narayanaswamy | Support Engineer | Microsoft