Remoting

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.