如何编码一个 BAT 文件始终作为管理模式运行?

我的 BAT 文件里有这么一行:

"Example1Server.exe"

我想执行这在管理员模式。如何修改蝙蝠代码运行这管理员?

是这样吗? 我需要加引号吗?

runas /user:Administrator invis.vbs Example1Server.exe
823724 次浏览

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

runas /user:Administrator Example1Server.exe

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

Screenshot

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.

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

The source and binaries are available on GitHub.

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

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

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

go get github.com/mattn/sudo

Then

sudo Example1Server.exe

When you use the /savecred argument, 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. Example for your question:

runas /user:Administrator /savecred Example1Server.exe

Just add this to the top of your bat file:

set "params=%*"
cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || (  echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/k cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B )

It will elevate to admin and also stay in the correct directory. Tested on Windows 10.

Use the complete physical drive\path to your Target batch file in the shortcut Properties.

This does not work in Windows 10 if you use subst drives like I tried to do at first...

  1. My experimenting indicates that the runas command must include the admin user's domain (at least it does in my organization's environmental setup):

    runas /user:AdminDomain\AdminUserName ExampleScript.bat
    

    If you don’t already know the admin user's domain, run an instance of Command Prompt as the admin user, and enter the following command:

    echo %userdomain%
    
  2. The answers provided by both Kerrek SB and Ed Greaves will execute the target file under the admin user but, if the file is a Command script (.bat file) or VB script (.vbs file) which attempts to operate on the normal-login user’s environment (such as changing registry entries), you may not get the desired results because the environment under which the script actually runs will be that of the admin user, not the normal-login user! For example, if the file is a script that operates on the registry’s HKEY_CURRENT_USER hive, the affected “current-user” will be the admin user, not the normal-login user.

I found there is possible to use powershell. The powershell will show the default Windows UAC Dialog.

powershell Start -File Example1Server.exe -Verb RunAs

For execute BAT file with admin rights, the content of the BAT file can look as this:

@echo off
if "%1"=="runas" (
cd %~dp0
echo Hello from admin mode
pause
) else (
powershell Start -File "cmd '/K %~f0 runas'" -Verb RunAs
)

where:

  • %1 First input argument assigned to BAT file.
  • %~f0 expands to full path to the executed BAT file
  • %~dp0 expands to full directory path from where the BAT file is executed
  • cmd -C <commands> Execute command in terminal and close

I Tested @Sire's answer on Windows 11, and it works like a charm. It's worth mentioning that using cmd /k - as @Sire has used - will keep the Administrator CMD open after it finishes running. Using cmd /c instead will close the window when it's over with the batch file.

set "params=%*"
cd /d "%~dp0" && ( if exist "%temp%\getadmin.vbs" del "%temp%\getadmin.vbs" ) && fsutil dirty query %systemdrive% 1>nul 2>nul || (  echo Set UAC = CreateObject^("Shell.Application"^) : UAC.ShellExecute "cmd.exe", "/c cd ""%~sdp0"" && %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs" && "%temp%\getadmin.vbs" && exit /B )