在批处理脚本中请求 uac 提升不起作用

问题描述 投票:0回答:0

我没有改变任何东西。我从这个网站得到了代码,忘记了确切的线程,但这是代码:

:: check if we are admin
:-------------------------------------
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%"
:--------------------------------------

几个月前我让它工作了,但是当我回来尝试它时,我得到了一个访问被拒绝的错误,即使它弹出了 uac 框并且我输入了管理员密码。我环顾四周,发现其他一些代码也做同样的事情,但有更多的代码,但它设置了批处理文件的主文件夹。当我选择清除打印假脱机的部分时,它删除了批处理文件开始的文件夹中的所有内容。这是我接下来尝试提升的代码,批处理的其余部分在下面:

:: check if we are admin
:-------------------------------------
::::::::::::::::::::::::::::::::::::::::::::
:: Elevate.cmd - Version 4
:: Automatically check & get admin rights
:: see "https://stackoverflow.com/a/12264592/1016343" for description
::::::::::::::::::::::::::::::::::::::::::::
 @echo off
 CLS
 ECHO.
 ECHO =============================
 ECHO Running Admin shell
 ECHO =============================

:init
 setlocal DisableDelayedExpansion
 set cmdInvoke=1
 set winSysFolder=System32
 set "batchPath=%~dpnx0"
 rem this works also from cmd shell, other than %~0
 for %%k in (%0) do set batchName=%%~nk
 set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
 setlocal EnableDelayedExpansion

:checkPrivileges
  NET FILE 1>NUL 2>NUL
  if '%errorlevel%' == '0' ( goto gotPrivileges ) else ( goto getPrivileges )

:getPrivileges
  if '%1'=='ELEV' (echo ELEV & shift /1 & goto gotPrivileges)
  ECHO.
  ECHO **************************************
  ECHO Invoking UAC for Privilege Escalation
  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%"
  
  if '%cmdInvoke%'=='1' goto InvokeCmd 

  ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
  goto ExecElevation

:InvokeCmd
  ECHO args = "/c """ + "!batchPath!" + """ " + args >> "%vbsGetPrivileges%"
  ECHO UAC.ShellExecute "%SystemRoot%\%winSysFolder%\cmd.exe", args, "", "runas", 1 >> "%vbsGetPrivileges%"

:ExecElevation
 "%SystemRoot%\%winSysFolder%\WScript.exe" "%vbsGetPrivileges%" %*
 exit /B

:gotPrivileges
 setlocal & cd /d %~dp0
 if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul  &  shift /1)

 ::::::::::::::::::::::::::::
 ::START
 ::::::::::::::::::::::::::::
 REM Run shell as admin (example) - put here code as you like
:--------------------------------------

这是我试图提升运行的批处理文件。三件事中只有两件事需要提升。我在打印部分添加了暂停以调试它并查看它哪里出了问题,并注释掉了删除部分以进行测试。对我来说真正奇怪的部分是海拔看起来像它工作,因为我在 system32 文件夹中,但它现在无法更改打印假脱机的目录。当我打开提升的命令提示符并导航到打印假脱机时,它说访问被拒绝,但是当我使用 Windows 资源管理器并在 uac 框中输入凭据时,它让我进入并且我可以在那里删除文件。对我来说似乎问题是提升的 cmd 没有提升,即使它在 system32 路径中。

:menu
cls
ECHO.
ECHO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ECHO What would you like to do? Type 4 to exit.
ECHO ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ECHO.
ECHO 1 - Clear the print spooler
ECHO 2 - Restore network connectivity
ECHO 3 - Get computer information
ECHO 4 - Exit
ECHO.

SET /P M=Type 1, 2, 3, or 4 then press ENTER:
IF %M%==1 GOTO print
IF %M%==2 GOTO network
IF %M%==3 GOTO compinfo
IF %M%==4 GOTO EOF

:print
@echo off
net stop spooler 
pause
cd c:\windows\system32\spool\printers\ 
pause
echo %cd%
pause
::del /q *.* 
net start spooler
goto menu

:network
@echo off
ipconfig /release
ipconfig /renew
ipconfig /flushdns
ipconfig /registerdns
netsh dump
nbtstat -R
netsh int ip reset reset.log
netsh winsock reset
echo You need to restart now to finish configuration changes..
::set /p answer=Restart now?
::if %answer%="y" shutdown /r /t 0 else
::if %answer%="yes" shutdown /r /t 0 else
pause
goto menu

:compinfo
@echo off
REM set variables
set computer=
set system=
set manufacturer=
set model=
set serialnumber=
set osname=
set sp=
set cstring=
set ustring=
set pstring=

FOR /F "tokens=2 delims='='" %%A in ('wmic %cstring% %ustring% %pstring% OS Get csname /value') do SET computer=%%A
FOR /F "tokens=2 delims='='" %%A in ('wmic %cstring% %ustring% %pstring% OS Get csname /value') do SET system=%%A
FOR /F "tokens=2 delims='='" %%A in ('wmic %cstring% %ustring% %pstring% ComputerSystem Get Manufacturer /value') do SET manufacturer=%%A
FOR /F "tokens=2 delims='='" %%A in ('wmic %cstring% %ustring% %pstring% ComputerSystem Get Model /value') do SET model=%%A
FOR /F "tokens=2 delims='='" %%A in ('wmic %cstring% %ustring% %pstring% Bios Get SerialNumber /value') do SET serialnumber=%%A
FOR /F "tokens=2 delims='='" %%A in ('wmic %cstring% %ustring% %pstring% os get Name /value') do SET osname=%%A
FOR /F "tokens=1 delims='|'" %%A in ("%osname%") do SET osname=%%A
FOR /F "tokens=2 delims='='" %%A in ('wmic %cstring% %ustring% %pstring% os get ServicePackMajorVersion /value') do SET sp=%%A

echo done!

echo ----------------
echo System Name: %system%
echo Manufacturer: %manufacturer%
echo Model: %model%
echo Serial Number: %serialnumber%
echo Operating System: %osname%
echo Service Pack: %sp%
echo ----------------

REM Generate file
SET file="%~dp0%computer%.txt"
echo ---------------- > %file%
echo Details For %computer%: >> %file%
echo System Name: %system% >> %file%
echo Manufacturer: %manufacturer% >> %file%
echo Model: %model% >> %file%
echo Serial Number: %serialnumber% >> %file%
echo Operating System: %osname% >> %file%
echo Service Pack: %sp% >> %file%
echo ---------------- >> %file%

echo File created at %file%


REM request user to push any key to continue
pause
goto menu

任何帮助表示赞赏。我被彻底难住了。我什至创建了一个新的用户配置文件,看看它是否有效,但没有。

编辑:我在提升的命令提示符下运行 sfc/scannow 并且它运行了,所以它实际上是提升的,但它无法访问打印假脱机。

batch-file
© www.soinside.com 2019 - 2024. All rights reserved.