Freigeben über


Sample Code (VBScript) - Query CAPICOM

This script queries capicom com object to get cert expiration date. Capicom.dll must be installed and registered in order to run this script. If you need additional cert info, you can just add more CAPICOM Cert object properties to my sample code.

Option Explicit
on error resume next
Const CAPICOM_MY_STORE = "My"
Const CAPICOM_LOCAL_MACHINE_STORE  = 1
Const CAPICOM_CURRENT_USER_STORE  = 2
Const CAPICOM_STORE_OPEN_READ_ONLY = 0
Const CAPICOM_EKU_CLIENT_AUTH = 2
Const CAPICOM_EKU_CODE_SIGNING = 3
Const CAPICOM_EKU_EMAIL_PROTECTION = 4
Const CAPICOM_EKU_SERVER_AUTH = 1
Const CAPICOM_EKU_OTHER = 0
Const CR_DISP_ISSUED  = &H3
Const CR_OUT_CHAIN = &H100
Const CR_OUT_BASE64 = &H1
Const CERT_SYSTEM_STORE_LOCAL_MACHINE = &H20000
Const CR_IN_BASE64  = &H1
Const CR_IN_PKCS10  = &H100

Dim Wshshell, Wshfile, oCert, oStore, return, certname, validto, expirationdate, computername, systemroot, cn, sn
Set Wshshell = CreateObject("Wscript.shell")
Set Wshfile = CreateObject("Scripting.FileSystemObject")
computername = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
systemroot = WshShell.ExpandEnvironmentStrings("%SYSTEMROOT%")

REM *** Query cert Store and Update new server records ***
Set oStore = CreateObject ("CAPICOM.Store")
oStore.Open CAPICOM_LOCAL_MACHINE_STORE, CAPICOM_MY_STORE, CAPICOM_STORE_OPEN_READ_ONLY

For Each oCert in oStore.Certificates
 cn = Split(oCert.SubjectName,",")
 sn = Split(cn(0),"=")
 certname = sn(1)
 validto = Split(oCert.ValidToDate," ")
 expirationdate = validto(0)
 WScript.Echo "  server name: " & computername
 WScript.Echo "  certname: " & certname
 WScript.Echo "  Valid To: " & expirationdate
End If 
Next

REM *** Completion and Cleanup
Set Wshshell = nothing
Set Wshfile = nothing
Set oStore = nothing