PowerShell Ezine, Logon Scripts

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!


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


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.

ˆ

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.

Instructions

  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 http://computerperformance.co.uk
' 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
     objService.StopService()
     Next
Next
WScript.Echo "Now check: Services, MS Exchange xx" & vbCr _
& "This script cuts the time it takes to reboot your Exchange server."

WScript.Quit

' 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

ˇ

Challenges

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.

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

Their topics and material are ideal for getting you started with VBScript.  The videos are easy to follow and you can control the pace.  Try their free demo material and then see if you want to buy the full package. See more about VB Script Training CD.


 *


Google

WebComputerperformance.co.uk

GFi Events Manager

Guy Recommends: GFi EventsManager

Here is a solution to monitor, manage and archive thousands of events that are generated by devices across your entire network.  Get your free evaluation copy of GFI EventsManager.

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.