Schedule a Servers reboot with email notification when are online
Schedule a servers restart it’s a healthy practice, it’s good for apply systems updates and it helps to free up memory. Thera are a lot of ways to do this, in this reminder i’ll show you wath I did. This example it works with Windows 2003, 2008, 2008r2, 2012, 2012r2, 2016…
First of all I chose one of our servers which is going to send the restart order to the rest, in my case i decided to use the main Domain controller, we have a huge number of server and not all of them can be restarted at same time, for this reason I made some restarting groups, you can do the same, the number it’s up to you.
For email notifications I decided to use SendEmail http://caspian.dotconf.net/menu/Software/SendEmail/ this is a free software which can sends emails using command line. You only have to download, and uncompress in a root folder, for example “C:\scripts”
I prepared a file called “reboot_alert_group1.cmd” a I placed in C:\scripts , Inside the file I placed an email notification similar than this
reboot_alert_group1.cmd
c:\Scripts\sendemail.exe -f noreply@MyDomain.com -t MyEmail@MyDomain.com -u "Starting Server restart tasks 1" -m "Now we are restarting servers: Server01, Server02, Server03" -s smtp.office365.com:587 -xu ValidMail@MyDomain.com -xp MyEmailPassword
If you take a look of the script you’ll see there is no secret on it.
sendemail.exe -f “Sender email” -t “Destination Email” -u “Subject” -m “Body message” -s “SMTP Server adsress” -xu “Valid userto auth SMTP” -xp “Password of this user”
Then I prepared another file called restart_servers_group1.cmd, with this one I call reboot_alert_group1.cmd and then send the restart order using powershell to the servers.
restart_servers_group1.cmd
call reboot_alert_group1.cmd powershell restart-computer "Server01","Server02","Server01" -force
Before you can execute this I recommend to you to test the remote access to this servers using powershell, to do this, go to command line and run “powershell”. Take in mind to replace “Server01” with your destination server name that you want to test.
PS C:\Scripts> gwmi win32_networkadapterconfiguration -computername Server01 -filter "ipenabled = 'true'" -erroraction stop | select dnshostname,ipaddress,defaultipgateway,dnsserversearchorder,winsprimaryserver,winssecondaryserver | ft -property * -autosize | out-string -Width 4096
If all it’s Ok, it’s done, but if you get a connection error, probably you have to allow a “Remote Administration” from your IP in Server Firewall
Schedule the restarting tasks in this way, and repeat the process for every servers group
For notifications when servers are going to be online again we will use the same program SendEmail, and the only we are going to do is copy the SendEmail files in a root folder called C:\Script in every server and prepare a file with desired message. In my case I did with a file rebootalert.cmd
rebootalert.cmd
c:\Scripts\sendemail.exe -f noreply@MyDomain.com -t MyEmail@MyDomain.com -u "Server01 has booted by scheduled task" -m "Server01 has booted by scheduled task!" -s smtp.office365.com:587 -xu ValidMail@MyDomain.com -xp MyEmailPassword
And then in this way we can prepare an Scheduled task that calls rebootalert.cmd when server starts.
That’ss all folks