0% found this document useful (0 votes)
366 views

Vbscript Exemplos

The document contains multiple VBScript code snippets to manage Windows services programmatically. It queries existing services, stops services if running, changes their startup type to disabled, and loops through lists of target services to apply the same changes. Later sections open a text file containing a list of services, split it into an array, and call a subroutine to disable each one by stopping and changing the startup type.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
366 views

Vbscript Exemplos

The document contains multiple VBScript code snippets to manage Windows services programmatically. It queries existing services, stops services if running, changes their startup type to disabled, and loops through lists of target services to apply the same changes. Later sections open a text file containing a list of services, split it into an array, and call a subroutine to disable each one by stopping and changing the startup type.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 6

strComputer = ".

"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where Name = 'Alerter'")
For Each objService in colServiceList
If objService.State = "Running" Then
objService.StopService()
Wscript.Sleep 5000
End If
errReturnCode = objService.ChangeStartMode("Disabled")
Next

On Error Resume Next


strComputer = "."
arrTargetSvcs = Array("Alerter", "SCardSvr", "WZCSVC")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery("SELECT * FROM Win32_Service")
Wscript.Echo "Checking for target services ..."
For Each objService in colServices
For Each strTargetSvc In arrTargetSvcs
If LCase(objService.Name) = LCase(strTargetSvc) Then
WScript.Echo VbCrLf & "Service Name: " & objService.Name
WScript.Echo " Status: " & objService.State
Wscript.Echo " Startup Type: " & objService.StartMode
WScript.Echo " Time: " & Now
If objService.State = "Stopped" Then
WScript.Echo " Already stopped"
Else
intStop = objService.StopService
If intStop = 0 Then
WScript.Echo " Stopped service"
Else
WScript.Echo " Unable to stop service"
End If
End If
If objService.StartMode = "Disabled" Then
WScript.Echo " Already disabled"
Else
intDisable = objService.ChangeStartMode("Disabled")
If intDisable = 0 Then
WScript.Echo " Disabled service"
Else
WScript.Echo " Unable to disable service"
End If
End If
End If
Next
Next

sComputer = "."
aTargetSvcs= Array("SERVICE1","SERVICE2","SERVICE3")
Set oWMIService = GetObject("winmgmts:" & "{impersonationlevel=impersonate}!\\" _
& sComputer & "\root\cimv2")
Set cServices = oWMIService.ExecQuery("SELECT * FROM Win32_Service")
For Each oService In cServices
For Each sTargetSvc In aTargetSvcs
If LCase(oService.Name) = LCase(sTargetSvc) Then
If oService.State <> "Stopped" Then
oService.StopService()
End If
If oService.StartMode <> "Disabled" Then
oService.ChangeStartMode("Disabled")
End If
End If
Next
Next

sComputer = "."
aTargetSvcs= Array("SERVICE1","SERVICE2","SERVICE3")
Set oWMIService = GetObject("winmgmts:" & "{impersonationlevel=impersonate}!\\" _
& sComputer & "\root\cimv2")
Set cServices = oWMIService.ExecQuery("SELECT * FROM Win32_Service")
For Each oService In cServices
For Each sTargetSvc In aTargetSvcs
If LCase(oService.Name) = LCase(sTargetSvc) Then
If oService.State <> "Stopped" Then
oService.StopService()
End If
If oService.StartMode <> "Disabled" Then
oService.ChangeStartMode("Disabled")
End If
End If
Next
Next

'=*=*=*=*=*=*=*=*=*=*=*=
' Coded By Assaf Miron
'=*=*=*=*=*=*=*=*=*=*=*=
'This Script Disabels a List of Services from a text file

Const ForReading=1
Sub DisableService(ServiceName)
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery _
("Select * from Win32_Service where DisplayName = " & ServiceName)
For Each objService in colServiceList
errReturnCode = objService.StopService()
errReturnCode = objService.Change( , , , , "Disabled")
Next
End Sub
Set objDialog = CreateObject("UserAccounts.CommonDialog")
objDialog.Filter = "Text Files|*.txt|All Files|*.*"
objDialog.FilterIndex = 1
objDialog.InitialDir = "C:\"
intResult = objDialog.ShowOpen

If intResult = 0 Then
Wscript.Quit
Else
FileLoc = objDialog.FileName
End If
set objReadFile = objFSO.OpenTextFile(FileLoc, ForReading)

Do Until objReadFile.AtEndOfStream
Err = 0
strNextLine = objReadFile.Readline
arrServiceList = Split(strNextLine , ",")
For i = 0 to Ubound(arrServiceList)
DisableService arrServiceList(i)
Next
Loop

VBS Script To Set A Service Start Mode To Automatic And Start It

Here you will find two VBS script to set a Service to Automatic and Start it even if the Service is
Disabled or set to Manual. In the first script you can specify the Service Name and in the second
script you can specify the Services Display Name.

VBS Script: By Service Name

strComputer = InputBox ("Enter Machine Name")


objServiceName = "Service_Name"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CimV2")


Set colListOfServices = objWMIService.ExecQuery ("Select * From Win32_Service Where Name
='" & objServiceName & "'")

For Each objService in colListOfServices


objService.ChangeStartMode("Automatic")
Wscript.Sleep 5000

errReturnCode = objService.StartService()
Next

MsgBox "Done"

Example: objServiceName = " TlntSvr"

VBS Script: By Service Display Name

strComputer = InputBox ("Enter Machine Name")


objServiceDisplayName = "Service_Display_Name"

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CimV2")


Set colListOfServices = objWMIService.ExecQuery ("Select * From Win32_Service Where
DisplayName ='" & objServiceDisplayName & "'")

For Each objService in colListOfServices


objService.ChangeStartMode("Automatic")
Wscript.Sleep 5000
errReturnCode = objService.StartService()
Next

MsgBox "Done"

Example: objServiceDisplayName = "Telnet"

Note: You can also change the Start Mode to Disabled Or Manual as well as Stop the service
using StopService()

You might also like