Sunday, December 21, 2008

Automatically restart failed Exchange services using PowerShell...

 

Automatically restart failed Exchange services using PowerShell...

New blog... first post.  Unfortunately I don't have anything ground breaking or earth shattering to share.

Oh wait, there's this, at least...

This morning when I arrived at work, I found that my main Hyper-V server had been restarted due to the installation of security patches.  While all 29 of my virtual machines restarted successfully, more than half of my Exchange services failed on each of my seven Exchange servers.  Frustrated with having to deal with this yet again, I sat down for a few minutes and threw together a little script to help automatically start all of those failed Exchange services upon logging onto the server. 

There are two files involved:

  • CheckServices.cmd (stored at C:\Documents and Settings\All Users\Start Menu\Programs\Startup)
  • CheckServices.ps1 (stored at C:\Program Files\Microsoft\Exchange Server\Scripts)

CheckServices.cmd

@echo off

powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command "checkservices.ps1"

CheckServices.ps1

cls

$checkservices=test-servicehealth

$servicestatus=$checkservices

if ($servicestatus.requiredservicesrunning -match "False")

{

Write-Host " "

Write-Host "Server Roles:"

$servicestatus.role

Write-Host " "

Write-Host "Services Not Started:"

$servicestatus.servicesnotrunning

Write-Host " "

Write Host "Starting required services. Please wait..."

Write-Host " "

foreach ($service in $servicestatus.servicesnotrunning) { start-service $service }

Write-Host " "

}

$checkservices=test-servicehealth

$servicestatus=$checkservices

if ($servicestatus.requiredservicesrunning -match "True")

{

Write-Host " "

Write-Host "All required services are started..."

Write-Host " "

}

else

{

Write-Host " "

Write-Host "The following services failed to start:"

Write-Host " "

$servicestatus.servicesnotrunning

Write-Host " "

}

CheckServices.cmd acts as a launcher for CheckServices.ps1, which executes test-servicehealth against Exchange services. If any of the services are in a stopped state for the installed Exchange role(s), the script will attempt to restart them.  If you decide to use this script, you will likely need to modify your file paths.

Yeah, I know. There’s a seven year old out there somewhere who could have done this in 30 characters or less… Try not to laugh too hard at my l33t scripting skillz! J

Published Sunday, December 21, 2008 9:20 PM by DaveH

My life as a UC n00b... : Automatically restart failed Exchange services using PowerShell...

No comments:

Blog Archive