The Daily Reboot (with execution check)

To workaround memory leaks in its application, one of my customers had an old bad thing on its webservers: a scheduled perl script to reboot the servers each day which uses the ‘Win32::InitiateSystemShutdown’ function, like this:

‘Win32::InitiateSystemShutdown ("","Reboot MACHINE ",60,0,1);’

According to the documentation, the parameter ‘0’ setted in fourth position is the reason why sometimes the servers didn’t reboot and no one knows it…

Perl win32 quick reference
Shutsdown the specified MACHINE, notifying users with the supplied MESSAGE, within the specified TIMEOUT interval. Forces closing of all documents without prompting the user if FORCECLOSE is true, and reboots the machine if REBOOT is true.

Let’s improve it a little bit with powershell.

  1. First the ‘DailyReboot.ps1’ script
  2. <#
    .SYNOPSIS
        DailyReboot - Restarts the local computer daily (if daily scheduled!)
    .DESCRIPTION
        DailyReboot - Automation of a Daily Reboot
        Restarts the local computer daily (if daily scheduled!) and write a entry in the eventlog, for checking purpose.
    .NOTES
        File Name  : DailyReboot.ps1
        Author     : Fabrice ZERROUKI - fabricezerrouki@hotmail.com
    .EXAMPLE
        PS D:\>DailyReboot.ps1
        Restarts the local computer. Has to be scheduled to be 'daily'.
    #>
    Restart-Computer -Force
    
    $ScriptName=$MyInvocation.MyCommand.ToString()
    $LogName="System"
    $ScriptPath=$MyInvocation.MyCommand.Path
    $Username=$env:USERNAME
    New-EventLog -Source $ScriptName -LogName $LogName -ErrorAction SilentlyContinue
    $Message="Script: " + $ScriptPath + "`nScript User: " + $Username + "`nStarted: " + (Get-Date).toString() + "`n`nThe computer has been restarted successfully."
    Write-EventLog -LogName $LogName -Source $ScriptName -EventID "6005" -EntryType "Information" -Message $Message
    

    Has to be scheduled, every day at 05h00 for example.
    http://www.zerrouki.com/schedule-a-powershell-script-execution/

  3. Then, we need to check, every day too, if the reboot has occured, and send an alert email if not!
    Here’s the ‘DailyRebootCheck.ps1’ script
  4. <#
    .SYNOPSIS
        DailyRebootCheck - Checks if the local computer rebooted today
    .DESCRIPTION
        DailyRebootCheck - Checks if the local computer rebooted today and send an alert email if not.
    .NOTES
        File Name  : DailyRebootCheck.ps1
        Author     : Fabrice ZERROUKI - fabricezerrouki@hotmail.com
    .EXAMPLE
        PS D:\>DailyRebootCheck.ps1
        Searches the System event log for the event written by the DailyReboot.ps1 script. Has to be scheduled to be 'daily'.
    #>
    $Today=(Get-Date).AddHours(-2)
    $check=Get-EventLog -logname System -Source "DailyReboot.ps1" -After $Today -ErrorAction SilentlyContinue
    
    if (!$check) {
    $MailTo="recipient@mail.com"
    $MailSubject="$env:computername - Daily reboot failed!"
    $MailFrom="sender@mail.com"
    $SmtpServer="mailserver.example.com"
    $MailBody=@"
    Today ($Today), the computer $env:comptername has <strong>not</strong> been able to restart. Please have a look at it.
    "@
    Send-MailMessage -To $MailTo -Subject $MailSubject -From $MailFrom -SmtpServer $SmtpServer -Priority High -BodyAsHtml $MailBody
    
    Write-Host "Get-EventLog: No matches found"
    Write-Host "Email alert send to $MailTo" -ForegroundColor DarkGreen
    }
    

    Has to be scheduled, every day at least a few minutes after the ‘DailyReboot.ps1’ script schedule execution.
    http://www.zerrouki.com/schedule-a-powershell-script-execution/

Leave a Reply

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

Scroll to Top