Most of the time we need admin privileges to run the bat, then we need to combine VBScript to achieve
Method One:
%1 Mshta vbscript:createobject ("Shell.Application"). ShellExecute ("cmd.exe", "/C%~s0::", "", "RunAs", 1) (Window.close) &&exit
Common
@echo off
mode con lines=30 cols=60
%1 mshta vbscript:createobject ("Shell.Application"). ShellExecute ("cmd.exe", "/C%~s0::", "", "RunAs", 1) (window.close) &&exit
cd/d "%~dp0"
rem Now you can write your bat code.
Method Two:
@echo off
%1%2
ver|find "5." >nul&&goto:st
mshta vbscript:createobject ("Shell.Application"). ShellExecute ("%~s0", "Goto:st", "" ", "RunAs", 1) (Window.close) &goto:eof
: St
Copy "%~0" "%windir%\system32\"
The principle is similar
ShellExecute method
Run a script or application in the Windows Shell.
Syntax
. ShellExecute "Application", "Parameters", "dir", "verb", window
. ShellExecute ' Some program.exe ', ' Some parameters with spaces ',, ' runas ', 1
Key
Application the file to execute (required)
Parameters Arguments for the executable
Dir Working Directory
Verb the operation to execute (runas/open/edit/print)
Window View mode application window (normal=1, hide=0, 2=min, 3=max, 4=restore, 5=current, 7=min/inactive, 10=default)
The different (double "and single") quotes that can is used to delimit paths with spaces.
The runas verb is undocumented but can be used to elevate permissions. When a script is run with elevated permissions several aspects of the user environment could change:the current directory, The current TEMP folder and any mapped drives would be disconnected.
RunAs would fail if you are are running in WOW64 (a bit-process on-bit Windows) For example%systemroot%\syswow64\cmd.exe ...
The ShellExecute method is a member of the IShellDispatch2 object.
Examples
Run a batch script with elevated permissions, Flag=runas:
Set Objshell = CreateObject ("Shell.Application")
Objshell.shellexecute "E:\demo\batchScript.cmd", "", "", "RunAs" , 1
Run a VBScript with elevated permissions, Flag=runas:
Set Objshell = CreateObject ("Shell.Application")
Objshell.shellexecute "cscript", "E:\demo\vbscript.vbs", "", " RunAs ", 1
"If you don ' t execute your ideas, they die" ~ Roger Von Oech
Related:
Run with elevated Permissions-script to run as Admin
. Exec-execute command, returning an object
. Run-run a command
Joeware.net-cpau (Create Process as User) like RunAs but with a options to encrypt the password.
Equivalent CMD command:shellrunas-run a command under a different user account
%~dp0 In a batch file indicates meaning
~ is the meaning of extension, which is equivalent to converting a relative path to absolute path
%0 refers to the batch file itself
%1 represents the first parameter that the batch file command line received,%2 represents the second, and so on
%~d0 refers to the letter in which the batch is located, where D represents the drive
%~p0 refers to the directory where the batch is processed, where p represents path
%~DP0 is the disk alphanumeric path where the batch process resides
CD%~dp0 is the directory where the batch is being processed.
Detailed explanation also can refer to command call/?
Since Vista brought UAC, the application has become two, with administrator privileges and no administrator privileges. Some old apps will be inexplicably wrong, so consider right-clicking the application and then "running as an administrator." This is not a big problem, exe file in the right menu will have this, but for some script files (cmd, JS Class), it is not so convenient. It's usually a hassle to reopen a command-line window with administrator privileges and then hit a lot of CDs back to the new folder before running the script.
Search a bit and find a solution. Save the following code as Elevate.js:
var command = WScript.Arguments.Item (0);
var argument = "";
for (var i = 0; i < WScript.Arguments.Count (); ++i) {
argument + = WScript.Arguments.Item (i) + "";
}
try{
var shellapp = new ActiveXObject ("Shell.Application");
Shellapp. ShellExecute (command, argument, null, "RunAs", 1);
}
catch (E) {
WScript.Echo ("Something wrong:" + e.description);
}
In the future to run the program as an administrator, as long as the input "elevate <exefile> <arguments>" on it, such as "Elevate cmd/k."
Of course, this escape is not a UAC check, or there will be a dialog box to play out the point "OK".