VBScript- Single line as administrator - windows

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"

Related

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

Command prompt opening multiple windows on wshShell.Run

Hi I'm trying to run following command in VBA
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run "cmd.exe runas some commands"
It's opening multiple command prompt windows.
If I run
wshShell.Run "cmd.exe"
Only this then it opens a single window.
Am i doing anything wrong in the 1st scenario.
Try using the /K switch and keep the quotes aroung cmd.exe before you run the rest of your commands
wshShell.Run " 'cmd.exe /K' 'commands here'"

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.

Running script on elevated cmd.exe

How do I ran a script after I open a command window using the below script?
Set objSh = CreateObject("Shell.Application")
objSh.ShellExecute "cmd.exe", "uac" , "", "runas", 1
For example, how do I run ipconfig as an admin using the above script?
If you specify the /c switch, then cmd.exe will carry out the specified command and then terminate.
So, for example:
Set objSh = CreateObject("Shell.Application")
objSh.ShellExecute "cmd.exe /c ipconfig", "uac" , "", "runas", 1
Alternatively, you could use the /k switch, which works exactly the same way, except it keeps the command prompt on the screen once your command finishes executing.

How to keep the VBScript command window open during execution

When I execute a VBScript, the command window that it creates closes quickly before the user gets a chance to read the output. How can I get the window to stay open without modifying windows registry?
This is the code:
Set objShell = WScript.CreateObject("WScript.shell")
objShell.Run "SyncToyCmd.exe -R", 1, True
You can send your execution command through the cmd.exe command interpreter, along with a pause command which will give the user a Press any key to continue . . . prompt to close the window.
objShell.run "%comspec% /c ""SyncToyCmd.exe -R & pause""", 1, True
Or to keep the window alive, use the /k flag instead of /c:
objShell.run "%comspec% /k SyncToyCmd.exe -R", 1, True
But beware, your VBScript will not continue (or terminate) until this cmd window is manually closed.
The %comspec% environment variable refers to the correct command to open the command interpreter as per your operating system. On my XP machine, for instance, %comspec% is equal to C:\WINDOWS\system32\cmd.exe.
See cmd.exe documentation here: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true
More info on the use of the & versus the && command separators here.
Assuming that it's the popped-up command window that you want to keep open (rather than the one running your VBScript), you can use CMD.exe's Pause command to achieve this:
Set objShell = WScript.CreateObject("WScript.shell")
objShell.Run "cmd.exe /C ""SyncToyCmd.exe -R & Pause"" ", 1, True
Make it sleep for a while, maybe tell the user it will close in 5 seconds?
Set WScript = CreateObject("WScript.Shell")
WScript.Sleep 5000

Resources