How to code a BAT file to always run as admin mode?

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

I have this line inside my BAT file:

"Example1Server.exe" 

I would like to execute this in Administrator mode. How to modify the bat code to run this as admin?

Is this correct? Do I need to put the quotes?

runas /user:Administrator invis.vbs Example1Server.exe 

回答1:

You use runas to launch a program as a specific user:

runas /user:Administrator Example1Server.exe 


回答2:

The other answer requires that you enter the Administrator account password. Also, running under an account in the Administrator Group is not the same as run as administrator see: UAC on Wikipedia

Windows 7 Instructions

In order to run as an Administrator, create a shortcut for the batch file.

  1. Right click the batch file and click copy
  2. Navigate to where you want the shortcut
  3. Right click the background of the directory
  4. Select Paste Shortcut

Then you can set the shortcut to run as administrator:

  1. Right click the shortcut
  2. Choose Properties
  3. In the Shortcut tab, click Advanced
  4. Select the checkbox "Run as administrator"
  5. Click OK, OK

Now when you double click the shortcut it will prompt you for UAC confirmation and then Run as administrator (which as I said above is different than running under an account in the Administrator Group)

Check the screenshot below

Note: When you do so to Run As Administrator, the current directory (path) will not be same as the bat file. This can cause some problems in many cases that the bat file refer to relative files beside it. For example, in my Windows 7 the cur dir will be SYSTEM32 instead of bat file location! To workaround it, you should use

cd %~dp0

or better

pushd %~dp0

to ensure cur dir is at the same path where the bat file is.



回答3:

If you can use a third party utility, here is an elevate command line utility.

This is the usage description:

Usage: Elevate [-?|-wait|-k] prog [args] -?    - Shows this help -wait - Waits until prog terminates -k    - Starts the the %COMSPEC% environment variable value and                 executes prog in it (CMD.EXE, 4NT.EXE, etc.) prog  - The program to execute args  - Optional command line arguments to prog 


回答4:

convert your batch file into .exe with this tool: http://www.battoexeconverter.com/ then you can run it as administrator



回答5:

I think I have a solution to the password problem. This single argument is truly amazing. It asks for the password once, and than never asks for it again. Even if you put it onto another program, it will not ask for the password. Here is is:

runas /user:Administrator /savecred Example1Server.exe



回答6:

You can use nircmd.exe's elevate command

NirCmd Command Reference - elevate

elevate [Program] {Command-Line Parameters} 

For Windows Vista/7/2008 only: Run a program with administrator rights. When the [Program] contains one or more space characters, you must put it in quotes.

Examples:

elevate notepad.exe  elevate notepad.exe C:\Windows\System32\Drivers\etc\HOSTS  elevate "c:\program files\my software\abc.exe" 

PS: I use it on win 10 and it works



回答7:

go get github.com/mattn/sudo 

Then

sudo Example1Server.exe 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!