• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 9. Scripting and Automation > Managing Services with Scripts

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


PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint