Q247557: IIS: How to Restart IIS Services by Using WSH with ADSI
Article: Q247557
Product(s): Internet Information Server
Version(s): 5.0
Operating System(s):
Keyword(s):
Last Modified: 13-MAY-2002
-------------------------------------------------------------------------------
The information in this article applies to:
- Microsoft Internet Information Services version 5.0
-------------------------------------------------------------------------------
SUMMARY
=======
Microsoft Windows Scripting Host (WSH) and Active Directory Services Interface
(ADSI) in Windows 2000 Server offer administrators new functionality through
scripting that was not possible previously. This article illustrates how these
two technologies can be used together to administer Internet Information
Services (IIS) 5.0.
MORE INFORMATION
================
Microsoft provides programming examples for illustration only, without warranty
either expressed or implied, including, but not limited to, the implied
warranties of merchantability and/or fitness for a particular purpose. This
article assumes that you are familiar with the programming language being
demonstrated and the tools used to create and debug procedures. Microsoft
support professionals can help explain the functionality of a particular
procedure, but they will not modify these examples to provide added
functionality or construct procedures to meet your specific needs. If you have
limited programming experience, you may want to contact a Microsoft Certified
Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more
information about Microsoft Certified Partners, please visit the following
Microsoft Web site:
http://www.microsoft.com/partner/referral/
For more information about the support options that are available and about how
to contact Microsoft, visit the following Microsoft Web site:
http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
Restarting Windows 2000 Services Using ADSI:
The following WSH code illustrates a generic example of how to use ADSI to start
a service when it is stopped.
1. Save the following code as "Startsvc.vbs" in the root of your C: drive:
' define a constant for stopped services
Const ADS_SERVICE_STOPPED = 1
' get an ADSI object for a computer
Set objComputer = GetObject("WinNT://MYCOMPUTER,computer")
' get an object for a service
Set objService = objComputer.GetObject("Service","MYSERVICE")
' check to see if the service is stopped
If (objService.Status = ADS_SERVICE_STOPPED) Then
' if the service is stopped, then start it
objService.Start
End If
2. Run the code by using the following command:
"CSCRIPT C:\STARTSVC.VBS" (without the quotation marks)
NOTES:
- You must replace MYCOMPUTER and MYSERVICE with a valid computer and service
name for the previous script to work. Use the short version of the service
name (for example, for the World Wide Web Publishing Service, use W3SVC).
- CScript.exe is located in the %SystemRoot%\system32 directory, but should be
in the PATH.
Restarting IIS Using ADSI:
The following WSH code is a more elaborate example of WSH and ADSI working
together to restart all of the IIS services.
1. Save the following code as "Restsvc.vbs" in the root of your C: drive:
Option Explicit
' define ADSI status constants
Const ADS_SERVICE_STOPPED = 1
Const ADS_SERVICE_START_PENDING = 2
Const ADS_SERVICE_STOP_PENDING = 3
Const ADS_SERVICE_RUNNING = 4
Const ADS_SERVICE_CONTINUE_PENDING = 5
Const ADS_SERVICE_PAUSE_PENDING = 6
Const ADS_SERVICE_PAUSED = 7
Const ADS_SERVICE_ERROR = 8
' define string constants for service methods
Const START_SERVICE = "START"
Const STOP_SERVICE = "STOP"
Const PAUSE_SERVICE = "PAUSE"
Const CONTINUE_SERVICE = "CONTINUE"
' declare global variables
Dim objWsh
Dim objEnv
Dim strComputerName
' get the environment variable with the computer name
Set objWsh = WScript.CreateObject("WScript.Shell")
Set objEnv = objWsh.Environment("PROCESS")
strComputerName = objEnv("COMPUTERNAME")
' call CycleService() to stop all the services
CycleService strComputerName,"MSFTPSVC",STOP_SERVICE,True
CycleService strComputerName,"SMTPSVC" ,STOP_SERVICE,True
CycleService strComputerName,"NNTPSVC" ,STOP_SERVICE,True
CycleService strComputerName,"W3SVC" ,STOP_SERVICE,True
CycleService strComputerName,"IISADMIN",STOP_SERVICE,True
' call CycleService() to start all the services
CycleService strComputerName,"IISADMIN",START_SERVICE,True
CycleService strComputerName,"W3SVC" ,START_SERVICE,True
CycleService strComputerName,"NNTPSVC" ,START_SERVICE,True
CycleService strComputerName,"SMTPSVC" ,START_SERVICE,True
CycleService strComputerName,"MSFTPSVC",START_SERVICE,True
' ****************************************
' CycleService() subroutine
' this subroutine is passed four variables:
' 1. strComputer = the name of the computer
' 2. strService = the name of the service (e.g. w3svc, smtpsvc, etc.)
' 3. strOperation = the operation to be completed (e.g. start, stop)
' 4. boolTrace = True will output trace information, False will not
' ****************************************
Sub CycleService(strComputer,strService,strOperation,boolTrace)
On Error Resume Next
' declare variables
Dim objComputer
Dim objService
Dim strTrace
Dim boolSuccess
' get ADSI objects and initial variables
Set objComputer = GetObject("WinNT://" & strComputer & ",computer")
Set objService = objComputer.GetObject("Service",strService)
strTrace = strOperation & " " & strService & " on " & strComputer
boolSuccess = False
' output trace information if needed
If boolTrace Then Trace "Attempting to " & strTrace & "..."
' determine the operation and carry it out
Select Case (strOperation)
Case START_SERVICE
If (objService.Status = ADS_SERVICE_STOPPED) Then
objService.Start
If Err.Number<>0 Then ErrorHandler strTrace
While objService.Status <> ADS_SERVICE_RUNNING: Wend
boolSuccess = True
End If
Case STOP_SERVICE
If (objService.Status = ADS_SERVICE_RUNNING) Or (objService.Status = ADS_SERVICE_PAUSED) Then
objService.Stop
If Err.Number<>0 Then ErrorHandler strTrace
While objService.Status <> ADS_SERVICE_STOPPED: Wend
boolSuccess = True
End If
Case PAUSE_SERVICE
If (objService.Status = ADS_SERVICE_RUNNING) Then
objService.Pause
If Err.Number<>0 Then ErrorHandler strTrace
While objService.Status <> ADS_SERVICE_PAUSED: Wend
boolSuccess = True
End If
Case CONTINUE_SERVICE
If (objService.Status = ADS_SERVICE_PAUSED) Then
objService.Continue
If Err.Number<>0 Then ErrorHandler strTrace
While objService.Status <> ADS_SERVICE_RUNNING: Wend
boolSuccess = True
End If
End Select
' output trace information if needed
If boolTrace And boolSuccess Then Trace strTrace & " was successful."
End Sub
' ****************************************
' Trace() subroutine
' outputs time and trace information
' ****************************************
Sub Trace(strText)
WScript.Echo Now & " : " & strText
End Sub
' ****************************************
' ErrorHandler() subroutine
' outputs error status and exits
' ****************************************
Sub ErrorHandler(strText)
Dim strError
strError = Now & " : The following error occurred trying to " & strText & vbCrLf
strError = strError & vbCrLf & "0x" & Hex(Err.Number) & " - " & Err.Description
WScript.Echo strError
WScript.Quit
End Sub
2. Run the code with the following command:
"CSCRIPT C:\RESTSVC.VBS" (without the quotation marks)
When you use the CycleService function, only stop the services that are
installed on the computer. You will receive an "object required" error
message when you try to stop or start a service that is not installed on the
computer.
3. Stop the services first that depend on the service you are trying to stop.
For example, you must stop W3SVC, MSFTPSVC and SMTPSVC first before you stop
IISADMIN.
4. When you execute the code, the output should be similar to the following:
12/31/1999 12:00:00 PM : Attempting to STOP MSFTPSVC on MYCOMPUTER...
12/31/1999 12:00:00 PM : STOP MSFTPSVC on MYCOMPUTER was successful.
12/31/1999 12:00:00 PM : Attempting to STOP SMTPSVC on MYCOMPUTER...
12/31/1999 12:00:01 PM : STOP SMTPSVC on MYCOMPUTER was successful.
12/31/1999 12:00:01 PM : Attempting to STOP W3SVC on MYCOMPUTER...
12/31/1999 12:00:02 PM : STOP W3SVC on MYCOMPUTER was successful.
etc.
More information on Microsoft's scripting technologies can be found at
http://msdn.microsoft.com/scripting/
Additional query words:
======================================================================
Keywords :
Technology : kbiisSearch kbiis500
Version : :5.0
Issue type : kbhowto
=============================================================================
THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.
Copyright Microsoft Corporation 1986-2002.