Is it possible to have Windows send me an email when a restart is required to complete updates? I have my server setup to install updates automatically, but not restart afterwards. I have found that I can add actions for events in the event viewer, but I don't know what to look for.
Email Notifications for Required Restarts in Windows Server 2008
windows-server-2008windows-update
Related Solutions
I went about this a bit differently, but this approach generates emails on new events that match a custom filter, with all the event details included in the email body.
1) Create a 'Custom View' in the Event Viewer with your desired filter.
2) Once you have the view, you should see a link to 'Attach Task to This Custom View...'.
I chose to use sendMail.exe from here (http://caspian.dotconf.net/menu/Software/SendEmail/) which I extracted to C:\sendmail. The reason is Microsoft's 'Send an email' action has issues with SMTP authentication and also apparently isn't even present in Server 2012.
So in my case I selected 'Start a program' while attaching the task to the Custom View. But we're going to edit it as XML so don't worry about filling it in via the GUI.
3) Export the new Task to XML, we'll be editing it later.
4) Create a 'mail-event.bat' file under C:\sendmail folder with the following 3 lines:
C:\Windows\system32\wevtutil.exe qe Application /f:text /q:"<QueryList><Query Id='0' Path='Application'><Select Path='Application'>*[System[(EventRecordID=%1)]]</Select></Query></QueryList>" > C:\sendmail\%1.log
C:\sendmail\sendEmail.exe -s <smtp_server> -f <from> -xu <user> -xp <pass> -t <to> -u "<subject>" -o message-file=c:\sendmail\%1.log
del C:\sendmail\%1.log
Obviously, replace 'smtp_server', 'from', 'user', 'pass', 'to', 'subject' with the desired values.
This will create a '$(EventRecordID).log' file under C:\sendmail with all the details for that event, mail it, and then delete it.
You can test if the batch file works by going into Event Viewer, opening an event in your Applications log, switching to Details tab, selecting 'XML View' and then look for EventRecordID. Copy that integer, and then run from the command line:
C:\sendmail> log-event.bat 53522
Of course, replacing 53522 with the value from the EventRecordID node. If you receive the email, go to your happy place.
NOTE WELL: You might have noticed the string 'Application' shows up a couple times in the command line for wevtutil.exe -- that's because I couldn't seem to get it to work by pointing it directly at the Custom View, and my Custom View happened to be a sub-set of events that are all inside the Application log. You might have to adjust that to make it work in your case if your trying to mail events from the System log, for example.
5) Edit the XML you exported, we're going to make two changes:
First, add the following 'ValueQueries' node into the XML under the 'EventTrigger' node:
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>...snip...</Subscription>
<ValueQueries>
<Value name="EventRecordID">Event/System/EventRecordID</Value>
</ValueQueries>
</EventTrigger>
NOTE: In the above, I snipped the 'Subscription' info which will have been filled in based on the Custom View you created. Don't copy my 'Subscription' into your XML!
Second, replace the Actions node with the following:
<Actions Context="Author">
<Exec>
<Command>C:\sendmail\mail_event.bat</Command>
<Arguments>$(EventRecordID)</Arguments>
</Exec>
</Actions>
Now, cause a new event to appear in your Custom View, and you should automatically get the email notification! Woohoo!
The hanging during reboots is a very well known problem. I suggest changing the way the server does it's updates, especially not allowing it to reboot as part of the updating process. The method I've been using for a number of years with total reliability is:
- Updates are downloaded by the server (in my case they are obtained from a WSUS server) as they become available but not installed.
- Saturday night a script runs to install those updates but the server is not rebooted at this stage.
- Early Sunday morning the server is rebooted using a batch file via the task scheduler. That batch file uses shutdown.exe to perform the reboot. It is first run to log off anyone on the server, then run again to do the actual reboot. Trying to do both at once is very unreliable.
Since implementing that system I've never had a server hang during a reboot. Prior to that at least one of the servers, sometimes more, would hang at few times a year.
In addition to the above I also have a batch file run as a machine startup script which emails me when the server reboots. I check me email first thing Sunday morning. If ever I don't have an email from each of the servers I know I have a problem, because either it didn't start the reboot or it didn't finish it. It doubles as a handy alert should a server ever reboot itself for any reason.
Update:
The install script I use (UpdateHF_v2.5.vbs) was created by Rob Dunn and is available from here. I have customised my copy slightly to suit my own needs.
The reboot batch file is:
@echo off
%windir%\system32\shutdown /l /t 1
%windir%\system32\shutdown /r /t 10 /c "Planned reboot" /f /d P:2:17
The batch file which sends the reboot notification email is (private information removed):
@echo off
c:\windows\blat.exe - -to "list of email addresses, separated with comas" -f "sender email" -s "Reboot Alert (Put the server name here)" -ti 30 -try 120 -noh -body "Whatever message you want" -server mail.server
I use blat but obviously you can use any command line email program you like.
Related Topic
- Windows Updates Fails Server 2008 0x80070490
- Can’t update Windows 2008 server with WSUS installed on it
- Does a Required Restart for Windows Update log an event? If so what is the source/id
- Windows Server 2008 Shut down by itself
- Windows Server – Restart/Shutdown History
- Powershell – Windows Server 2008 Send an email on reboot
- Powershell – Windows Server notify when installing updates finished
Best Answer
Here is a portion of a PowerShell script I have set to run daily on a few of my servers.
The full script sends an html email with other info, but right at the top it tells me every day if there are Windows updates available.