다음을 통해 공유


Useful Script Number 1 - Start and Stop Services

So over the next few weeks, I will be presenting a mini series of scripts that may useful in your deployments. All of the scripts will be BDD/MDT specific - meaning that they can be run from the task sequencer and will have the correct format for using the BDD/MDT framework for logging/utilities etc.

The first script in this series is one for starting and stopping services - originally I created this script because I needed to stop and start the SMS service so that SMS auto site allocation could take place, but then my colleague Richard Trusson suggested that I build this into something that is more flexible....

The code looks like this (cut and paste into a new file called zCFG-Services.wsf and place in your Deployment Point\Scripts Directory):

<job id="zCFG-Services">
<script language="VBScript" src="ZTIUtility.vbs"/>
<script language="VBScript">
' //***************************************************************************
' // ***** Script Header *****
' //
' // Solution: Solution Accelerator for Business Desktop Deployment/Microsoft Deployment
' // File: zCFG-Services.wsf
' //
' // Purpose: Starts or stops services based on command line entries
' //
' // Usage: cscript zCFG-Services.wsf [/service:ServiceName] [/state:Start or Stop] [/debug:true]
' // NOTE: /service:ServiceName is case sensitive. /state: can be Start or Stop only
' //
' // Microsoft Solution Version: NA
' // Microsoft Script Version: NA
' //
' // Microsoft History:
' //
' // Custom History:
' // 1.0.0 RAS 29/01/08 Starts and stops services as specified on the command line.
' //
' //
' // ***** End Header *****
' //***************************************************************************

'//----------------------------------------------------------------------------
'//
'// Global constant and variable declarations
'//
'//----------------------------------------------------------------------------

Option Explicit
Dim iRetVal

'//----------------------------------------------------------------------------
'// End declarations
'//----------------------------------------------------------------------------

'//----------------------------------------------------------------------------
'// Main routine
'//----------------------------------------------------------------------------

'On Error Resume Next
iRetVal = ZTIProcess
ProcessResults iRetVal
On Error Goto 0

'//---------------------------------------------------------------------------
'//
'// Function: ZTIProcess()
'//
'// Input: None
'//
'// Return:   Success - 0 Failure - non-zero
'//
'// Purpose: Perform main ZTI processing
'//
'//---------------------------------------------------------------------------
Function ZTIProcess()

Dim objWMIService, objItem, objService
Dim colListOfServices, strComputer, strService, strState, strSName, objExecute
ZTIProcess=1

oLogging.CreateEntry ": Starting Actions ************************************************************ ",LogTypeInfo

strSName=oUtility.ScriptName
strComputer = "."
strService = oUtility.Arguments("service")
'Note: strService is case sensitive.
strState = oUtility.Arguments("state")
'Note: strState must be Start or Stop

If strService="" Then
oLogging.CreateEntry strSName & ": No service was specified on command line.",LogTypeError
ZTIProcess=90
Exit Function
Else
oLogging.CreateEntry strSName & ": Service that will be actioned has been set to: " & strService,LogTypeInfo
End if

If strState="" Then
oLogging.CreateEntry strSName & ": No state was specified on command line.",LogTypeError
ZTIProcess=90
Exit Function
Else
oLogging.CreateEntry strSName & ": State of specified service will be set to: " & strState,LogTypeInfo
End if

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name = '"&strService&"'")
oLogging.CreateEntry strSName & ": Number of services identified as " & strService & " = " &colListOfServices.count,LogTypeInfo

For Each objService in colListOfServices
objExecute = "objService." & strState & "Service()"
oLogging.CreateEntry strSName & ": Executing " & strState & " of service: " & strService,LogTypeInfo
execute(objExecute)
If Err<>0 Then
oLogging.CreateEntry strSName & ": Failed to carry out " & strState & " of service: " & strService & " error code = " & Err,LogTypeError
ZTIProcess=50
Exit Function
Else
oLogging.CreateEntry strSName & ": Service: " & strService & " " & strState & "ed successfully",LogTypeInfo
End If

Next
oLogging.CreateEntry ": Completed Actions ************************************************************ ",LogTypeInfo
ZTIProcess = 0
End Function

    </script>
</job>

The script takes two inputs on the command line - /service:ServiceName and /state:Start or Stop

The input /service: is case sensitive and needs to match exactly the service as shown on the properties page for the service-for example the service name for the SMS agent is CcmExec so the input would be /service:CcmExec

image

 

The input /state: sets the state of the service and can be either Start or Stop, meaning that you can use the script multiple times in the task sequencer to set service state as required.

The script is then run at the required place in the task sequencer by adding a task and specifying the following as the command line:

cscript.exe "%SCRIPTROOT%\zCFG-Services.wsf" /service:CcmExec /state:Stop

Later in the task sequence, you can use the same script to re-start the service by creating a new task and specifying the following as the command line:

cscript.exe "%SCRIPTROOT%\zCFG-Services.wsf" /service:CcmExec /state:Start

This post was contributed by Richard Smith a Senior Consultant with Microsoft Services, UK.