Windows Scripting

Soldato
Joined
12 Dec 2003
Posts
2,588
I want to write a script that when I run it, it toggles a Service on/off
ie so run it first time, it turns the service on, run it a second time it turns the service off. How would I go about this?
(I know I could just make 2 seperate script files but I wanted to know if its possible to combine them).

Thanks
 
NET STOP ServiceName

Put this in a batch file and that will do it.

And to start the service:

NET START ServiceName

Does this help?



M.

i.e.

NET STOP SNMPSERVICE

NET START SNMPSERVICE
 
Well for that I'd need 2 seperate files wouldnt I?
I was thinking something like:

If ServiceName = Started
Then net stop ServiceName
If ServiceName = Stopped
Then net start ServiceName
 
how about? in VB this is...

Code:
Dim Flag as Boolean


Private Sub Form_Load()
Flag = 0
End Sub

Private Sub Button_Click()
If Flag = 1 then
Flag = 0
Else
Flag = 1
End IF

IF Flag = 1 then 
Shell ("C:\Windows\System32\cmd.exe NET START *SERVICE*")
Else
Shell ("C:\Windows\System32\cmd.exe NET STOP *SERVICE*")
End IF

End Sub

Not sure if that would actually work, wrote it free hand so have no idea how many errors that would throw :D
 
Last edited:
If SC has an error level when it returns an 'already running' response which I guess it will do then you could do:

@Echo off

sc \\REMOTE_MACHINE start "Service Name"

IF ErrorLevel *Error level code if running* GOTO Stop
IF ErrorLevel 0 GOTO End

:Stop

sc \\REMOTE_MACHINE stop "Service name"

GOTO End

:End


But like I said...I don't know if it has an error level code response...haven't got a windows machine booted here at the minute or I'd try myself. Might be something to try?
 
LOL, was being a div there, trying to assign 0's and 1's to a False/True variable doesn't work :rolleyes:

Code:
Dim SetFlag As String

Private Sub Form_Load()
SetFlag = 0
End Sub

Private Sub Command1_Click()

If SetFlag = 1 Then
SetFlag = 0
Else
SetFlag = 1
End If

If SetFlag = 1 Then
Shell "C:\WINDOWS\system32\cmd.exe /c NET START PunkBuster"
Else
Shell "C:\WINDOWS\system32\cmd.exe /c NET STOP PunkBuster"
End If

End Sub

tried this and it works fine :)
 
No problem mate. Don't use the .vbs file, if you go to the menu, go to "File" then down to "Make Project1.exe", click that rename and place it where you want and you have an executable file to use. :)
 
Oh, right. I have VB 2008 Express Installed but I dont know what sort of project to create. I tried 'Console App', then pasted the code in but it didnt have 'Make Project1.exe' in the File menu.
 
Ah right thats a little different, using VB6 myself. :) i'll just find out for you

Edit: I remember that if you save the project in 2005 that it automatically created the .exe to go with it, have you saved it? if so check the directory you saved it to for an executable file. If not then do so :)
 
Last edited:
There was a toggle.vshost.exe in ToggleProject\bin\Debug but double clicking it didnt seem to do much... would it be easier if you just compiled the exe and sent it to me? If so the service I am trying to start/stop is called 'KService', thanks for all the help!
 
Yeah sure, I'll get on it next chance I have, Vista doesn't like VB so only got it on my XP install

Edit: Went onto college network and did it :D

Download Here
 
Last edited:
Back
Top Bottom