2024年11月Win10让批处理文件自动获取管理员权限的方法

发布时间:

  ⑴:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

  ⑵:: 软媒魔方自动添加批处理文件管理员权限 ::

  ⑶:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

  ⑷echo off

  ⑸ECHO ================================

  ⑹ECHO 软媒魔方获取批处理文件管理员权限

  ⑺ECHO ================================

  ⑻setlocal DisableDelayedExpansion

  ⑼set "batchPath=%~"

  ⑽for %%k in (%) do set batchName=%%~nk

  ⑾set "vbsGetPrivileges=%temp%OEgetPriv_%batchName%.vbs"

  ⑿setlocal EnableDelayedExpansion

  ⒀:checkPrivileges

  ⒁ FILE >NUL >NUL

  ⒂if '%errorlevel%' == '' ( goto gotPrivileges ) else ( goto getPrivileges )

  ⒃:getPrivileges

  ⒄if '%'=='ELEV' (echo ELEV & shift / & goto gotPrivileges)

  ⒅ECHO ********************************

  ⒆ECHO 请求 UAC 权限批准……

  ⒇ECHO ********************************

  ⒈ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"

  ⒉ECHO args = "ELEV " >> "%vbsGetPrivileges%"

  ⒊ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"

  ⒋ECHO args = args ^& strArg ^& " " >> "%vbsGetPrivileges%"

  ⒌ECHO Next >> "%vbsGetPrivileges%"

  ⒍ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", >> "%vbsGetPrivileges%"

  ⒎"%SystemRoot%SystemWScript.exe" "%vbsGetPrivileges%" %*

  ⒏:gotPrivileges

  ⒐setlocal & pushd .

  ⒑cd /d %~dp

  ⒒if '%'=='ELEV' (del "%vbsGetPrivileges%" >nul >nul & shift /)

  ⒓:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

  ⒔:: 以下为需要运行的批处理文件代码 ::

  ⒕:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

  ⒖rem 本行以下可修改为你需要的bat命令(从上面三行冒号开始到下面都可删改

  ⒗ECHO 欢迎使用软媒魔方!