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.

Using Test-ServiceHealth for Exchange Server Health Checks

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.

[adrotate banner=”48″]

About the Author

Paul Cunningham

Paul is a former Microsoft MVP for Office Apps and Services. He works as a consultant, writer, and trainer specializing in Office 365 and Exchange Server. Paul no longer writes for Practical365.com.

Comments

  1. Dann

    Thank you Paul. very useful

  2. Syed

    Thank you PAUL!!! I LOVE YOU!! Needed to see which exact service was not running, so glad for this.

  3. Wayne

    The .\Test-ExchangeServerHealth.ps1 script works quite well, but we do not have MSExchangeEdgeSync service running. This causes “Hub Transport Server Role” test failed. Is there a way to get around it?

    Thanks

    1. Avatar photo
      Paul Cunningham

      Test-ServiceHealth is expect it to be running. In the past I’ve simply enabled and started the service.

  4. David

    Thanks for sharing this. I have a question, how do you check if all the CAS servers are accepting outlook client connections? We have in the past where all the services are running fine and server health is okay but the CAS server just doesn’t accept the Outlook connections.

    Thank you.
    David

  5. Gordon

    Hello,
    I would like to achieve the following and need some guidance. I would like to run a scheduled task to run the test-servicehealth but only email out to a DL when a required service is in a failed state.
    Could you help to add the necessary code to achieve this?

    Thanking you in advance.

    Gordon

  6. Krish

    HI Paul,

    Thanks for Providing this Wonderful Script. But can you please help me with starting the failed services, after the script shows failed services in red?

    Appreciate your help

    Thanks
    Krish

    1. Avatar photo
      Paul Cunningham

      There’s a Start-Service cmdlet in PowerShell, or you can use the Services.msc console.

      1. Krish

        Yes. But i was asking you, where i can place Start-Service cmdlet in your script.

        So that if the loop checks and shows as “services not running” then it will start the required service automatically.

        It would be a great help , if you can help me with this.

        Thanks

        1. Avatar photo
          Paul Cunningham

          You could add it to the section of the script that is running Test-ServiceHealth and processing the results. But I don’t really recommend that. The script can’t tell the difference between a service that is stopped due to an error, and one that is stopped on purpose due to maintenance. I recommend you let the report flag problems to you for manual attention. If you have lots of service failures you need to address the root cause of those.

          1. krish

            Got you Paul. Thanks a lot.

            Best Regards,
            Krish

  7. O.R

    Hi Paul,
    this is a great script. Is there any fix for the EdgeSync failure in report available ?
    Thanks a lot

  8. art

    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

    1. Avatar photo
      Paul Cunningham

      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.

  9. Paul

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

    How to make it to true?

    1. Paul

      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.

    1. Ryan Shaw

      Paul,

      I am too curious to know how to get the last script sent in an email.

  10. Mo

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

      1. Mo

        Thanks Paul

  11. Oscar Pedroza

    Excellent post Paul, great job…

  12. Kunal

    Worked well, thanks Paul!!

    1. razzak

      Excellent , can somebody send me this script .

Leave a Reply