Board index » Visual Studio » Script Not Working Properly

Script Not Working Properly

Visual Studio52
Hello,



I have a script that restarts a service, I have it running from the Task

Scheduler under Administrator. If I double click the script, it runs great,

however, when the Task Scheduler runs it, the command prompt opens and

nothing happens, it just sits there and the window never closes (the

commands are never sent to the prompt).



Below is the script:



Option Explicit

Dim objShell, intShortSleep, intLongSleep

Dim strService

Set objShell = CreateObject("WScript.Shell")



' Values set

strService = "My Service"

intShortSleep = 1500

intLongSleep = 5500



' Cmd prompt opened

objShell.Run "cmd"

Wscript.Sleep intShortSleep



' Service stopped with 'Net' command

objShell.SendKeys "net stop " & chr(34) & strService & chr(34)

Wscript.Sleep intShortSleep

objShell.SendKeys "{Enter}"

Wscript.Sleep intLongSleep



' Service started with 'Net' command

objShell.SendKeys "net start " & chr(34) & strService & chr(34)

Wscript.Sleep intShortSleep

objShell.SendKeys "{Enter}"

Wscript.Sleep intLongSleep



' Cmd prompt exitedExit



objShell.SendKeys "Exit"

Wscript.Sleep intShortSleep

objShell.SendKeys "{Enter}"



Any Assistance is Greatly Appreciated,



Chuck


-
 

Re:Script Not Working Properly

On Nov 29, 2:53 pm, "Charles A. Lackman"

<Char...@CreateItSoftware.net>wrote:

Quote
Hello,



I have a script that restarts a service, I have it running from the Task

Scheduler under Administrator. If I double click the script, it runs great,

however, when the Task Scheduler runs it, the command prompt opens and

nothing happens, it just sits there and the window never closes (the

commands are never sent to the prompt).



Below is the script:



Option Explicit

Dim objShell, intShortSleep, intLongSleep

Dim strService

Set objShell = CreateObject("WScript.Shell")



' Values set

strService = "My Service"

intShortSleep = 1500

intLongSleep = 5500



' Cmd prompt opened

objShell.Run "cmd"

Wscript.Sleep intShortSleep



' Service stopped with 'Net' command

objShell.SendKeys "net stop " & chr(34) & strService & chr(34)

Wscript.Sleep intShortSleep

objShell.SendKeys "{Enter}"

Wscript.Sleep intLongSleep



' Service started with 'Net' command

objShell.SendKeys "net start " & chr(34) & strService & chr(34)

Wscript.Sleep intShortSleep

objShell.SendKeys "{Enter}"

Wscript.Sleep intLongSleep



' Cmd prompt exitedExit



objShell.SendKeys "Exit"

Wscript.Sleep intShortSleep

objShell.SendKeys "{Enter}"



Any Assistance is Greatly Appreciated,



Chuck



First, AFAIK, SendKeys cannot be used in a scheduled task and second,

there is a far better way to do this using the WScript.Shell Run

method ...



' Service stopped with 'Net' command

objShell.Run "net stop " & chr(34) & strService & chr(34), 0, True

Wscript.Sleep intLongSleep



' Service started with 'Net' command

objShell.Run "net start " & chr(34) & strService & chr(34), 0, True



Tom Lavedas

===========

members.cox.net/tglbatch/wsh/">members.cox.net/tglbatch/wsh/

-

Re:Script Not Working Properly

Your solution does't account for dependant services and maybe the

service you wish to restart doesn't have any dependacies. However, it

would make your script more robust and reusable if you were to use the

WMI object as below:-



' Values set

strService = "My Service"

strComputer = "."

'

Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" & strComputer & "\root

\cimv2")



Set colServiceList = objWMIService.ExecQuery("Associators of " _

& "{Win32_Service.Name=" & strService & "} Where " _

& "AssocClass=Win32_DependentService " & "Role=Antecedent" )

'

For Each objService in colServiceList

objService.StopService()

Next

'

Wscript.Sleep 20000

'

Set colServiceList = objWMIService.ExecQuery _

("Select * from Win32_Service where Name=" & strService)

For Each objService in colServiceList

errReturn = objService.StopService()

Next

'

Wscript.Sleep 20000

'

For Each objService in colServiceList

errReturn = objService.StartService()

Next

'

Wscript.Sleep 20000

'

Set colServiceList = objWMIService.ExecQuery("Associators of " _

& "{Win32_Service.Name=" & strService & "} Where " _

& "AssocClass=Win32_DependentService " & "Role=Dependent" )



For Each objService in colServiceList

objService.StartService()

Next

Wscript.Quit



Cheers,

Danny

-