Using Test-ServiceHealth for Exchange Server Health Checks

Exchange Server 2010 (and 2007 for that matter) ships with a useful PowerShell cmdlet called Test-ServiceHealth.

As the name suggests, Test-ServiceHealth checks the state of the services that should be running on the Exchange server. One of the best things about this cmdlet is that it checks the services depending on the Exchange server roles that are installed.

So for example, for a Hub Transport server only those services relating to the Hub Transport role will be checked. While for a “typical” Exchange 2010 server the services for the Hub Transport, Client Access, and Mailbox server roles will be checked.

Here is an example of the Test-ServiceHealth results.

[PS] C:\>Test-ServiceHealth br-ex2010-mb

Role                    : Mailbox Server Role
RequiredServicesRunning : True
ServicesRunning         : {IISAdmin, MSExchangeADTopology, MSExchangeIS, MSExchangeMailboxAssistants, MSExchangeMailSub
                          mission, MSExchangeRepl, MSExchangeRPC, MSExchangeSA, MSExchangeSearch, MSExchangeServiceHost
                          , MSExchangeThrottling, MSExchangeTransportLogSearch, W3Svc, WinRM}
ServicesNotRunning      : {}

Role                    : Client Access Server Role
RequiredServicesRunning : True
ServicesRunning         : {IISAdmin, MSExchangeAB, MSExchangeADTopology, MSExchangeFBA, MSExchangeFDS, MSExchangeMailbo
                          xReplication, MSExchangeProtectedServiceHost, MSExchangeRPC, MSExchangeServiceHost, W3Svc, Wi
                          nRM}
ServicesNotRunning      : {}

Role                    : Hub Transport Server Role
RequiredServicesRunning : True
ServicesRunning         : {IISAdmin, MSExchangeADTopology, MSExchangeEdgeSync, MSExchangeServiceHost, MSExchangeTranspo
                          rt, MSExchangeTransportLogSearch, W3Svc, WinRM}
ServicesNotRunning      : {}

As you can see that is a lot of useful information. But whenever possible I like to see just the minimum relevant information for my servers. In the case of Test-ServiceHealth the RequiredServicesRunning result is the thing I am most interested in.

So in this case I could run the following command to see just that information:

[PS] C:\>Test-ServiceHealth br-ex2010-mb | ft Role,RequiredServicesRunning -auto

Role                      RequiredServicesRunning
----                      -----------------------
Mailbox Server Role                          True
Client Access Server Role                    True
Hub Transport Server Role                    True

Much better.

Now suppose I wanted to run that for all of my Exchange servers. I could do that with the following command:

[PS] C:\>Get-ExchangeServer | Test-ServiceHealth | ft Role,RequiredServicesRunning -auto

Role                      RequiredServicesRunning
----                      -----------------------
Client Access Server Role                    True
Hub Transport Server Role                    True
Client Access Server Role                    True
Hub Transport Server Role                    True
Mailbox Server Role                         False
Mailbox Server Role                          True
Client Access Server Role                    True
Hub Transport Server Role                    True
Mailbox Server Role                          True
Client Access Server Role                    True
Hub Transport Server Role                    True

Interesting, especially the one that failed the test for the Mailbox server role. But in that output I can’t tell which server failed.

Let’s try this instead:

[PS] C:\>$servers = Get-ExchangeServer
[PS] C:\>foreach ($server in $servers) {
>> Write-Host "Checking" $server.name
>> Test-ServiceHealth $server | ft Role,RequiredServicesRunning -auto
>> }
>>

Now we get output that is a little more useful, and tells me which server failed the test.

Checking HO-EX2010-CAHT1

Role                      RequiredServicesRunning
----                      -----------------------
Client Access Server Role                    True
Hub Transport Server Role                    True

Checking HO-EX2010-CAHT2

Role                      RequiredServicesRunning
----                      -----------------------
Client Access Server Role                    True
Hub Transport Server Role                    True

