Link to home
Start Free TrialLog in
Avatar of WeTi
WeTi

asked on

Check cert in 60days warning if did.

Dear expert

Below script is not working well for me, the error msg I get is:
Cannot convert value "30/05/2018 20:52:00"
Exception while checking URL https://google.com: Exception calling "GetResponse" with "0" argument(s)
Code is going to check cert if its 60 days left in cert or not...

Anyone know whats wrong?
Regards


 $minimumCertAgeDays = 60
 $timeoutMilliseconds = 10000
 $urls = @(
 "https://google.com"
 )
 [Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
foreach ($url in $urls)
 {
 $req = [Net.HttpWebRequest]::Create($url)
 $req.Timeout = $timeoutMilliseconds
try {$req.GetResponse() |Out-Null} catch {Write-Host Exception while checking URL $url`: $_ -f Red}
[datetime]$expiration = $req.ServicePoint.Certificate.GetExpirationDateString()
 [int]$certExpiresIn = ($expiration - $(get-date)).Days
 $certName = $req.ServicePoint.Certificate.GetName()
 $certPublicKeyString = $req.ServicePoint.Certificate.GetPublicKeyString()
 $certSerialNumber = $req.ServicePoint.Certificate.GetSerialNumberString()
 $certThumbprint = $req.ServicePoint.Certificate.GetCertHashString()
 $certEffectiveDate = $req.ServicePoint.Certificate.GetEffectiveDateString()
 $certIssuer = $req.ServicePoint.Certificate.GetIssuerName()
if ($certExpiresIn -gt $minimumCertAgeDays){
 $certok = Write-Host Cert for site $url expires in $certExpiresIn days [on $expiration] -f Green}
 else{
 $certnotok = Write-Host Cert for site $url expires in $certExpiresIn days [on $expiration] Threshold is $minimumCertAgeDays days. Check details:`n`nCert name: $certName`nCert public key: $certPublicKeyString`nCert serial number: $certSerialNumber`nCert thumbprint: $certThumbprint`nCert effective date: $certEffectiveDate`nCert issuer: $certIssuer -f Red
 }
 }

Open in new window

Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

your script works for me in windows 10

Scripts> . 'G:\Documents\WindowsPowerShell\Scripts\Untitled13.ps1' <# script is not saved yet #>
Cert for site https://google.com expires in 69 days [on 06/05/2018 14:16:00]
Cert for site https://microsoft.com expires in 659 days [on 01/16/2020 16:24:02]
Cert for site https://techsupport4me.com expires in 629 days [on 12/17/2019 07:40:47]

PS G:\Documents\WindowsPowerShell\Scripts>
SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WeTi
WeTi

ASKER

Using Qlemo's option, thanks both.