Tuesday 4 February 2020

Monitoring Server Certificate and Send email to owner when it about to expire


# Get today's date for the report

$Date = Get-Date

# Taking Server Name

$ServerName = $env:Computername

# Checking the Certificate expire infomration and storing as HTML in C:\temp\report.html

Get-ChildItem -Path cert:\localmachine\My | Where-Object {$_.NotAfter -lt ($date).AddDays(30)} | Select-Object @{Name="ComputerName";e={$Servername}},Subject,Issuer,DnsNameList,@{Name="Cert Expires ON";e={$_.NotAfter}},NotBefore,EnhancedKeyUsageList | ConvertTo-Html | Out-File C:\temp\Report.html


# Taking html report as variable

$Result = Get-Content C:\temp\Report.html -Raw

# Creating Email Variable

$subject = "$ServerName Certification Expires Report- " + $Date
$priority = "Normal"
$smtpServer = "Mail.Google.com"
$emailFrom = "Xyz.com"
$emailTo = "abc.com"

# Sedning Report

Send-MailMessage -To $emailTo -Subject $subject -Body $Result -BodyAsHtml -SmtpServer $smtpServer -From $emailFrom -Priority $priority

No comments:

Post a Comment