Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如果UAC提示被拒绝,则正常运行批处理文件_Batch File_Uac - Fatal编程技术网

Batch file 如果UAC提示被拒绝,则正常运行批处理文件

Batch file 如果UAC提示被拒绝,则正常运行批处理文件,batch-file,uac,Batch File,Uac,我想做一个批量文件,提示管理员权限,但不只是阅读,直到最后才张贴pls。通常,我通过批处理文件创建vbs脚本,该文件将提示管理员权限,但问题是,如果用户在UAC窗口中选择“否”,批处理文件将不会运行,我希望它在有或没有管理员权限的情况下运行。我的代码错误/不完整: @echo off if "%1" == "S" goto skip ::checks if the user already have admin rights, in this case no prompt needed ne

我想做一个批量文件,提示管理员权限,但不只是阅读,直到最后才张贴pls。通常,我通过批处理文件创建vbs脚本,该文件将提示管理员权限,但问题是,如果用户在UAC窗口中选择“否”,批处理文件将不会运行,我希望它在有或没有管理员权限的情况下运行。我的代码错误/不完整:

@echo off

if "%1" == "S" goto skip

::checks if the user already have admin rights, in this case no prompt needed
net file 1>nul 2>nul
if '%errorlevel%' == '0' goto skip

::create vbs script from which the batch file should run
echo Set UAC = CreateObject^("Shell.Application"^) > runasAdmin.vbs
echo UAC.ShellExecute "%~0", "S", "", "runas", 1 >> runasAdmin.vbs
start "" runasAdmin.vbs
::here, a code should check if the batch file is running as admin in another process
::if not, goto skip
exit /b

:skip

::commands and stuff...

检查进程是否以管理员身份运行的问题是,非管理员将无法看到管理员进程。i、 e.管理流程受到保护,不受非高架眼的影响。所以,这是行不通的

ShellExecute在针对进行编码时返回一个HWnd,该值可用于检查错误,但当然,这也不适用于shell.com应用程序对象。因此,您不能检查返回值并为bat检查一个好的WScript.Quiterr值

使用start runasAdmin.vbs意味着您的bat文件直接进入下一行,而不是等到UAC提示完成。使用start/wait或更简单的cscript-nologo可以确保bat等待直到提示被应答

因此,您无法使用VBS或通过检查进程来检查提升的进程创建的成功/失败。我所能想到的就是使用临时文件创建作为锁定机制。为了实现这一点,提升的进程需要创建一个临时文件,非提升的进程需要检查该临时文件,因此需要通过本地主机ping在每个临时文件中实现一个小的休眠

@echo off
setlocal

rem Note: Any user entered parameter will run unelevated
set runningFile=%1
if not defined runningFile goto checkElevation
echo Running as admin > %runningFile%
goto skip

:checkElevation
rem checks if the user already have admin rights, in this case no prompt needed
net file 1>nul 2>nul
if '%errorlevel%' == '0' goto skip

rem create vbs script from which the batch file should run
set filename=%tmp%\RunAsAdmin%random%
set runningFile="%filename%.lck"
set vbscript="%filename%.vbs"
echo Set UAC = CreateObject^("Shell.Application"^) > %vbscript%
echo UAC.ShellExecute "%0", %runningFile%, "", "runas", 1 >> %vbscript%
cscript -nologo %vbscript%
rem sleep 1 to allow elevated version to create lock file
ping -n 2 127.0.0.1 > NUL
del %vbscript%
rem check if running file has been created
if not exist %runningFile% goto skip
if exist %runningFile% del /f %runningFile%
exit /b

:skip
rem sleep 1 so that non elevated version can check lock file created
ping -n 2 127.0.0.1 > NUL
echo Running other things here %1

if exist %runningFile% del /f %runningFile%
pause
rem commands and stuff...

我优化了以下代码:

@echo off
@title 
@NET FILE 1>nul 2>nul
@if %ERRORLEVEL% neq 0 (
    @if not defined UAC (
        @set UAC=1;
    ) else (
        @echo. 
        @echo UAC elevating failed !
        @echo.
        @pause
        @exit
    )
    @echo CreateObject^("Shell.Application"^).ShellExecute "%~f0", "%*", "%~dp0", "runas", 1 > %temp%\UACtemp.vbs
    @%temp%\UACtemp.vbs
    @del /f /q %temp%\UACtemp.vbs
    @exit
)
@cd /d %~dp0
@echo on
::command under this line

使用2个脚本,第一个检查管理员权限,如果确定,它将运行第二个提升脚本,否则它将运行非提升脚本。如果未提升,我希望它提示管理员权限,如果用户选择“否”,它将正常运行,或者提升所有这些@符号是怎么回事?只需第一次@echo关闭就足够了。您在发布程序之前测试过吗?如果你不知道如何编程,不要在互联网上玩smartass。