Skip to content

Instantly share code, notes, and snippets.

@dimiboy
Last active February 5, 2017 21:37
Show Gist options
  • Save dimiboy/46c21973e881e3c4ab4ce9957de32c77 to your computer and use it in GitHub Desktop.
Save dimiboy/46c21973e881e3c4ab4ce9957de32c77 to your computer and use it in GitHub Desktop.
Check-SSL.ps1
<#
.SYNOPSIS
Check if SSL certificate of a specific site will expire within a given threshold.
.DESCRIPTION
Check if SSL certificate of a specific site will expire within a given threshold.
.PARAMETER Threshold
Set the threshold in days to check if the SSL expires within that threshold.
.PARAMETER WebsiteURL
List the websites to check (comma separated)
.PARAMETER SendMail
If specified, The script will send a mail for every expiring website.
.PARAMETER from
List the websites to check (comma separated).
.PARAMETER pass
Specify the password for your mail address
.PARAMETER to
List the mail addresses you want the script to send mails to (comma separated).
.PARAMETER SmtpServer
Specify your SMTP server
.EXAMPLE
.\Check-SSL.ps1
Runs the script with defaults written within the script
.EXAMPLE
.\Check-SSL.ps1 -Threshold 30 -WebsiteURL https://www.google.com , https://www.youtube.com
Checks Google.com and Youtube.com if their SSL will expire in 30 days.
Provides results in the PowerShell window only.
.EXAMPLE
.\Check-SSL.ps1 -Threshold 30 -WebsiteURL https://www.google.com , https://www.youtube.com -SendMail -from scripts@contoso.com -to john@gmail.com -SmtpServer smtp.contoso.com -pass P@55w0rd
Checks Google.com and Youtube.com if their SSL will expire in 30 days.
Provides results in the PowerShell window & sends a mail for every expiring site or error to john@gmail.com
.NOTES
Author: Dima Kantargi
website: http://www.DimusTech.net
Changelog:
1.0 (20.08.2016) Initial Release
.LINK
http://www.DimusTech.net
http://www.dimustech.net/2016/08/check-ssl.html
#>
[CmdletBinding()]
Param (
[string]$Threshold = 30,
[string[]]$WebsiteURL = ("https://google.com","https://youtube.com"),
$from = "john@gmail.com",
$pass = "MY-PASSWORD",
$to = ("john1@gmail.com", "john2@gmail.com"),
$SmtpServer = "smtp.gmail.com",
[switch]$SendMail
)
$secpasswd = (ConvertTo-SecureString "$pass" -AsPlainText -Force)
$mycreds = New-Object System.Management.Automation.PSCredential ($from, $secpasswd)
foreach ($site in $WebsiteURL)
{
try {
[Net.HttpWebRequest] $req = [Net.WebRequest]::create($site)
$req.Method = "GET"
$req.Timeout = 10000 # = 10 Seconds
[Net.HttpWebResponse] $result = $req.GetResponse()
[IO.Stream] $stream = $result.GetResponseStream()
$cert = $req.ServicePoint.Certificate
$ValidTo = [datetime]::Parse($Cert.GetExpirationDatestring())
$ValidDays = $($ValidTo - [datetime]::Now).Days
if ($ValidDays -lt $Threshold)
{
Write-Host "`nStatus: Warning $site Expires in $ValidDays days" -ForegroundColor Yellow
Write-Host "CertExpiration: $ValidTo`n" -ForegroundColor Yellow
if ($SendMail)
{
$Subject = "Warning $site SSL Expires in $ValidDays days"
$Body = "Warning $site SSL Expires in $ValidDays days"
#Send-mail -server $SmtpServer -from $from -tos $to -subject $Subject -body $Body
Send-MailMessage -To $to -From $from -Subject $Subject -Body $Body -BodyAsHTML -SmtpServer $SmtpServer -port 587 -UseSsl -credential $mycreds
}
} else {
Write-Host "$site : SSL still good" -ForegroundColor Green
}
[IO.StreamReader] $reader = New-Object IO.StreamReader($stream)
[string] $output = $reader.readToEnd()
$stream.flush()
$stream.close()
} catch {
Write-Host "Error occured while checking $site `nThe Error: $_ "-ForegroundColor Red
if ($SendMail)
{
$Subject = "Error has occured while running SSL-Check script"
$Body = "Error occured while checking $site <br/> The Error: $_ "
Send-MailMessage -To $to -From $from -Subject $Subject -Body $Body -BodyAsHTML -SmtpServer $SmtpServer -port 587 -UseSsl -credential $mycreds
}
}
}
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment