Use PowerShell to monitor and check the date of cells within a csv file and email before expiry

Problem:

How do I check the date of cells within a csv file using PowerShell

Solution:

User the below script and run on a scheduled task

$date= Get-Date

$days = "-10"

$data = Import-Csv \\server\share\dates.csv

foreach($i in $data) 
    { 
        $CertName = $i.Application 
        $Expiry = $i.Expiry
        $AlertEmail = "myemailaddress@domain.com"
        $Type = $i.Domain

        $Expiry = get-date $Expiry 
        $Expiry1 = ($Expiry).adddays($days) 
 
        if ($Expiry1 -le $date)
            { 
                $to1 = $AlertEmail 
                $message = new-object Net.Mail.MailMessage 
                $smtp = new-object Net.Mail.SmtpClient($smtpserver,$smtpPort)
                $smtp.EnableSsl = $true
                $smtp.Credentials = new-object Net.NetworkCredential($username, $password)
                $message.From = $from 
                $message.To.Add($to1) 
                $message.bcc.ADD($errormail) 
                $message.IsBodyHtml = $False 
                $message.Subject = "Attention: Cert $CertName will expire on $Expiry - $Type" 
                $smtp.Send($message) 
            } 
    } 

Leave a Reply

Your email address will not be published. Required fields are marked *