我正在尝试为我的用户编写一个批处理文件,使他们能够在他们的Vista机器上以UAC方式运行。该文件正在重新编写他们的hosts文件,因此需要以管理员权限运行。我需要能够通过电子邮件向他们发送.bat文件的链接。期望的行为是,当他们右键单击文件并选择“打开”时,会出现一个UAC对话框,使屏幕变暗并强制他们回答是否要将应用程序授予以管理员身份运行的权限。相反,他们只看到命令行窗口上显示“访问被拒绝”的信息。
是否有不同的实现方式?
我正在尝试为我的用户编写一个批处理文件,使他们能够在他们的Vista机器上以UAC方式运行。该文件正在重新编写他们的hosts文件,因此需要以管理员权限运行。我需要能够通过电子邮件向他们发送.bat文件的链接。期望的行为是,当他们右键单击文件并选择“打开”时,会出现一个UAC对话框,使屏幕变暗并强制他们回答是否要将应用程序授予以管理员身份运行的权限。相反,他们只看到命令行窗口上显示“访问被拒绝”的信息。
是否有不同的实现方式?
这个脚本很好用!只需将其粘贴到批处理文件的顶部即可。如果您想查看脚本输出,请在批处理文件底部添加一个"pause"命令。
更新:此脚本现在稍微编辑,以支持命令行参数和64位操作系统。
感谢 Eneerge @ https://sites.google.com/site/eneerge/scripts/batchgotadmin
@echo off
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params= %*
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
<YOUR BATCH SCRIPT HERE>
set params= %*
而不是set params=%*
来修复了代码,因此可以确保params被定义。 - dbenham这是我一直在使用的:
@echo off
if not "%1"=="am_admin" (
powershell -Command "Start-Process -Verb RunAs -FilePath '%0' -ArgumentList 'am_admin'"
exit /b
)
echo main code here
pause
注意事项:
-Verb RunAs
标志是启用管理员权限的关键。-ArgumentList
中。请注意,-ArgumentList
可以接受单个字符串或字符串数组。am_admin
之后包含它们来传递参数。if not "%1"=="am_admin" (powershell start -verb runas '%0' 'am_admin "%~1" "%~2"' & exit)
参数将会比它们原本的位置高一个。 - Tony Brix'am_admin%*'
传递所有内容,但不太适用于引号和空格 :/ 您可以在批处理中使用 shift
弹出第一个参数,从而修复除 %0
之外的所有参数。 - toster-cxif not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
后面添加 cd /D %~dp0
以保持工作目录。 - Pedro Duarte这是我的代码!它看起来很大,但实际上大部分都是注释行(以 :: 开头的行)。
特点:
完全参数转发
不更改工作文件夹
错误处理
接受带括号的路径(除了 %TEMP% 文件夹)
支持 UNC 路径
映射文件夹检查(如果管理员无法访问映射驱动器,则会警告您)
可用作外部库(请查看此主题中的我的帖子:https://dev59.com/4msz5IYBdhLWcg3wsaLN#30417025)
可以在需要时随时调用
只需将其附加到批处理文件的末尾,或将其保存为库(请参见上文)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:RequestAdminElevation FilePath %* || goto:eof
::
:: By: Cyberponk, v1.6 - 05/05/2023 - Fixed crash when batch filename contained spaces
:: v1.5 - 10/06/2016 - Changed the admin rights test method from cacls to fltmc
:: v1.4 - 17/05/2016 - Added instructions for arguments with ! char
:: v1.3 - 01/08/2015 - Fixed not returning to original folder after elevation successful
:: v1.2 - 30/07/2015 - Added error message when running from mapped drive
:: v1.1 - 01/06/2015
::
:: Func: opens an admin elevation prompt. If elevated, runs everything after the function call, with elevated rights.
:: Returns: -1 if elevation was requested
:: 0 if elevation was successful
:: 1 if an error occured
::
:: USAGE:
:: If function is copied to a batch file:
:: call :RequestAdminElevation "%~dpf0" %* || goto:eof
::
:: If called as an external library (from a separate batch file):
:: set "_DeleteOnExit=0" on Options
:: (call :RequestAdminElevation "%~dpf0" %* || goto:eof) && CD /D %CD%
::
:: If called from inside another CALL, you must set "_ThisFile=%~dpf0" at the beginning of the file
:: call :RequestAdminElevation "%_ThisFile%" %* || goto:eof
::
:: If you need to use the ! char in the arguments, the calling must be done like this, and afterwards you must use %args% to get the correct arguments:
:: set "args=%* "
:: call :RequestAdminElevation ..... use one of the above but replace the %* with %args:!={a)%
:: set "args=%args:{a)=!%"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEDELAYEDEXPANSION & set "_FilePath=%~1"
if NOT EXIST "!_FilePath!" (echo/Read RequestAdminElevation usage information)
:: UAC.ShellExecute only works with 8.3 filename, so use %~s1
set "_FN=_%~ns1" & echo/%TEMP%| findstr /C:"(" >nul && (echo/ERROR: %%TEMP%% path can not contain parenthesis &pause &endlocal &fc;: 2>nul & goto:eof)
:: Remove parenthesis from the temp filename
set _FN=%_FN:(=%
set _vbspath="%temp:~%\%_FN:)=%.vbs" & set "_batpath=%temp:~%\%_FN:)=%.bat"
:: Test if we gave admin rights
fltmc >nul 2>&1 || goto :_getElevation
:: Elevation successful
(if exist %_vbspath% ( del %_vbspath% )) & (if exist "%_batpath%" ( del "%_batpath%" ))
:: Set ERRORLEVEL 0, set original folder and exit
endlocal & CD /D "%~dp1" & ver >nul & goto:eof
:_getElevation
echo/Requesting elevation...
:: Try to create %_vbspath% file. If failed, exit with ERRORLEVEL 1
echo/Set UAC = CreateObject^("Shell.Application"^) > %_vbspath% || (echo/&echo/Unable to create %_vbspath% & endlocal &md; 2>nul &goto:eof)
echo/UAC.ShellExecute "%_batpath%", "", "", "runas", 1 >> %_vbspath% & echo/wscript.Quit(1)>> %_vbspath%
:: Try to create %_batpath% file. If failed, exit with ERRORLEVEL 1
echo/@%* > "%_batpath%" || (echo/&echo/Unable to create %_batpath% & endlocal &md; 2>nul &goto:eof)
echo/@if %%errorlevel%%==9009 (echo/^&echo/Admin user could not read the batch file. If running from a mapped drive or UNC path, check if Admin user can read it.)^&echo/^& @if %%errorlevel%% NEQ 0 pause >> "%_batpath%"
:: Run %_vbspath%, that calls %_batpath%, that calls the original file
%_vbspath% && (echo/&echo/Failed to run VBscript %_vbspath% &endlocal &md; 2>nul & goto:eof)
:: Vbscript has been run, exit with ERRORLEVEL -1
echo/&echo/Elevation was requested on a new CMD window &endlocal &fc;: 2>nul & goto:eof
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
如何使用示例
:EXAMPLE
@echo off
:: Run this script with elevation
call :RequestAdminElevation "%~dpfs0" %* || goto:eof
echo/I now have Admin rights!
echo/
echo/Arguments using %%args%%: %args%
echo/Arguments using %%*: %*
echo/%%1= %~1
echo/%%2= %~2
echo/%%3= %~3
echo/
echo/Current Directory: %CD%
echo/
echo/This file: %0
echo/
pause &goto:eof
[here you paste the RequestAdminElevation function code]
echo/%TEMP%| findstr /C:"(" >nul
测试您的 %temp%
环境变量中是否有 (
字符,并且只有在测试结果为正时才运行 &&
后面的部分。但是奇怪的是,您的 (
测试结果却是正的。 - cyberponkBen Gripka的解决方案会导致无限循环。他的批处理程序工作方式如下(伪代码):
IF "no admin privileges?"
"write a VBS that calls this batch with admin privileges"
ELSE
"execute actual commands that require admin privileges"
@echo off
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"=""
echo UAC.ShellExecute "cmd.exe", "/c ""%~dp0\my_commands.bat"" %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
pushd "%CD%"
CD /D "%~dp0"
REM Your commands which require admin privileges here
这不是关于如何以管理员身份运行批处理脚本的问题,而是关于如何从批处理脚本中提升其他程序的权限...
我有一个批处理文件“包装器”用于一个exe文件。它们具有相同的“根文件名”,但是扩展名不同。我能够使用以下一行 PowerShell 命令以管理员身份启动 exe 文件,并将工作目录设置为包含脚本的目录:
@powershell "Start-Process -FilePath '%~n0.exe' -WorkingDirectory '%~dp0' -Verb RunAs"
还有一系列其他可用的Start-Process
选项可以应用!请查看: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-6
请注意,我使用了@
前缀。这相当于在一行上使用@echo off
。我在此处使用%~n0
来获取批处理脚本的“根名称”,然后将.exe
连接起来以指向相邻的二进制文件。使用%~dp0
提供批处理所在目录的完整路径。当然,-Verb RunAs
参数提供了升级权限。
我知道这不是对原帖的解决方案,但我确信在这里有许多其他用例,所以我想分享一下。
我遇到了所有这些答案中的代码示例问题,但后来我找到了: http://www.robotronic.de/runasspcEn.html
它不仅允许您以管理员身份运行,还会检查文件以确保其未被篡改,并将所需信息安全存储。 我承认这并不是最明显的工具,但对于我们编写代码的人来说,应该足够简单。
@echo off
Net session >nul 2>&1 || (PowerShell start -verb runas '%~0' &exit /b)
Echo Administrative privileges have been got. & pause
上述方法在我的 Windows 10 版本 1903 上有效。
@echo off
和title
可以放在这段代码之前:
net session>nul 2>&1
if %errorlevel%==0 goto main
echo CreateObject("Shell.Application").ShellExecute "%~f0", "", "", "runas">"%temp%/elevate.vbs"
"%temp%/elevate.vbs"
del "%temp%/elevate.vbs"
exit
:main
<code goes here>
exit
cd %~dp0
会切换到包含批处理文件的目录)此外,还有来自这篇帖子的FSUTIL查询,该查询也链接到ss64.com,具有以下代码:
@Echo Off
Setlocal
:: First check if we are running As Admin/Elevated
FSUTIL dirty query %SystemDrive% >nul
if %errorlevel% EQU 0 goto START
::Create and run a temporary VBScript to elevate this batch file
Set _batchFile=%~f0
Set _Args=%*
:: double up any quotes
Set _batchFile=""%_batchFile:"=%""
Set _Args=%_Args:"=""%
Echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\~ElevateMe.vbs"
Echo UAC.ShellExecute "cmd", "/c ""%_batchFile% %_Args%""", "", "runas", 1 >> "%temp%\~ElevateMe.vbs"
cscript "%temp%\~ElevateMe.vbs"
Exit /B
:START
:: set the current directory to the batch file location
cd /d %~dp0
:: Place the code which requires Admin/elevation below
Echo We are now running as admin [%1] [%2]
pause