schedule vbscript without task scheduler - vbscript

I have a VBScript, Abc.vbs, which contains:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "D:\Abc.bat" & Chr(34), 0
Set WshShell = Nothing
How do I schedule Abc.vbs to run without Windows Task Scheduler ?

'Put this code above your own code in the VBS file.
Do While(cStr(Time) <> "22:00:00") 'Please check the date/time format on your computer and make changes accordingly. This code schedules the script to 10 PM
Wscript.Sleep = 100
Loop
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "D:\Abc.bat" & Chr(34), 0
Set WshShell = Nothing
Now double click this VBS and leave it as it is.

It depends on your environment, operating system, when you want it to run and what tools you have available to you.
In windows you could use registry keys or group policy to run your script at logon which can then run in a loop and then at set intervals run your batch file.
You could install an executable as windows service, which basically does what the above but uses an executable.
If you have access to SQL service it has the ability to schedule jobs as well.
There are probably a few more ways but again they depend on your specific use case.

Related

Why does VBS always tell me file not found even though the file is right there? (At startup)

I have a .vbs file, which I have set to run at startup through regedit. Basically what the vbs does, is execute another program in the same directory (I will paste the vbs script below). Normally, the vbs script works great and everything is good. However, whenever the vbs script run at startup (i.e auto running right after the computer is booted), I always get a error message, telling me that Windows cannot find my file (i.e hello.exe), even though the exe file is right there.
I have tried setting a delay to the script, but that resulted in the same problem. I am extremely confused because everytime I run the vbs manually (like double click it), everything works fine, no problem.
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "hello.exe" & Chr(34), 0
Set WshShell = Nothing
The expected result is that the vbs script will just run normally, like how it runs everytime i manually launch it. The error message is "Line 2: File cannot be found", or something along the lines of that.
As Hackoo is getting to, use the full path to the EXE you are running:
WshShell.Run chr(34) & "C:\My Hello App\hello.exe" & Chr(34), 0
This will run the exe file if its in the same folder as your script.
strPath = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName,"\"))
WshShell.Run chr(34) & strPath & "hello.exe" & Chr(34), 0

Is it possible to write a Windows 10 script opening speaker settings?

I want to write a windows 10 batch script to change my microphone's output volume, as Skype apparently is not able to provide me with consistent settings. Can you point me in the right direction on how to achieve this?
To change the volume You can build 2 vbs files :
soundown.vbs
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "{" & chr(174) & " 5}"
soundup.vbs
Set WshShell = CreateObject("WScript.Shell")
WshShell.SendKeys "{" & chr(175) & " 20}"
and then call them from your .batfile :
#echo off
cscript.exe /nologo soundown.vbs
or the same with soundup.vbs
EDIT:
You can modify the steps it will modify the volume by modifing the value 5 and 20 in the VBSfiles

How to run a vbscript file in Task Scheduler

I want to run my VBS file script in the background on a specific date.
my VBS file is simple (call to the batch file and run it in mode invisible)
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Temp\Generate_Report.bat" & Chr(34), 0
Set WshShell = Nothing
PS: when I run the VBS file manually it works well but if I want to run it with TASK Scheduler I got this error.
Configuration file (VBS in the Task Scheduler tool)

Would you fix this vbs to execute this bat file with admin priviledge

Under windows 7 with uac activated.
This is all around an issue. THIS ISSUE:
upnphost excessive cpu load
Especifically:
Mine has this problem frequently, and I hate to go restart the upnphost service all the time, so instead i just created a task in the
task scheduler to run a once a day, and repeat every 5 minutes. The
task runs a .bat file:
net stop upnphost
net start upnphost
if you want to make it run in the background without the cmd window
coming up, run this .vbs with the above .bat already created as
"C:\upnphost.bat":
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\upnphost.bat" & Chr(34), 0 Set
WshShell = Nothing
this way the upnphost service will automatically restart every 5
minutes with no visible presentation, so if it decides to misbehave
and go high cpu, it will be for 5 minutes tops, you could change this
to any interval you want.
Everything works except for the fact that for the bat to actually work i need to right click directly on the bat and execute as admin.
For that reason if i click the vbs it will execute it but not as admin and it wont work.
So scheduling it as a task wont work either.
Can you fix the code in the vbs:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\upnphost.bat" & Chr(34), 0 Set
WshShell = Nothing
So it executes the bat with admin priviledge.
It that matters, my route to the bat has spaces.
Respectfully
You can launch an program as an administrator with UAC enabled, but you'll still get prompted whether you want to launch this program. For example,
Set objSA = CreateObject ("Shell.Application")
objSA.ShellExecute "cmd.exe","uac","","runas",1
However,
I was able to use this vbscript to stop and restart this service using a scheduled task, setting the task to run as "Hidden" and run with the highest privileges.
strService = "upnphost"
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colService = objWMIService.ExecQuery("SELECT Name FROM Win32_Service " _
& "where Name='" & strService & "'")
For Each objService In colService
return = objService.StopService()
return = objService.StartService()
Next
The scheduled task must be run as a user who can start and stop the service (usually an admin account) and with the option "Run with highest privileges" enabled.

Windows script - run silent but wait for completion / return right code

Here's my situation:
I have a BAT file that takes a long time to run (1minute to 70 minutes)
I schedule it with Windows scheduler to run every 10 minutes
If it schedules again while it is still running, nothing happens (this is good)
My problem is that I need my BAT to run silently, and it doesn't. So, I want to launch it with a Windows script like the following:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0
Set WshShell = Nothing
Unfortunately, when I schedule this script, it does the job but returns instantly, making Windows scheduler think the task is finished when in reality the BAT is just running off by itself somewhere.
Because of this, Windows will reschedule the job again 10 minutes later and make multiple run.
What I need:
Is there a way to tell the Windows script file to wait for the target of the .Run command to complete before progressing/exiting? Basically, I want it to act like I launched another thread and then called join on it, so it does the same thing the BAT would have, but without displaying the console window.
Other Solutions
Tell me any other way to get this BAT to execute silently (powershell commands, whatever) and I'll accept it as a solution as well. Just don't tell me to write a full on C++/C# appliation around it, that's overkill :)
Running: Windows Server 2008 R2
I think all you need is TRUE for the optional 3rd argument
WshShell.Run chr(34) & "C:\Batch Files\syncfiles.bat" & Chr(34), 0, TRUE
Here goes the madness...
Create a file invisible.vbs:
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, True
Then in the task scheduler, call your script like this:
wscript.exe "C:\Batch Files\invisible.vbs" "C:\Batch Files\syncfiles.bat"
I tested it and this does really work: No command window, and the task is considered running until the batch script ends.
Credit goes to https://superuser.com/a/62646/40362.
For 2008 R2 make invisible.vbs have this content, and just execute it directly. It will run the BAT silently and wait for completion.
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run "D:\IUpdateScript.bat", 0, true

Resources