PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 68 - Restart Services

Contents for Ezine 68 - Restart Services

This week’s secret

In the 1970's I had a career, or a 'life' in research.  When it came to producing a report or a thesis it was commonplace for we students to write 100s of pages.  My mate Rod showed me his thesis and to my amazement, it was only 1 page long.  I was even more surprised when his professor said it was the finest piece of work that he had ever seen.  What appealed to the professor was the script's clarity and simplicity. Rod had written an original mathematical proof of a theorem that had never been solved before - all on one page.

What has Rod's one page thesis got to do with scripting?  Well, last week I asked if anyone knew of utility similar to iisreset which restarts IIS, Hans K wrote in saying, 'Try Net Stop, Net Start'.  I have taken Hans's simple but brilliant idea and made it into a script to restart any Windows service.


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


This week's Mission to restart Windows Services

This week I have yet another script that works at several levels. The practical goal is to stop and then restart Windows services such as, Net Logon, IPSec or System Attendant. Meanwhile, the scripting goals include mastering 'SendKeys' and creating shell objects, (rather than the usual network objects).

Preamble

When ever I experiment with scripts that affect the operating system, my fear is that the script will run out of control and shutdown a vital service.  So in the testing phase, I choose services that will have minimal affect if anything goes wrong, for example, Alerter or Messenger, (rather than Net Logon or System Attendant).

Two Tips:

1) Try Tasklist from the command prompt.  On any of the XP family, Tasklist enumerates the names of services running on that computer.

2) Investigate the Services snap-in with a view to identifying other services that you could restart. Only last week, I needed to start Remote Procedure Call (RPC) Locator. Before that I need a script to control Telnet.

ˆ

Example 1 Stop the Service

Here is your script to stop a Windows service.  This example uses Net Stop to halt the Alerter service.

Instructions to Stop a Service

  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. Services.vbs
  3. Double click and then watch a command prompt open and the script stop the Alerter service.
  4. The script will then close the cmd prompt and display a message.

 

 

' Services.vbs
' Example VBScript to Stop Services
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.3 - March 20th 2005
' -----------------------------------------------------------------'
Option Explicit
Dim objShell, intShortSleep, intLongSleep
Dim strService
Set objShell = CreateObject("WScript.Shell")

' Values set
strService = " Alerter"
intShortSleep = 1500
intLongSleep = 3500

' Cmd prompt opened
objShell.Run "cmd"
Wscript.Sleep intShortSleep

' Service stopped with 'Net' command
objShell.SendKeys "net stop" & strService
Wscript.Sleep intShortSleep
objShell.SendKeys "{Enter}"
Wscript.Sleep intLongSleep

' Cmd prompt exited
objShell.SendKeys "Exit"
Wscript.Sleep intShortSleep
objShell.SendKeys "{Enter}"

Wscript.Echo strService & " service stopped "
WScript.Quit

' End of Example VBScript

Learning Points

Note 1:  What we need here is a shell object to hold the command prompt.  See Line 9:  Set objShell = CreateObject("WScript.Shell")

Note 2:  The star amongst the scripting command is SendKeys.  I expect that you have seen how it mimics you typing from the command line.

Note 3:  From the operating system's point of view, the vital command is net stop.

Challenge: Substitute another service for Alerter.

ˇ

Example 2 Start the Service

The idea here is to build a script which is the mirror image of Example 1, namely to start the service.  To achieve this goal just alter these two lines.

 


             ' Service stopped with 'Net' command
Line 21:  objShell.SendKeys "net stop" & strService ' Change to NET START

Line 31:   Wscript.Echo strService & " service STARTED "

Learning Points

Note 1: Rather than typing out the whole script, I challenge you to change two lines.

Note 2: The key point is to reverse the net stop command to NET START.

Example 3 Restart the Service

This script combines Example 1 and 2 to create a script which restarts the Windows Alerter service.

 

' RestartServices.vbs
' Example VBScript to Restart Services
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.3 - March 20th 2005
' -----------------------------------------------------------------'
Option Explicit
Dim objShell, intShortSleep, intLongSleep
Dim strService
Set objShell = CreateObject("WScript.Shell")

' Values set
strService = " Alerter"
intShortSleep = 1500
intLongSleep = 5500

' Cmd prompt opened
objShell.Run "cmd"
Wscript.Sleep intShortSleep

' Service stopped with 'Net' command
objShell.SendKeys "net stop" & strService
Wscript.Sleep intShortSleep
objShell.SendKeys "{Enter}"
Wscript.Sleep intLongSleep

' Service started with 'Net' command
objShell.SendKeys "net start" & strService
Wscript.Sleep intShortSleep
objShell.SendKeys "{Enter}"
Wscript.Sleep intLongSleep

' Cmd prompt exited
objShell.SendKeys "Exit"
Wscript.Sleep intShortSleep
objShell.SendKeys "{Enter}"

Wscript.Echo strService & " service stopped "
WScript.Quit

' End of example VBScript

Learning Points

Note 1: I have increased the sleep time to 5500, which is about 5 seconds.  You may wish to increase this value to ensure that the service has stopped before the script attempts a net start.

Note 2: If the service is not running, the script will initially produce an error, but then go on to start that service.  This effect may or may not be what you desire.

Scripting Challenge:  Introduce a Msgbox to prompt for the name of the service you wish to restart.

Windows Challenge:  Investigate other services, which you could start and stop with a VBScript.  To get ideas, open a command prompt and type: tasklist.   Then switch to the Services snap-in for other likely services to restart.

See more examples of how to stop and start services using WMI Commands.

Readers Letters

If you remember, one of last week's challenges was to discover the OU to which a user belonged.  In addition to Hans, I was so pleased that two more people wrote in with different ideas about how to find a users OU without scripting.

Brian pointed out that in Active Directory Users and Computers, if you View, Advanced Features, then the OU information is available on the Object tab.

Mike G wrote in with a neat use of Add Remove Columns.  If you 'Find' an object in Active Directory, it is possible to adjust the columns to display 'Published At'.

See here for diagrams.

Summary - Restart Services

Here is a handy script to restart your Windows services.  The main points are creating a shell object and employing SendKeys to mimic typing from the keyboard.

See here for similar techniques using WMI scripts.

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.