Guy’s Scripting Ezine 65 – Exchange Services

Contents for Ezine 65 – Shutdown Exchange Services


This Week’s Secret

Have you noticed how every mother truly believes that their own baby is the most beautiful newborn in the world?  Well that is how I feel about this week’s script.  However, unlike many of those mothers, I realize that you may not share the view that my script has a beautiful WMI body, a pretty select case and charming for… each loop!

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Mission to shutdown Exchange 2003 quickly

In keeping with recent ezines, this week’s script works at two levels, it has a clear mission, and is full of handy scripting devices.  My mission is to reduce the time it takes to shutdown or reboot an Exchange 2003 server. The learning points are to apply the Select Case statement and to persuade WMI* to stop the operating system’s services.

The script’s purpose is to stop just Exchange Services. Then, when you shutdown or reboot the Exchange Server the operation will be much quicker.  Have you noticed that shutting down Exchange 2003 on a Domain controller takes over 10 minutes longer than a normal Windows 2003 shutdown?  This is most noticeable in test domains where there is only one DC and Global Catalog server.  This script is ideal for that test Exchange 2003 server.

* WMI – Windows Management Instrumentation, a VBScript method to ‘hook’ into the operating system.

Exchange Monitor from SolarWindsGuy Recommends: The SolarWinds Exchange Monitor

Here is a free tool to monitor your Exchange Server.  Download and install the utility, then inspect your mail queues, monitor the Exchange server’s memory, confirm there is enough disk space and check the CPU utilization.

This is the real deal – there is no catch.  SolarWinds provides this fully-functioning freebie, as part of their commitment to supporting the network management community.

Free Download of SolarWinds Exchange Monitor

Example – To Shutdown Exchange 200x server

Firstly, do not get carried away with pure scripting goals.  Realize that my script will actually shutdown your Exchange services.  Therefore, either run it on test server, or change the values for strServiceX to relatively harmless services like Messenger or BITS.


  1. Copy and paste the script below into notepad. Alternatively, get a trial copy of OnScript.
  2. Save the file with .vbs extension e.g. StopService.vbs
  3. Double click and then check the message box.
  4. Check the Services.
  5. Note: This script does not shutdown the server, so either reboot manually or add extra code as explained in the Challenges.
  6. Note: If you do not have an Exchange 200x server, then alter the strServices to Alerter, BITS or other suitable services.

‘ StopService.vbs
‘ Author Guy Thomas
‘ Version 2.6 – February 27th 2005
‘ —————————————-‘

Option Explicit
Dim objService, objWMI, colListOfServices, intExSrv
Dim strService1, strService2, strService3, strService4
Dim strComp, strWMIsrv

‘  Amend strService if you do not have exchange e.g. Alerter
strService1 = "MSexchangeIS"
strService2 = "MSexchangeMTA"
strService3 = "MSexchangeSA"
strService4 = "MSexchangeSRS"
intExSrv = 1

‘ Loops through all the Exchange services
‘ Uses Select Case rather than If then Elseif, end if.
For intExSrv = 1 To 4
   Select Case intExSrv
      Case 1 strWMIsrv = strService1
      Case 2 strWMIsrv = strService2
      Case 3 strWMIsrv = strService3
      Case 4 strWMIsrv = strService4
   End Select

WScript.Echo "strWMIsrv = " & strWMIsrv

‘ Section using WMI to interrogate the services
‘ Note "." means local computer.
‘ Note strWMIsrv in Line 37 (ish)

strComp = "."
Set objWMI = GetObject("winmgmts:\\" & strComp & "\root\cimv2")
Set colListOfServices = objWMI.ExecQuery("Select * from " _
& "Win32_Service Where Name =’" & strWMIsrv & "’")

‘  See how it cycles through colListofServices until it gets a match
‘  Then stops the objService
     For Each objService in colListOfServices
WScript.Echo "Now check: Services, MS Exchange xx" & vbCr _
& "This script cuts the time it takes to reboot your Exchange server."


‘ End of example VBScript

Learning Points

Note 1:  I am fond of Select Case, where ever possible I use this construction in preference to endless: if, then, elseif, elseif, elseif, endif. 

Note 2: The heart of the script is GetObject("winmgmts…)  Here is where VBScript employs WMI to manipulate the operating system.

Note 3: Many of these WMI scripts use collations. (Like a collection.)  Observe the:  For Each objService in colListOfServices


Amend the script to start services.  Starting the services is fun for testing, useful in production.  Example, change line 41 (ish) objService.StartService()

Add a shutdown command at the end of the script, for example:
shutdown /l /t:20 /r  (Lower case L means local, t means time delay in seconds, r means reboot).  See more in Ezine 45 shutdown command

Summary – Exchange Services

Perhaps you have noticed that Exchange 2003 takes much longer to shutdown than SQL or even Windows 2003? This effect is most noticeable where Exchange is on a Domain Controller.  The trick is to shutdown the Exchange Services before you shutdown the Windows services.  We achieve this mission with a classic VBScript.

See more about shutdown instructions.

Registry  • Registry Win8  •PowerShell Registry  • Ezine 45 Shutdown  • Ezines

Ezine 135 Shutdown  •Ezine 65 Shutdown Exchange   •Free NCM Tool

Ezine 64 Write Registry  • Tool Kit  •Free Solarwinds IP Tracker