Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to start an application with administrator privileges on the CMD command line

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to start the application with administrator privileges on the CMD command line". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to start an application with administrator privileges on the CMD command line.

In many cases, we need administrator privileges to run bat, so we need to combine vbscript to implement it.

Method 1:

1 mshta _ vbscript:CreateObject ("Shell.Application") .ShellExecute ("cmd.exe", "/ c% ~ S0::", "runas", 1) (window.close) & & exit

Commonly used

@ echo offmode con lines=30 cols=60%1 mshta _ vbscript:CreateObject ("Shell.Application") .ShellExecute ("cmd.exe", "/ c% ~ s0::,", "runas", 1) (window.close) & & exitcd / d "% ~ dp0" rem is ready to write your bat code

Method 2:

@ 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)

Note the different (double "and single') quotes that can be 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 may change: The current directory, the current TEMP folder and any mapped drives will be disconnected.

Runas will fail if you are running in WOW64 (a 32 bit process on 64 bit windows) for example% systemroot%\ syswow64\ cmd.exe...

The ShellExecute method is a member of the IShellDispatch3 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 an options to encrypt the password.

Equivalent CMD command: ShellRunAs-Run a command under a different user account

The% ~ dp0 in the batch file means

~ means to expand, which is equivalent to changing a relative path to an absolute path.

0 refers to the batch file itself

1 represents the first parameter received by the batch file command line, 2 represents the second, and so on

% ~ d0 refers to the drive letter where the batch is located, where d stands for drive

% ~ p0 refers to the directory where the batch is located, where p stands for path

% ~ dp0 is the drive letter plus path where the batch is located

Cd% ~ dp0 is in the directory where the batch is located.

For a detailed explanation, please refer to the command call /?

Since Vista brought UAC, applications have become two kinds, those with administrator privileges and those without administrator privileges. Some old applications go wrong inexplicably, so consider right-clicking the application and "running as an administrator". This is not a big deal, it can be found in the right-click menu of exe files, but it is not so convenient for some script files (cmd, js, etc.). Usually you need to reopen a command line window with administrator privileges, then type a lot of cd back to the folder you just found, and then run the script, which is quite troublesome.

I searched it and found 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);}

When you want to run the program as an administrator in the future, just type "Elevate", such as "Elevate cmd / k".

Of course, this can not escape the inspection of UAC, there will still be a dialog box pop-up points "OK".

At this point, I believe you have a deeper understanding of "how to start the application with administrator privileges in the CMD command line". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report