Starting Exchange Services after a Power Failure

In my environment, with a virtualized SBS 2011 Standard, there are occasionally Microsoft Exchange 2010 services that don't properly restart if there has been an abrupt power failure on the Hyper-V host. (Don't ask.)

Now, of course, the first time this happened, I just logged in to the server and started the services. But when it happened again, it's time to write a script. And it was a fun script, since it uses WMI and PowerShell remoting and other fun stuff.

# Script to start Exchange services on SBS 2011 Server after power failure
#
# Accepts a parameter of the exchange server name, but defaults to SRV2 if none entered
# Assumes you are logged in to the domain with Domain Admin credentials
#
# Created: 14/03/2011 by Charlie
# ModHist: 15/03/11 -switched to using WMI in the session to get StartMode
#
#
param ($ExchSrv = "SRV2" )

# first, open a session to the Exchange server
$srv = New-PSSession $ExchSrv

#Now use Invoke-Command with -Session
Invoke-Command -Session $srv -scriptblock {
   $exsvc = gwmi win32_service | Where-Object {$_.Name -like "MsExch*" `
     -and $_.StartMode -eq "Auto"   -and $_.State -eq "Stopped" }
   if ($exsvc ) {
      foreach ($svc in $exsvc ) {
         Start-Service $svc.name
      }
   }
}

The if statement in there is to prevent an error if all the services are running. Of course, for this script to work as it's written, you'll need to run it from a workstation in the SBS domain, and you'll have to enable PowerShell remoting on both the server and the client. If you haven't done that yet, I've posted a quick setup guide on TechNet.

I've posted this script up to the Microsoft Script Center, so if you have comments or suggestions to improve it, please comment there.

Charlie.

2 Responses to Starting Exchange Services after a Power Failure

  • David Moisan says:

    I’ve used a PowerShell script similiar to that for years. I have found to my detriment over 11 years of SBS experience that the best indication of performance problems with SBS is on startup; I have run 2003, 2008 and now 2011 at home on a Tyan board that I can’t seem to kill. And which is now underpowered.

    I get Exchange service startup errors practically every time I boot. My script just goes through a list of stopped services that have an automatic start mode (same methodology as yours), checks them against an exclusion list (some services are automatic start but are normally stopped), and restarts them. This runs every hour and gives me a note in the event log if it found a stopped service.

    Good for reliabilty (did I say my Tyan board never died?) but I would like to find the root cause one of these days…

  • Charlie Russel says:

    This really shouldn’t be an issue except on startup (so I question the run every hour need). Supposedly, this is related to Exchange running on a GC. See: http://support.microsoft.com/kb/940845.

    That being said, I’m still keeping this script around for when I run into it again. Plus it’s easily modified to do other useful things, which is why I posted it in the first place.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>