1
votes

I have a scheduled task that launched a .vbs file, that launches a batch file with no window, that checks to see if a service is running and if not, then it starts it. The reason I have a .vbs launching the batch file is so the batch file doesn't have the window pop up. The issue I am having is that I get access denied when I run the batch via .vbs, so I want to run the whole system in a .vbs script to check to see if a service is started or running and if not start it. Also I used the schtasks to to run the .vbs with /RL Highest so I do not get access denied, but when the .vbs launches the batch file, the batch files does not have the permission and get access denied.

The scheduled task command I entered:

    schtasks /CREATE /TN "OpenTunnel" /TR "C:\BTools\OpenTunnel\Invisable_OpenTunnel.vbs" /sc minute /mo 10 /RU ***** /RP ***** /RL HIGHEST

And here is the batch file that runs and checks the service state.

for /F "tokens=3 delims=: " %%H in ('sc query "Hamachi2Svc" ^| findstr "        STATE"') do (
  if /I "%%H" NEQ "RUNNING" (
   net start "Hamachi2Svc"
  )
)

And here is the code of the .vbs that lanches the batch with no window:

Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "opentunnel.bat" & Chr(34),0
Set WinScriptHost = Nothing

So it works if I run the batch as an administrator, but is there a way to have a .vbs run every 10 min that does something similar that the batch file does? A .vbs file would be the best after all, I can grant it /RL with HIGHEST and that should work.

Thanks in advance =)

1

1 Answers

2
votes

Use WMI (specifically the Win32_Service class) for this:

Set wmi = GetObject("winmgmts://./root/cimv2")

qry = "SELECT * FROM Win32_Service WHERE Name = 'Hamachi2Svc'"
For Each svc In wmi.ExecQuery(qry)
  If svc.State <> "Running" Then svc.StartService
Next