Vbscript Exemplos
Vbscript Exemplos
"
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
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
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.
errReturnCode = objService.StartService()
Next
MsgBox "Done"
MsgBox "Done"
Note: You can also change the Start Mode to Disabled Or Manual as well as Stop the service
using StopService()