我想写我的用户从他们的Vista计算机与UAC运行的批处理文件。 该文件被重新书写自己的hosts文件,因此它需要具有管理员权限运行。 我需要能够给他们一个链接到.bat文件的电子邮件。 期望的行为是,当他们在文件上单击鼠标右键,并说开放,他们将获得这些UAC对话框,使屏幕暗下去,并迫使他们回答他们是否想给以管理员身份运行的应用程序的权限之一。 相反,他们只是看到“拒绝访问”命令行窗口上。
这是可以做到不同?
我想写我的用户从他们的Vista计算机与UAC运行的批处理文件。 该文件被重新书写自己的hosts文件,因此它需要具有管理员权限运行。 我需要能够给他们一个链接到.bat文件的电子邮件。 期望的行为是,当他们在文件上单击鼠标右键,并说开放,他们将获得这些UAC对话框,使屏幕暗下去,并迫使他们回答他们是否想给以管理员身份运行的应用程序的权限之一。 相反,他们只是看到“拒绝访问”命令行窗口上。
这是可以做到不同?
该脚本的伎俩! 只需将其粘贴到您的BAT文件的顶部。 如果你想查看你的脚本的输出,在批处理文件的底部添加一个“暂停”命令。
UPDATE:此脚本现在稍微修改以支持命令行参数和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>
这是我一直在使用一个班轮:
@echo off
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit /b)
echo main code here
pause
笔记:
这里是我的代码! 它看起来很大,但它主要是注释行(开始与线::)。
特征:
映射文件夹检查(Warn's你,如果管理员不容访问映射驱动器)
可作为一个外部库(检查我的岗位在这个主题: https://stackoverflow.com/a/30417025/4932683 )
只是附加给你的批处理文件的末尾,或将其保存为一个库(检查上面)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:RequestAdminElevation FilePath %* || goto:eof
::
:: By: Cyberponk, 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]
另一种方法是
然后
丹尼斯
[添加之后 - 是的,我也没有注意到这个线程的日期。]
本Gripka的解决方案会导致无限循环。 他的批次是这样的(伪代码):
IF "no admin privileges?"
"write a VBS that calls this batch with admin privileges"
ELSE
"execute actual commands that require admin privileges"
正如你所看到的,这会导致一个无限循环,如果VBS失败请求管理员权限。
然而,可能会出现无限循环,虽然管理员特权时已成功申请。
在本Gripka的批处理文件检查是在容易出错。 我与一批发挥各地,并指出,管理权限是可用虽然检查失败。 有趣的是,检查和预期一样,如果我开始从Windows资源管理器的批处理文件,但它没有当我从IDE启动它。
因此,我建议使用两个独立的批处理文件。 第一生成调用第二个批处理文件的VBS:
@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"
第二个,命名为“my_commands.bat”,并在同一目录中第一个包含您的实际的命令:
pushd "%CD%"
CD /D "%~dp0"
REM Your commands which require admin privileges here
这不会引起无限循环,并消除了容易出错的管理员权限检查。
我知道这是不是OP的解决方案,但因为我敢肯定,还有很多其他的用例在这里,我想我也有同感。
我已经受够了在这些答案中的所有代码示例问题,但后来我发现: http://www.robotronic.de/runasspcEn.html
它不仅可以让你以管理员运行,它会检查该文件以确保它不被篡改,并安全地存储所需要的信息。 我承认这不是弄清楚如何使用最为明显的工具,但对于我们这些写代码应该足够简单。
@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
将变更为包含批处理文件的目录) 既然我有这个脚本弹出了自己的新的命令提示符再次运行,在无限循环(使用Win 7的专业版)的麻烦,我建议你尝试另一种方法: 我怎样才能自动提升我的批处理文件,所以它从请求UAC管理员权限如果需要的话?
小心,你必须在脚本的末尾添加此,作为一个编辑说,让你又回到了脚本目录的权限均升高后:CD / d%〜DP0
基于后由toster-CX和此页面上的其他有趣的帖子,我上如何配置和解决我的问题的见解。 我有类似的问题,我真希望磁盘清理实用程序在午餐时间(比如下午2点),每周运行一次两次在周一和周四。 然而,这需要提升权限。
共享批处理文件,这可能会帮助其他新手像我这样的 -
@echo off
echo Welcome to scheduling 'PC Maintenance Activity'
ping localhost -n 3 >nul
echo -- Step - 1 of 3 : Please give 'Admin' rights on next screen
ping localhost -n 5 >nul
if not "%1"=="am_admin" (powershell start -verb runas '%0' am_admin & exit)
cls
echo -- Step - 2 of 3 : In next screen, select temp areas for cleaning
during routine scheduled activity
ping localhost -n 3 >nul
C:\Windows\System32\cleanmgr.exe /sageset:112
cls
echo Now scheduling maintenance activity...
SchTasks /Create /SC WEEKLY /D MON,THU /TN PC_Cleanup /TR
"C:\Windows\System32\cleanmgr.exe "/sagerun:112 /ST 14:00
cls
echo -- Thanks for your co-operation --
echo -- Maintenance activity is scheduled for --
echo -- Every Monday and Thursday at 2 pm --
ping localhost -n 10 >nul
非常感谢这个论坛,Rems的张贴在这里[ https://www.petri.com/forums/forum/windows-scripting/general-scripting/32313-schtasks-exe-need-to-pass-parameters-to-script ] [1]
他帮助后配置可选参数,而调度的任务。
你不能从一个批处理文件,要求管理员权限,但你可以写在%TEMP%在Windows脚本宿主脚本并运行(而这又执行批处理作为管理员)你想调用的程序中的ShellExecute方法。与“运行方式”作为动词Application对象
使用Runas命令。 但是,我不认为你可以很容易地通过电子邮件发送.bat文件。