解决BAT文件以管理员权限运行有多种方法能做到。
但是,要做到,既要自动以管理员权限运行,同时又要做到不弹出黑窗口,二者兼得却不能做到。
下面几种代码,都能以管理员权限运行,即使试着加用了我所能得知的几种方法(包括所说的VBS)去解决 不弹黑窗口 的问题 ,却始终还是要弹出黑窗口来。或许是我没有搞得来
特来请教这里的大神是否有高招解决?
期待大神的热心援手指导, 谢谢您了!!
自动以管理员权限运行的代码之一:- @echo off
- net session >nul 2>&1
- if not "%errorLevel%" == "0" (
- echo Oops: This tools must run with administrator permissions!
- echo it will popup the UAC dialog, please click [Yes] to continue.
- echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
- echo UAC.ShellExecute "%~s0", "%*", "", "runas", 1 >> "%temp%\getadmin.vbs"
- "%temp%\getadmin.vbs"
- exit /b 2
- )
- netsh interface ipv4 set address "WLAN" static 192.168.1.111 255.255.255.0 192.168.1.1 1
- netsh interface ipv4 set dnsservers "WLAN" static 114.114.114.114 primary
- netsh interface ipv4 add dns "WLAN" ... index=2
复制代码 自动以管理员权限运行的代码之二:- @echo off
- >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
- if '%errorlevel%' NEQ '0' (
- echo 请求管理员权限...
- goto UACPrompt
- ) else ( goto gotAdmin )
- :UACPrompt
- echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
- echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
- "%temp%\getadmin.vbs"
- exit /B
- :gotAdmin
- netsh interface ipv4 set address name="WLAN" source=dhcp
- netsh interface ipv4 set dnsservers name="WLAN" source=dhcp
复制代码 自动以管理员权限运行的代码之三:- @echo off
- %1 %2
- ver|find "5.">nul&&goto :st
- mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :st","","runas",1)(window.close)&goto :eof
- :st
- copy "%~0" "%windir%\system32\"
- netsh interface ipv4 set address "WLAN" static 192.168.1.113 255.255.255.0 192.168.1.1 1
- netsh interface ipv4 set dnsservers "WLAN" static 114.114.114.114 primary
- netsh interface ipv4 add dns "WLAN" ... index=2
复制代码 自动以管理员权限运行的代码之四:- @echo off
- :: BatchGotAdmin
- :-------------------------------------
- REM --> Check for permissions
- >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"
- echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
- "%temp%\getadmin.vbs"
- exit /B
- :gotAdmin
- if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
- pushd "%CD%"
- CD /D "%~dp0"
- :--------------------------------------
- netsh interface ipv4 set address name="WLAN" source=dhcp
- netsh interface ipv4 set dnsservers name="WLAN" source=dhcp
复制代码 ——另外,以上几个代码运行时,都会弹出那个UAC提示框出来,且必须要点击 是 才能继续运行,请问如何才能让其不弹这个提示框且能继续运行呢? |