Checking HO-EX2010-MB1

Role                RequiredServicesRunning
----                -----------------------
Mailbox Server Role                   False

Checking HO-EX2010-MB2

Role                RequiredServicesRunning
----                -----------------------
Mailbox Server Role                    True

Checking BR-EX2010-CAHT

Role                      RequiredServicesRunning
----                      -----------------------
Client Access Server Role                    True
Hub Transport Server Role                    True

Checking BR-EX2010-MB

Role                      RequiredServicesRunning
----                      -----------------------
Mailbox Server Role                          True
Client Access Server Role                    True
Hub Transport Server Role                    True

But it still isn’t quite enough, so let’s wrap this up into a handy script that will do the following:

  • Run Test-SystemHealth for all of the Exchange servers in the organization
  • Tell me which servers passed the test
  • Tell me which servers failed the test, and why

Here is the script code that will perform those steps.

#Get the list of Exchange servers in the organization
$servers = Get-ExchangeServer

#Loop through each server
ForEach ($server in $servers)
{
	Write-Host -ForegroundColor White "---------- Testing" $server

	#Initialize an array object for the Test-ServiceHealth results
	[array]$servicehealth = @()

	#Run Test-ServiceHealth
	$servicehealth = Test-ServiceHealth $server

	#Output the results
	ForEach($serverrole in $servicehealth)
	{
		If ($serverrole.RequiredServicesRunning -eq $true)
		{
			Write-Host $serverrole.Role -NoNewline; Write-Host -ForegroundColor Green "Pass"
		}
		Else
		{
			Write-Host $serverrole.Role -nonewline; Write-Host -ForegroundColor Red "Fail"
			[array]$notrunning = @()
			$notrunning = $serverrole.ServicesNotRunning
			ForEach ($svc in $notrunning)
			{
				$alertservices += $svc
			}
			Write-Host $serverrole.Role "Services not running:"
			ForEach ($al in $alertservices)
				{
					Write-Host -ForegroundColor Red `t$al
				}
		}
	}
}

The output from running the script will look something like this.

You can now see at a glance which servers have passed the test, which ones failed, and which services aren’t running for the servers that failed.

About Paul Cunningham

Paul is a Microsoft Exchange Server MVP and publisher of Exchange Server Pro. He also holds several Microsoft certifications including for Exchange Server 2007, 2010 and 2013. Find Paul on Twitter, LinkedIn or Google+, or get in touch for consulting/support engagements.

Comments

  1. Worked well, thanks Paul!!

  2. Oscar Pedroza says:

    Excellent post Paul, great job…

  3. Great Post , Just 1 question, how can i get it to write to a txt file and e-mail it to a DL

  4. Paul,
    I am having a hard time figuring out how to send this via email. I’ve read your http://exchangeserverpro.com/powershell-how-to-send-email but still can’t get it to work right. Can you post a screen shot of the code with the email part in it?

  5. Hi Paul !
    I am one of your fans
    I made a translation to my language and put it on WIKI if you don’t mind
    and i referred it to this page at the end of the wiki post

    http://social.technet.microsoft.com/wiki/contents/articles/15413.exchange-fa-ir.aspx

  6. Hi,
    If there I get
    Role : Client Access Server Role
    RequiredServicesRunning : False

    How to make it to true?

    • Run the command on its own for that server (look at the first example in the article) and it will tell you which required services aren’t running.

  7. Hi Paul;
    Thanks for the great script works perfect.
    if you may need to ask my reports has generate only “Hub Transport Server Role Services status is Fail”
    is there something to do? or just ignore?
    thnx

    • Run Test-ServiceHealth on a Hub Transport that is reporting a fail and see what it says.

      My guess is EdgeSync isn’t running, which is harmless if you don’t have Edge servers, but will show as a fail in the report because the test cmdlet considers it a required service.

Leave a Comment

*

We are an Authorized DigiCert™ SSL Partner.