Jul 152010
 

At my job site we have a very nice and cool lab environment. However due to budget cuts we are currently not in the position to extend our lab. So at the moment we currently have two uninterruptable power supplies (UPS) which would give the systems enough “juice” in case we have a power outrage. However, these UPS systems don’t have a management interface card. The problem with that is we are not able to communicate with the UPS to make sure that the servers are brought down nicely.

So we changed our system environment a bit. The Cisco switches are now connected on dirty power, so if we have a power outrage, the switches would be gone immediately. If the switches fail, our servers will not be able to communicate anymore with their default gateway. Within VMware this is known as isolation mode. VMware will bring it’s VM’s down, however our 3 physical servers (SQL 2008, VCenter and a DC) will have a problem. They are not brought down in such an event.

Therefore I wrote a little PowerShell script you can find below. Simple edit the time and other variables to suit your environment. With the current timers, the quickest shutdown will take place within 4 minutes, else it will take up to a maximum of 6 minutes. Note: make sure your UPS can hold it that long.

Although I do know that this is really a poor mans solution, I don’t think we have a better choice.

[code lang=”ps”]
$Gateway = "10.75.36.254"

function checkStatus {
$PingCount = "2"
 if (!(Test-Connection $Gateway -Count $pingCount -ErrorAction SilentlyContinue)) {
  $Subject="Network Lost"
  $LogLevel= "Warning"
  $Message = "Gateway didn’t respond within a timely fasion"
  WriteEventLog
  Recheck
 }
}

function Recheck{
$PingCount = "4"
#recheck gatway response within 4 pings.
#wait ten seconds before continue. This to rule out a temporarily  unplugged cable.
Start-Sleep -Seconds 180
 if (Test-Connection $Gateway -Count $pingCount -ErrorAction SilentlyContinue) {
  $Subject="Network connection restored"
  $LogLevel= "Information"
  $Message = "Gateway responded again. ‘nConnection restored."
  WriteEventLog
 }
 else {
 #If ping is still not responding, receck it again, else shudown the Windows Server
 Start-Sleep -Seconds 60
  if (!(Test-Connection $Gateway -Count $pingCount -ErrorAction SilentlyContinue )) {
   shutdownSystem
  }
  else {
  $Subject="Network connection restored"
  $LogLevel= "Information"
  $Message = "Gateway responded again. ‘nConnection restored"
  WriteEventLog
  }
 }

Function shutdownSystem{
$Subject="Network Lost"
$LogLevel= "Error"
$Message = "System is going down since network is lost. Possible due to a power failure `nPlease contact one the System Administrators."
WriteEventLog
# Shutting down the computer will start right now.
Stop-Computer -Force
}

Function WriteEventLog {
 $Event=new-object System.Diagnostics.EventLog("System")
 $Eevent.Source=$Subject
 $InfoEvent=[System.Diagnostics.EventLogEntryType]::$LogLevel
 $Event.WriteEntry($Message,$InfoEvent,65000)
}

$Counter = 1
do {
 #loop forever
 start-sleep -Seconds 120
 checkStatus
}
while ($Counter -eq 1)

[/code]