Windows XP Annoyances Free Open Book

Windows XP Annoyances

Previous PageNext Page

9.9 Managing Services with Scripts

Windows XP Services, such as the IIS web server service, the FTP daemon service, or the Remote Desktop service, can be managed with the Services MMC snap-in (discussed in Chapter 8). Rudimentary service control is also possible with WSH scripts. The following routine allows you to start and stop any service, or just see if a service is running:

Function Service(ServiceName, Action)
  Const SERVICE_STOPPED = 1
  Const SERVICE_RUNNING = 4
  Set WshShell = WScript.CreateObject("WScript.Shell")
  Set EnvObject = WshShell.Environment("PROCESS")
  ComputerName = EnvObject("COMPUTERNAME")
  Set ComputerObject = GetObject("WinNT://" & ComputerName & ",computer")
  Set ServiceObject  = ComputerObject.GetObject("Service",ServiceName)
  If Action = 1 and ServiceObject.Status = SERVICE_STOPPED Then
    ServiceObject.Start
  ElseIf Action = 2 and ServiceObject.Status = SERVICE_RUNNING Then
    ServiceObject.Stop
  End If
  If ServiceObject.Status = SERVICE_RUNNING Then
    Service = True
  Else
    Service = False
  End If
End Function

This general-purpose routine accepts two parameters: ServiceName and Action. ServiceName is a single word that represents the service you wish to start, stop, or query, and Action is just a number, representing what you want the routine to do. To find the service name for a given service, open the Services window (services.msc) and double-click the service in question. The service name is listed at the top of the General tab; for example, the service name for the IIS service is IISADMIN, the name for the FTP service is MSFTPSVC, and the name for the Remote Desktop (a.k.a. Terminal Services) service is TermService.

So, to start the FTP service, you would type:

Result = Service("MSFTPSVC", 1)

or, to stop the service, you would type:

Result = Service("MSFTPSVC", 2)

Either way, the function returns True (-1) if your action resulted in the service being started, or False (0) if your action resulted in the service being stopped. To simply query the service, without starting or stopping it, specify any other number for Action, like this:

Result = Service("MSFTPSVC", 0)

Including this routine in your script would allow you to start and stop a service with a single click (rather than having to wade through the Services window). Or, using these script routines in conjunction with Scheduled Tasks (explained later in this chapter), for example, you could schedule your web server service to operate only during certain hours of the day. See Chapter 8 for more information on Services and the Microsoft Management Console.

Previous PageNext Page


     Main Menu
Cover
Copyright
Preface
Chapter 1. Making the Most of Windows XP
Chapter 2. Basic Explorer Coping Skills
Chapter 3. The Registry
Chapter 4. Tinkering Techniques
Chapter 5. Maximizing Performance
Chapter 6. Troubleshooting
Chapter 7. Networking and Internetworking
Chapter 8. User Accounts and Administration
Chapter 9. Scripting and Automation
9.1 Building a Script with VBScript
9.2 Running Applications from Scripts
9.3 Accessing the Registry from Scripts
9.4 Manipulating Files from Scripts
9.5 Creating Windows Shortcuts and Internet Shortcuts in Scripts
9.6 Networking with Scripts
9.7 Manipulating Internet Explorer from Scripts
9.8 Using Command-Line Parameters in Scripts
9.9 Managing Services with Scripts
9.10 Writing CGI Scripts for a Web Server
9.11 Making a Startup Script
9.12 Deciphering Script Errors
9.13 Finding a Better Editor
9.14 Further Study
9.15 Automating Scripts with Scheduled Tasks
9.16 Wacky Script Ideas
Chapter 10. Installing Windows XP
Appendix A. Setting Locator
Appendix B. BIOS Settings
Appendix C. Command Prompt Crash Course
Appendix D. TCP/IP Ports
Appendix E. Error Messages (Blue Screen of Death)
Colophon


More Books
PHP Hacks
Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
The Koran (Holy Qur'an)
Macromedia Flash 8 Bible
Search Engine Optimization for Dummies
YouTube Traffic
PHP 5 for Dummies
Harry Potter and The Chamber of Secrets
Harry Potter and the Sorcerer's Stone
The Pilgrim's Progress
Wireless Hacks
Flash Hacks. 100 Industrial-Strength Tips & Tools
PayPal Hacks. 100 Industrial-Strength Tips and Tools
Amazon Hacks
Pdf Hacks
The Da Vinci Code
Google Hacks
The Holy Bible
Windows XP For Dummies
Harry Potter and the Half-Blood Prince
Seo Book
Upgrading and Repairing Networks
Macromedia Dreamweaver 8 UNLEASHED
Windows XP Annoyances
Windows XP Hacks
Microsoft Windows XP Power Toolkit
Teach Yourself MS Office In 24Hours
iPod & iTunes Missing Manual
PC Hacks 100 Industrial-Strength Tips and Tools
PC Overclocking, Optimization, and Tuning - 2th Edition
PC Hardware In A Nutshell 3rd Edition
PC Hardware in a Nutshell, 2nd Edition
Upgrading and Repairing PCs
Google for Dummies
MySQL Cookbook
Teach Yourself Macromedia Flash 8 In 24 Hours
PHP CookBook
Sams Teach Yourself JavaScript in 24 Hours
PHP5 Manual
Free Games Paper Airplanes
500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele