VB script to stop services then disable
-
Wednesday, March 17, 2010 6:54 PMI 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 to Disabled silently, without user interaction.
We are using System Center Configuration Manager in our environment and I want to use this to push a VB script to a few servers on our domain 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.
Thanks ahead of time.
All Replies
-
Wednesday, March 17, 2010 7:50 PM
Have you had a look at the "sc.exe" command? "sc stop" and "sc config" do exactly what you are looking for.
-
Wednesday, March 17, 2010 8:09 PM
I'm not too familiar with it. I would assume you would have to create a batch file for this command, right?
The only issue, as I understand it from our System Center Configuration Manager admin, that Config Manager has an issue with pushing out batch files, or something, that it is better to use a VB script.
I don't know anything about Config Manager so I am going by what she told me. That is why I was looking for a VB script.
I found a script that is close to what I am looking for but it requires a response in which would not work in my scenario. I need a non-interactive, a silent mode, script that just runs, stops the listed services in the script and disables them.
Here is what I found:
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
I know nothing about scripting enough to change this, to remove the WScript.Echo lines and such, otherwise I could tweak this to my needs.
Any help would be greatly appreciated. -
Wednesday, March 17, 2010 9:15 PM
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- Proposed As Answer by Aron Beaver Thursday, March 18, 2010 4:57 PM
-
Wednesday, March 17, 2010 10:30 PM
Config Manager has an issue with pushing out batch files, or something, that it is better to use a VB script.
That's not true -
Thursday, March 18, 2010 9:17 AMI always push batch file with the following command line; cmd.exe /c <batch file>. Works like a charm.
-
Thursday, March 18, 2010 10:21 AM
Thanks everyone for your response. Thanks a thousand times to Aron, I will check out that script when I get to work today. Torsten and VincentK, that is good to know. I've been tasked to learn Config Manager so I will also play around with pushing batch files. It'll be much easier for me to write batch file than trying to come up with a VB script for my projects.Thanks again everyone.- Marked As Answer by edwardcrosby Thursday, March 18, 2010 1:44 PM
-
Tuesday, May 11, 2010 5:59 PM
I wanted to follow up on this thread to post what batch file I ended up using pushed from Config Manager. Here is what I used:
net stop <servicename> /yes
That stops the service and its dependencies
sc config "servicename" start= disabled
That disables the service. The other commands are demand which sets it to manual and also auto that sets it to automatic.
-
Saturday, March 10, 2012 12:15 PM
edwardcrosby's answer worked.
I was trying to stop a set of services. I did it in a manual way (don't know much about scripts...). I opened up a notepad and wrote
NET STOP SPAdminV4 NET STOP SPTimerV4 NET STOP SPUserCodeV4
included as many NET STOP statements I needed. I was able to find the service name by going to services, right click on the service, go to properties. "SPAdminV4" is the SharePoint 2010 Administration service.
Saved the file by going a name "stopservice.bat" then just run the script by double-clicking. Hope this helps too..

