Contents for Ezine 99 Restarting Services
- This Week’s Secret
- This Week’s Mission – Restart Services
- This Week’s Scripting Mission – Variables and Sub Routines
- Example: To Restart the Windows Time Service
- PowerShell Restart Service
- Summary – Restarting the Time Service
It’s an open secret that scripters need to be multi-talented. Not only do we need knowledge of VBScript, but also understanding of the operating system’s mechanics. A generous dose of logic is also useful for greasing the scripting wheels.
As ever,I want to combine a script that is useful in its own right, with code that can be adapted to other scenarios. This week’s script, which restarts Windows services fits the bill perfectly.
As you may know, I use the OnScript editor for my own scripts. In February 2006 OnScript introduced a new affiliate program which gives me a % of the sale rather than a fixed price for advertising in this ezine. To get an evaluation copy use any of my links, then press the ‘Back’ button once you reach OnScript’s site.
As a matter of tactics, it’s much more satisfying to cure a problem by restarting a service than by rebooting the machine. Not only is restarting the service faster and less disruptive for users, but also it teaches you to pinpoint the problem.
I want to introduce you to the benefits of variables in scripts. I also want to explain how I employ an adaptation of the Hungarian Variable system in my scripts. Most people find a good script and then uses it with minimal thought. My mission is to take you to the next level where you know what to look for in a script, thus you can make sensible adjustments, even improvements to suit your circumstances.
I realize that using time as the script’s vehicle for the third week, may seem tedious to some. However, the other side of the coin is that familiarity makes the scripts easier to concentrate on the new methods. Additionally, there is a sense of building on success, which is not to be underestimated when writing scripts.
I have flirted with scripting languages that were very strict in declaring variables, in comparison VBScript is pure anarchy. For a start, you don’t have to declare variables at all. If you do declare variables, then you can use any reasonable combinations of letters and numbers; for example, a string variable could be strServer, sServer, or you need not start with ‘s’ at all.
Children don’t like anarchy, and so it is with scripters. My advice is to impose rules when you name variables, but make them your rules not mine. The concept behind the Hungarian system for naming variables is that the first letters give you a clue whether the variable will be used for a string, a number, a time or other category of object. For example, strTimeSrv (string) intSleepLong (integer number). You may notice that my scripts invariably begin with the seemingly inconsequential, Option Explicit. This forces me to declare my variables, the benefit is that Option Explicit warns me if (when) I make a typo later in the script.
Guy Recommends: The Free IP Address Tracker (IPAT)
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
This Week’s Scripting Mission – Sub Routine
Like a footnote in a book, I have added the restart code as sub routine at the end of the script. Normally subroutines are used where you need to recycle the same code for multiple tasks. Thinking ahead to more complex scripts, there are time and space advantages to repeating a routine. This is my reasoning for familiarizing you with the Sub Routine syntax and the ‘Call Routine()’ command.
Two Purposes of the Script
The main learning point in this example is how to script a restart of the Windows Time Service. Incidentally, if you just change the value for strService, then you can adapt the script to restart other services. I advise you to begin, not with the code, but in the Administrative Tools, Services snap-in. What you are looking for is the precise name of the service that you wish to restart, for example is the time service called, W32tm, Windows Time or W32Time? To my surprise, the answer was W32Time.
The secondary learning point is to synchronize with a time server. From a computing perspective, the script sets the time server, then it restarts the service, only then does it synchronize with the newly configured internet time source.
Instructions for Restarting the Time Service
This script is designed for Windows servers, but there is no reason why it should not work on an XP machine. If you use uk.pool.ntp.org or time-a.nist.gov as the time server, make sure that your machine has an internet connection.
Copy and paste the example script below into notepad or use a VBScript editor.
One advantage of a good script editor such as OnScript is that you can see the line numbers, which helps when you have to troubleshoot error messages.
Save the file with a .vbs extension, for example: RestartTime.vbs
Double click RestartTime.vbs, observe the cmd session and check the message box.
‘ VBScript Source File — Created with XLnow OnScript
‘ AUTHOR: Guy Thomas
‘ COMPANY: Computer Performance
‘ DATE: January 2006 Version 2.2
‘ COMMENT: Script to Restart the Time service
Dim intShortSleep, intLongSleep, strService
Set objShell = CreateObject("WScript.Shell")
strService = "w32Time"
intShortSleep = 500
intLongSleep = 8000 ‘1000 = 1 second
‘ Time Server set (Remove ‘ Rem if you want to change)
‘strTimeSrv = "time-a.nist.gov"
strTimeSrv = "uk.pool.ntp.org"
‘ Cmd prompt opened with .Run method
‘ set the time server. Equivalent of net time /setsntp
objShell.SendKeys "w32tm /config /syncfromflags:manual /manualpeerlist:"_
‘ Point where the script synchronizes
objShell.SendKeys "w32Tm /resync /rediscover"
‘ Cmd prompt exited
‘ Restart Service
objShell.SendKeys "net stop " & strService
objShell.SendKeys "net start " & strService
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.
VBScript Tutorial – Learning Points
Note 1: Remember how scripts mimic commands that you can issue manually; in this case ‘net stop serviceName’ and ‘net start serviceName’
Note 2: Option Explicit forces me to declare all my variables. This is why I have Dim statements at the top of my scripts, to dimension variables such as strService.
Note 3: Pay close attention to how the script constructs the routine: Sub Restart(). This is not difficult, but you need to obey all the rules. Start with the specific word: Sub. Follow sub with the name of the routine and then end with brackets(). When you need to insert the routine into the main script, the command is Call Restart(). These are two common mistakes :a) Call Sub Restart() or b) Call Restart.
Note 4: Check the logic of the script, namely to:
i) Set the Time Server
ii) Restart the Windows Time Server
iii) Only now do we, resync \ rediscover.
- Experiment with longer values for intLongSleep = 8000. At 8 seconds, I may have left the value on the short side.
- Try removing the net stop command.
- Give the script a real job by manually stopping the Windows Time service before running the script.
- Seek other Services that you could restart, then strip down the script and adapt it to a service such as Alerter or Messenger.
This script fits well with my master plan of building a script in stages. The secret for easy learning is get the basic section working then bolt on other routines. Another of my hobby-horses is that scripters have to be multi talented. For this script you need knowledge of the Windows Services, the Net Stop command, topped off with a dose of logical thinking about the sequence of the operating system commands.
See more about VBScript