I needed to be able to send an email 24 hours before a server is rebooted via a scheduled task.
In order to achieve this I have a task that will reboot the server called „Computer reboot for updates”. The below script gathers the Next Run time of the scheduled task for the server reboot and then sends an email with that information.
#Destination email
$To = "john.doe@nodomain.com"
#Sender email
$From = "it@nodomain.com"
#SMTP server used
$SMTPServer = "0.0.0.0"
#name of the scheduled task for reboots
$taskname = "Computer reboot for updates"
#getting the reboot time from the task details
$rebootime = $((Get-ScheduledTask -TaskName $taskname | Get-ScheduledTaskInfo).NextRunTime)
#Building the email
$messageParameters = @{
Subject = "$env:ComputerName.$env:USERDNSDOMAIN Will be REBOOTED on $rebootime"
Body = "Server $env:ComputerName.$env:USERDNSDOMAIN Will be REBOOTED on $rebootime in order to apply the Windows Updates. Thank you, IT Team"
from = $From
To = $To
SmtpServer = $SMTPServer
}
#Sending the email
Send-MailMessage @messageParameters -BodyAsHtml
The result is an email like below:
From: it@nodomain.com
To: john.doe@nodomain.com
Subject: Test.domain.com Will be REBOOTED on 11/30/2017 06:00:00
Body: Server test.domain.com Will be REBOOTED on 11/30/2017 06:00:00 in order to apply the Windows Updates. Thank you, IT Team