Description

This simple script puts the server into maintenance mode using the Start-MaintenanceMode.ps1 script I wrote, logs the event, then reboots the server. Additional processes should be made to account for standard clusters, Exchange clusters, and other application specific requirements. I had stripped this script of all that extra functionality, but if you want, I can add it back in. Let me know.

Script

PowerShell
Edit|Remove
#=============================================================================# 
#                                                                             # 
# Restart-Server.ps1                                                          # 
# Simple PowerShell script to reboot a server.                                # 
# Author: Jeremy Engel                                                        # 
# Date: 04.13.2011                                                            # 
# Version: 1.1.0                                                              # 
#                                                                             # 
#=============================================================================# 
 
Param([Parameter(Mandatory = $false)][switch]$NoMaintenanceMode) 
  
function Main { 
  if(!$NoMaintenanceMode) { 
    .\Start-MaintenanceMode.ps1 -Server $env:computername -Minutes 30 -Comment "Scheduled Reboot" 
    } 
  Start-Sleep -Seconds 30 
  $eventLog = New-Object System.Diagnostics.EventLog("System") 
  $eventLog.MachineName = "." 
  $eventLog.Source = "Scheduled Reboot" 
  $eventLog.WriteEntry("The server has begun its scheduled reboot.","Information",22) 
  $server = Get-WmiObject Win32_OperatingSystem 
  $server.PSBase.Scope.Options.EnablePrivileges = $true 
  $null = $server.Reboot() 
  } 
 
Main