Just a quick little snippet that can be used if you want something like email alerts from a script to add an email address to maybe a NOC, but only want it to happen after hours or on the weekends.

function SendMail()
                $Msg = new-object Net.Mail.MailMessage
                $SmtpServer = "mail.myserver.com"
                $Smtp = new-object Net.Mail.SmtpClient($SmtpServer)
                    $Msg.From = "from@myserver.com"
                    $Msg.ReplyTo = "replyto@myserver.com"
                    if ($D.Hour -ge '17' -and $D.Hour -le '7' -or $D.DayOfWeek -eq 'Saturday' -or $D.DayOfWeek -eq 'Sunday')
                    {$Msg.To.Add("noc@example.org") } 
		            $Msg.Priority = [System.Net.Mail.MailPriority]::High
                    $Msg.subject = "Example Subject"
                    $Msg.body = "Example Body "

$d = (Get-Date)


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.