I am currently searching through Google results but I was hoping someone would be able to assist me here too.
I am looking for a VB script that will stop more than one service on a Windows 2003 server then set it Disabled.
We are using System Center Configuration Manager in our environment and I want to use this to push a VB script to a few boxes in our environment to stop some services and then set the services to Disabled. This script will need to be written to run locally, instead of remotely, as Config Manager will be pushing it to the local machine.
Would anyone be able to assist me with this? If I have posted to the wrong thread, please let me know and I will post to the correct section.
Thanks ahead of time.
Oh, and Happy St. Patty's Day!

Dani AI

Generated

As found on TechNet and as suggested, there are a few ways to stop multiple services and set them to Disabled. The snippet below is an alternate, more robust VBScript pattern that (a) targets services by their short Name, (b) calls Stop, (c) polls for the stopped state, and (d) calls ChangeStartMode. It also logs results so you can verify what happened.

' Save as DisableServices.vbs and run with cscript.exe (run elevated)
Option Explicit
On Error Resume Next

Dim svcList, arr, wmi, fso, log, logFile, i, svcName, col, svc, ret, retries, temp, s2
svcList = "Service1,Service2,Service3"  ' replace with short service names, comma-separated

Set wmi = GetObject("winmgmts:{impersonationlevel=impersonate}!\\" & "." & "\root\cimv2")
Set fso = CreateObject("Scripting.FileSystemObject")
logFile = "C:\Temp\ServiceDisable.log"
Set log = fso.OpenTextFile(logFile, 8, True)

arr = Split(svcList, ",")
For i = 0 To UBound(arr)
  svcName = Trim(arr(i))
  If svcName <> "" Then
    Set col = wmi.ExecQuery("SELECT * FROM Win32_Service WHERE Name='" & svcName & "'")
    If col.Count = 0 Then
      log.WriteLine Now & " - Service not found: " & svcName
    Else
      For Each svc In col
        log.WriteLine Now & " - " & svc.DisplayName & " (" & svc.Name & ") state=" & svc.State
        If LCase(svc.State) <> "stopped" Then
          ret = svc.StopService()
          log.WriteLine " - StopService returned: " & ret
          retries = 0
          Do While LCase(svc.State) <> "stopped" And retries < 30
            WScript.Sleep 1000
            Set temp = wmi.ExecQuery("SELECT * FROM Win32_Service WHERE Name='" & svcName & "'")
            For Each s2 In temp
              Set svc = s2
            Next
            retries = retries + 1
          Loop
          log.WriteLine " - State after wait: " & svc.State
        End If
        If LCase(svc.StartMode) <> "disabled" Then
          ret = svc.ChangeStartMode("Disabled")
          log.WriteLine " - ChangeStartMode returned: " & ret
        End If
      Next
    End If
  End If
Next

log.Close

Notes and troubleshooting: use short service names (the Service name field in services.msc), run the script elevated (System or Administrator), and test on one machine first. Win32_Service methods return numeric codes (0 = success; 3 indicates dependent services are running), so inspect the method return values in the log to determine failures. (learn.microsoft.com)

If a StopService call fails with a dependent-service error, stop dependents first or investigate the Dependencies tab and event logs. Avoid disabling critical system services; test in a lab before wide deployment. The Win32_Service class docs explain properties like Name and StartMode if you need to adapt the script. (learn.microsoft.com)

Recommended Answers

All 2 Replies

i think you would be better asking ? here .good luck
http://www.daniweb.com/forums/forum4.html

Okay. Thanks.
I found the answer over at Microsoft's Technet Scripting Center forum anyhow.
Just in case anyone is searching for this also on this forum, I found out that Microsoft System Center Configuration Manager will push batch files also. So it would be easier, for me at least, to create a batch file than a VB script. But, if anyones is looking for the VB script, here is what was posted over there:

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

The other suggestion over there, if you were going to create a batch file, is to use the sc.exe command; 'sc stop' and 'sc config'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.