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.

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.

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.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

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 https://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.

Guy Recommends SolarWinds’ Free Network MonitorSolarwinds Network Device Monitor

Thus utility makes it easy to check the health of a router or firewall.  Check the real-time performance, and availability statistics, for any device on your network.  Get started with an extensive collection of "out-of-the-box" monitors for popular network devices. Give Network Monitor a whirl – it’s free. Download your free Network Device Monitor

If you need more comprehensive network analysis software:
Download a free trial of NPM (Network Performance Monitor)

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 https://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.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell v 3.0

PowerShell 3.0  • What’s New in PowerShell 3.0  • PowerShell 3.0 Foreach-Object

PowerShell Show-Command  • Out-GridView -PassThru  • PowerShell Ordered Hash Tables

PowerShell Home  • PowerShell 3.0 Get-ChildItem  • PowerShell 3 -NotIn  • PowerShell 3.0 Where