Run exe from vbs error - vbscript

I'm trying to run an executable from a VBScript. Tried this:
oShell = CreateObject("Shell.Application")
oShell.ShellExecute("cmd.exe", , , "runas", 1)
oShell.Run("File.exe")
However, it gives me sub error on the second line. Where did I do mistake?

I assume that you want to execute file.exe with elevated privileges and keep the window open.
VBScript:
set oShell = CreateObject("Shell.Application")
oShell.ShellExecute "cmd.exe", "/K file.exe", "", "runas", 1
Note that runas verb is undocumented.
PowerShell:
Start-Process -FilePath "cmd.exe" -ArgumentList #("/K", "file.exe") -Verb "runas"

Related

Modify Windows hosts file from vbscript with admin privileges

Our network team uses a .VBS script file that runs every time a user logs into the network. They asked me to edit their script so that it will modify the windows hosts file.
The problem is the script needs admin privileges on the user's computer. From the script, how do I open the hosts file with elevated privileges, make some changes, and then save the file?
Something like that :
If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, WScript.ScriptFullName & " /elevate", "", "runas", 1
WScript.Quit
End If
Hosts = "%windir%\system32\drivers\etc\hosts"
Command = "cmd /c attrib "& Hosts &" -r"
Set Ws = WScript.CreateObject("WScript.Shell")
Result = Ws.run(Command,0,True)
EditHostsFile = Ws.run("cmd /c Notepad "& Hosts,0,True)
HostsReadOnly = Ws.run("cmd /c attrib "& Hosts &" +r",0,True)

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

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"

run cmd.exe as administrator in a script

I have a script that I need to run as Administrator just as I would right click on cmd.exe and click Run As Administrator.
Currently this is what I have:
Call WSHShell.Run("cmd.exe /K netdom renamecomputer ... end code")
Have you tried using ShellExecute?
RunCmdElevated.vbs
Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute "cmd.exe", "/k echo test", "", "runas", 1
wscript RunCmdElevated.vbs
gives
test
C:\Windows\system32>
in a new window. The "1" is the view mode
http://ss64.com/vb/shellexecute.html

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.

Resources