Execute a file in VBScript with cscript.exe and not wscript.exe - vbscript

I know how to execute a exe with wscript.exe, something like this:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "c:\myprogram.exe"
But I'm forced to run my vbscript with cscript.exe, and can't use the WScript object. Are there any way to execute an exe when loaded with cscript.exe?

Both wscript.exe and cscript.exe provide the WScript object; so "Set WshShell = WScript.CreateObject(...)" is okay for .vbs files started with "w|cscript.exe whatever.vbs". VBScript - the language - provides its own CreateObject() function, so you can use plain "Set WshShell = CreateObject(...)" in all scripts (.hta, html too). The WScript COM object is another object. You can use it 'everywhere' (if we disregard security settings). In short: your code will work (or fail) with both hosts.

Set objShell = CreateObject("WScript.Shell")
objShell.run("cscript d:\Test2.vbs")

Related

schedule vbscript without task scheduler

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.

Restart vb script

I'm trying to make a Vb script that restarts another vbs script the problem is I'm new to this and I don't know how to do this, someone suggested using WshShell I have tried a few websites on how to use it but nothing. Here is what I've got,
Dim WshShell, oExec
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("Test_To_Block.vbs")
Do
If NOT WshShell.Status = 1 then
WScript.Exec("Test_To_Block.vbs")
End If
WScript.Sleep(100)
Loop
Thanks,
Regards,
A Viper
Yes, you can use Exec method to run another VB Script, but you are likely to get a console window flash.
Dim WshShell
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Exec("CMD /C Test_To_Block.vbs")
Refer to SS64 site to learn about VB Script basics.

Run a bat hidden and as admin the same time

I have a bat file that can run for more than an hour due to the things it does. Can I make it somehow run hidden and in admin mode?
I have found a way to make it run hidden with vbs:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run chr(34) & "C:\Users\Ab\Desktop\vi\nove.bat" & Chr(34), 0
Set WshShell = Nothing
I would prefer to find a way to put them inside the batch file but I think it's not possible so a vbs file would be ok as well.
I believe the runas verb of the Shell.Application object's ShellExecute method will run with elevated permissions. You can run it hidden by the same way you've done so in your current script, by setting the window handle to 0.
set shell=CreateObject("Shell.Application")
' shell.ShellExecute "application", "arguments", "path", "verb", window
shell.ShellExecute "move.bat",,"C:\Users\Ab\Desktop\vi\", "runas", 0
set shell=nothing

Is there a way to start a program minimized with VBScript using WScript.Shell?

Here is some example code I have right now to launch an app:
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run """C:\Program Files\Handbrake\HandBrakeCLI.exe"""
I tried the following to launch the app minimized but it didn't work. I'm assuming this only works from a normal command prompt?
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "start /MIN ""C:\Program Files\Handbrake\HandBrakeCLI.exe"""
I've also tried launching a shortcut (which just gave a null error and the script couldn't run) as well as trying to do a sendkeys to minimize it which didn't work at all.
This is from a VBScript running via cscript.exe by the way.
Does anyone know how I can start this app minimized within VBScript?
Check the docs and use the second parameter of the .Run method.
Evidence:
set s = createobject("WScript.Shell")
s.run "notepad", 2
starts Notepad minimized.

VBScript- Single line as administrator

Is it possible to use the shell.run command to run the specified program as an administrator?
for instance:
shell.run(cmd.exe) <---Run this as admin
shell.run(Notepad) <---Run this as normal
I know i can execute the script to run as admin but that means everything inside that script is executed as an administrator.
My other option was to seperate the scripts and run one as admin and include what needs to be ran as admin in that script, then call another script to run and run that one as normal.
Set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "cmd.exe", , , "runas", 1
oShell.Run "nodepad.exe"
(Source: http://social.technet.microsoft.com/Forums/eu/ITCG/thread/310e57f9-2367-4293-9f97-53d0e077aacc)
(More Info: http://ss64.com/vb/shellexecute.html)
Windows (from XP through to Win7 at least) has a runas command which does what you need. See here for details.
So, instead of running cmd.exe, you would run runas.exe, giving cmd.exe as the program to run.
Try this:
Dim ObjShell
Set ObjShell = CreateObject ("WScript.Shell")
ObjShell.Run "runas /K (command goes here) "
ObjShell.Run "notepad.exe"

Resources