Visual basic script to start/stop any Windows service(s) in any server(s)
Well, i have been improving this script in several post and I think this is the most useful versión.
Functionalities
· Start a windows Service
· Stop a windows Service
· Do any of this actions in several services using Wildcards expressions
· Do any of this actions in 1 to n Servers.
Within the code you can have a look at the usage section to learn how to use it.
Requirements
· The user running the script should be local Administrator and have rights to stop the service you want to stop
· WMI and scripting execution rights (especially when you select to start or stop services in remote machines)
Usage
The script will return 0 IF ALL THE ACTIONS finish successfully and 1 if just on error found.
If an error is raised, the script will no continue.
Cscript ActServices.vbs ACTION;SERVICES;SERVERNAMEA[;SERVERB] [;SERVERC] [;SERVERN]
Parameters
ACTION START / STOP
SERVICES Service name to start or stop. You can use wildcards here like %
BTSSVC% will act over the services called BTSSVC*
SERVERNAME à LIST OF SERVERS separated by ;
Examples:
· This will stop all BizTalk Services in localhost:
cscript ".\ActServices.vbs" START;'BTSSvc%%';.
· This will stop MSE service in local host:
sscript ".\ActServices.vbs" START;'Microsoft.ManagedSolutions.Mse%%';.
Output
· The script will quit with ResultCode of 0 if everything went well and 1 in case of error
· It will also show all the actions taken:
The code
The ActServices.vbs script
Just copy and paste this code into a file called ActServices.vbs :
' VBScript ActBizTalkServerInstance.vbs
' Sample script to Stop Or Start ALL wildcard Services, it will return 0 if no errors found, 1 if any, and will exit when the error occurs (it will not finish the work).
'
'EXAMPLES OF SERVICES WOULD BE:
' strService = " 'BTSSvc%' " ' WILL STOP/START ALL BIZTALK SERVICES
' strService = "'Microsoft.ManagedSolutions.Mse.Runtime.ServiceHost.exe'" WILL STOP/START THE MSE
'The Following example will stop all BizTalkHost instances in Server 1, 2 and 3
' SAMPLE USE: cScript ActBizTalkServerInstance STOP;'BTSSvc%';Server1;Server2;Server3 (Note: without ";" at the end)
' Valid actions are : START or STOP
' Created by Agustín Mántaras Sept 2014
' -------------------------------------------------------'
'Option Explicit
Dim objWMIService, objItem, objService, str, ReturnCode,i, Action
Dim colListOfServices, strService, intSleep ,sComputers,g_aComputers
strComputer = "."
On Error Resume Next
Main
Sub Main
ProcessArguments
ReturnCode = 0 'By default the exit Code will be zero --> No error
For i = 0 to UBound(g_aComputers)
'Looping through the parameters to start/stop services in those machines
if i = 0 Then
'Parameter 0 should be the action to run --> START OR STOP
StrAction = g_aComputers(i)
if strAction <> "START" Then
if strAction <> "STOP" Then
ReturnCode = 1
WScript.Echo "Invalid Action " & strAction & " only START and STOP are valid options"
end if
end if
Else
if i = 1 then
'Paramter 1 will be the service name to stop.
strService = g_aComputers(i)
Else
WScript.Echo "------------------------------------------"
WScript.Echo "Service to interact with " & strService
WScript.Echo "Action to execute " & strAction
WScript.Echo "------------------------------------------"
WScript.Echo straction & "ING THE SERVICES IN SERVER " & g_aComputers(i)
ActBizTalkServices g_aComputers(i),strAction,strService
end if
End if
if ReturnCode <> 0 then
Exit For
End if
Next
WScript.Echo "Exit Code: " & ReturnCode 'Returning 0 or 1
WScript.Quit (ReturnCode)
End Sub
Sub ActBizTalkServices (strComputer,strAction,strService)
' NB strService is case sensitive.
On Error Goto 0
On Error Resume Next
dim intNumberOfServices, intNumberOfActionsTaken
intNumberOfActionsTaken = 0
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service Where Name like "_
& strService & " ")
if err.Number = 0 Then
ReturnCode = 1
For Each objService in colListOfServices
On error GoTo 0
On Error Resume Next
WScript.Echo "Attempting to " & strAction & " Service " & objService.Name & " in " & strComputer
IF strAction = "STOP" Then
objService.StopService()
Else
objService.StartService()
end if
if err.Number = 0 then
ReturnCode = 0 'The action was sussecfully
Wscript.Echo "OK"
intNumberOfActionsTaken = intNumberOfActionsTaken +1
else
'Getting out the For loop since we get an error stopping a BizTalk Service
ReturnCode = 1
WScript.Echo err.Description
'ReturnCode = err.number
WScript.Echo "FAIL. Exiting Process"
Exit for
end if
Next
' end if ' IF Ubound(colListOfServices) < 1 then
' WScritp.Echo "Actions " & cstr(intNumberOfActionsTaken)
Else
WScript.Echo err.Description
ReturnCode = 1
end if
End Sub
Sub ProcessArguments()
sSyntax = "CScript StopBizTalkServers.vbs ACTION [START/STOP];SERVICENAME+[WILDCARS];<computer[;computer]>" & vbNewLine & _
" <computer[;computer]> List of computers to interact with services" & vbNewLine
Set oArgs = WScript.Arguments
SELECT CASE oArgs.Count
CASE 0
WScript.Echo sSyntax
WScript.Quit
CASE 1
sComputers = oArgs(0)
CASE Else
WScript.Echo sSyntax
WScript.Quit
END SELECT
' Change any localhost entryies to be a period for WMI.
g_aComputers = Split(sComputers, ";")
For i = 0 to UBound(g_aComputers)
If LCase(g_aComputers(i)) = "localhost" Then
g_aComputers(i) = "."
End If
Next
End Sub
Example code for Testing (START ACTION)
This script will start:
· ALL BizTalk Server host instances
· MSE
· IIS services.
To test it just create a cmd file in the same location that ActServices.vbs script.
cls
cscript ".\ActServices.vbs" START;'BTSSvc%%';.
cscript ".\ActServices.vbs" START;'Microsoft.ManagedSolutions.Mse%%';.
cscript ".\ActServices.vbs" START;'W3SVC%%';.
pause
Example code for Testing (STOP ACTION)
This script will stop:
· ALL BizTalk Server host instances
· MSE
· IIS services.
To test it just create a cmd file in the same location that ActServices.vbs script.
cls
cscript ".\ActServices.vbs" STOP;'BTSSvc%%';.
cscript ".\ActServices.vbs" STOP;'Microsoft.ManagedSolutions.Mse%%';.
cscript ".\ActServices.vbs" STOP; 'W3SVC %%';.
pause