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)
Related
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
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.
I would like to run a batch file with some extra character at the end.
If I write it directly to cmd it is working, like change directory to "c:\Users\Public\Uploader\" and write the following: start.bat "cmd:file.import c:\Users\tom\Desktop\a.xml"
I do not know how to write it in VBScript, because the following script is not working:
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.CurrentDirectory="C:\Users\Public\Uploader\"
WshShell.Run "start.bat" & "cmd:file.import C:\Users\tom\Desktop\a.xml"
The a.xml should dynamically change based on the last modified file in the folder.
Thank you for your help!
If you're trying to duplicate what you're doing in the command prompt, you need to add a space between the batch file name and its arguments. You also need to add the quotes.
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.CurrentDirectory="C:\Users\Public\Uploader\"
WshShell.Run "start.bat " & chr(34) & "cmd:file.import C:\Users\tom\Desktop\a.xml" & chr(34)
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.
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