skeider
asked on
How to schedule a service to stop using vbscript
I am trying to create a script that will create a scheduled job that will stop a windows service. I know that I can use schtasks, but am having difficulties when the executable requires parameters. I have been trying to use NET STOP in order to stop the service. I know that I could alternatelyuse SC.exe STOP, but both require the service name as a parameter and in many cases the service name contains spaces.
How can I make this work?
How can I make this work?
Set objShell= WScript.CreateObject("WScript.Shell")
strTask = "schtasks /create /tn ""Stop_WWW" /tr ""net stop ""World Wide Web Publishing"""" /sc minute /mo 5 /ru System"
objShell.Run strTask,2,true
And I do apologize, I know you asked for a VB method, but since I dont script, I thought I would offer the next best, and easierst alternative fix to your dilemna....
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Actually, I was referring to simple DOS, not VBS.. Should have clarified.... That applies to the SC method as well.....
sc stop "Symantec AntiVirus"
sc stop "Symantec AntiVirus"
ASKER
Thanks to all for their assistance.
I realized that my issue was with the embedded quotes, but could not figure out the proper syntax. I had tried single quotes around the service. This created the task successfully, but the task would fail. I finally found out (after much frustration and too many hours) that the inner quotes needed to be escaped with a slash. Final syntax located below...
strTask = "schtasks /create /tn ""Stop_WWW"" /tr ""net stop \""World Wide Web Publishing\"""" /sc once /st 23:00:00 /ru System"
I realized that my issue was with the embedded quotes, but could not figure out the proper syntax. I had tried single quotes around the service. This created the task successfully, but the task would fail. I finally found out (after much frustration and too many hours) that the inner quotes needed to be escaped with a slash. Final syntax located below...
strTask = "schtasks /create /tn ""Stop_WWW"" /tr ""net stop \""World Wide Web Publishing\"""" /sc once /st 23:00:00 /ru System"
All you need to do.... Use Quotes......