run cmd.exe as administrator in a script - windows

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

Related

Run exe from vbs error

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"

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'"

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"

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