How to Check Certificate expirations on IIS with custom script.

Recently I ran into a SSL cert issue. I run a small e-commerce site and was doing some testing on it. Got to the buy now feature (when the site switches from non- SSL to SSL or http to https) I received a your SSL cert has expired. What? No I remember I purchased it forever ago but it shouldn’t be up now why did I have no warning?

Well with the help and guidance of the crew @ AwesomeIdeas I found a way to look up my SSL Certificate life.

copy & paste script below into a file called "CertExpirationCheck.vbs" and run the script from command line

When in the command prompt use the following parameter:

C:\> cscript certexpirationcheck.vbs [SubjectName]

C:\> cscript certexpirationcheck.vbs mikedopp.com

CertExpirationCheckScript

'**************************************************
'* CertExpirationCheck.vbs
'* Enumerate certificates with day left for expiry
'**************************************************

Option Explicit
Dim SubjectName
If WScript.Arguments.Count > 0 Then
SubjectName = LCase(WScript.Arguments(0))
Else
CommandUsage
End If

Dim Store, Certificates, Certificate
Const CAPICOM_LOCAL_MACHINE_STORE = 1
Const CAPICOM_CERTIFICATE_FIND_SUBJECT_NAME = 1
Const CAPICOM_STORE_OPEN_READ_ONLY = 0

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_SUBJECT_NAME, SubjectName, 0)

If Certificates.Count >0 Then
For Each Certificate in Certificates
'Certificate.display() 'If you want to see the Cert in UI
WScript.Echo "*** Subject " & Certificate.SubjectName & " ***"
WScript.Echo "Issued by " & Certificate.IssuerName
WScript.Echo "Valid from " & Certificate.ValidFromDate & " to " & Certificate.ValidToDate
WScript.Echo "Days to expiry " & DateDiff("d",now(),Certificate.ValidToDate)
WScript.Echo
Next
Else
WScript.Echo "No certificates with SubjectName => '" & SubjectName & "'"
End If

Set Certificates = Nothing
Set Store = Nothing

Sub CommandUsage
MsgBox "Usage: CertExpirationCheck.vbs [SubjectName] ", vbInformation,"CertExpirationCheck"
WScript.Quit(1)
End Sub
 
 

 

 

 

 

 

No Comments