Forum Discussion
Monitor SSL Certificates that will be expired soon and also provide an email notification
- Nov 27, 2020
I hope I understand the last response correct but try to do this change in the $Result Variable
$Result += "
<br>Certificate for Host: $url Expires on -----> $ExpirationDate
<br>Certificate for Host: $url Expires in -----> $DayCount DAYS"The output will be much better and each certificate will be in a separate line
Hello farismalaeb,
Thank you for your valuable and fast support.
I tried to create something even simpler that the code that i have found that works for me.
So i ended up with the following more simple code:
$Urls = @()
$Urls = "https://google.com",
"https://prod-caesar-service-app.apps.sag.services/"
$Result = @()
$MinimumCertAgeDays = 5000
$ErrorActionPreference= 'silentlycontinue'
Foreach ($url in $Urls)
{
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
$req = [Net.HttpWebRequest]::Create($url)
$req.GetResponse() | Out-Null
#$req.ServicePoint.Certificate.GetExpirationDateString()
$ExpirationDate = $req.ServicePoint.Certificate.GetExpirationDateString()
$ExpDateToDT = [Datetime]::ParseExact($ExpirationDate, "dd/MM/yyyy hh:mm:ss", $null)
$DayCount = ( $( $ExpDateToDT ) - $( Get-Date ) ).Days
If ( $DayCount -le $MinimumCertAgeDays )
{
$Result += "
---Certificate for Host: $url Expires on -----> $ExpirationDate
---Certificate for Host: $url Expires in -----> $DayCount DAYS"
}
}
Write-Output $Result
# Sender and Recipient Info
$MailFrom = "alexandros.ananikidis@sag-ag.ch"
$MailTo = "alexandros.ananikidis@sag-ag.ch"
# Sender Credentials
$Username = "alexandros.ananikidis@sag-ag.ch"
$Password = "Mypass"
# Server Info
$SmtpServer = "smtp-mail.outlook.com"
$SmtpPort = "587"
# Message stuff
$MessageSubject = "Live your best life now"
$Message = New-Object System.Net.Mail.MailMessage $MailFrom,$MailTo
$Message.IsBodyHTML = $true
$Message.Subject = $MessageSubject
$Message.Body = $Result
# Construct the SMTP client object, credentials, and send
$Smtp = New-Object Net.Mail.SmtpClient($SmtpServer,$SmtpPort)
$Smtp.EnableSsl = $true
$Smtp.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
$Smtp.Send($Message)
It works fine.
The only issue that i need support now is how to make my email response to include some change of lines as the attached image show. Because it is really hard to read.
Can you maybe help on that one?
Thank you a lot in advance,
Alexandros
I hope I understand the last response correct but try to do this change in the $Result Variable
$Result += "
<br>Certificate for Host: $url Expires on -----> $ExpirationDate
<br>Certificate for Host: $url Expires in -----> $DayCount DAYS"
The output will be much better and each certificate will be in a separate line
- Alexandros8888Dec 01, 2020Copper Contributor
- rtushar1400Mar 10, 2022Copper ContributorHi, can you please help in this context ? I am trying the original script but it is not giving any output .