สคริปต์ Cmd ทำงานไม่ถูกต้อง

Sep 12 2020

ฉันสร้างสคริปต์ที่เพิ่มโหมดความเข้ากันได้ผ่านทางcmdแต่ฉันมีปัญหาในการทำให้มันใช้งานได้:

Reg Add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "%CD%\mha2.exe" /t REG_SZ /d "WINXPSP3 256COLOR 640X480" /f

:: # For testing only:
   MkDir Success
   Exit
  • เมื่อเรียกใช้สคริปต์ผ่าน GUI ไดเร็กทอรีSuccessจะถูกสร้างขึ้น แต่ไม่ได้ตั้งค่าโหมดความเข้ากันได้ ไม่สำคัญว่าฉันจะเรียกใช้สคริปต์โดยมี / ไม่มีสิทธิ์ผู้ดูแลระบบ
  • เมื่อเรียกใช้สคริปต์ด้วยสิทธิ์ของผู้ดูแลระบบผ่านสิ่งต่อไปนี้มันจะทำทุกอย่างที่ควร:
    Start "C:\Path\To\script.bat"
    

เหตุใดจึงเป็นเช่นนี้และฉันจะแก้ไขได้อย่างไร

คำตอบ

1 AlphaInc. Sep 12 2020 at 15:41

หลังจากไม่กี่ชั่วโมงของการทำงานฉันคิดว่ามันออก: สิทธิ์ของผู้ดูแลระบบจะต้องเริ่มต้นสคริปต์เมื่อรวมทั้งหนึ่งซับก่อนReg Addขณะที่จะเริ่มต้นในcmd%WinDir%\system32

  • ในกรณีที่มีคนต้องการทำเช่นเดียวกัน:
    @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"
    :--------------------------------------
    
    Reg Add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "%CD%\mha2.exe" /t REG_SZ /d "WIN98 256COLOR 640X480" /f
    
ItWasn'tMe Sep 17 2020 at 10:16
@echo off && Setlocal EnableDelayedExpansion

2>nul "%__APPDIR__%whoami.exe" /groups | find "S-1-16-12288" >nul && goto=:gotAdmin 

echo\Requesting administrative privileges... && set "_args=%*" && set "_flag=1" && >"%temp%\getadmin.vbs" ^
echo=CreateObject("Shell.Application"^).ShellExecute "%~sdpnx0", "!_args:"=""!", "RunAsAdministrator", "runas", 1 :WScript.Quit 

%__AppDir__%cscript.exe "%temp%\getadmin.vbs"|find/v "." & echo\<nul & cd. & 2>nul del /q /f "%temp%\getadmin.vbs" && goto=:EOF 

:gotAdmin
cd /d "%~dp0" && set "_Reg_Key=HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"
%__AppDir__%reg.exe add "!_Reg_Key!" /v \""!CD!\Launcher.exe\"" /t REG_SZ /d "WIN98 256COLOR 640X480" /f
 
2>&1 %__AppDir__%reg.exe Query "!_Reg_Key!"|%__AppDir__%findstr.exe /i Launcher.exe.*WIN98.256COLOR.640X480 && ^
echo\Requesting admin works || echo\Not works, sorry... & %__AppDir__%timeout.exe /t -1 && endlocal && goto=:EOF

วิธีการเกี่ยวกับคำสั่งอื่นเพื่อลดความซับซ้อนในการตรวจสอบสิทธิ์ผู้ดูแลระบบ:

ExecuteCMD && return 0 and I'm the admin and goto label :gotAdmin
The last command was unsuccessful because I'm not the admin and now I'll create and call vbs file... 


Whoami.exe | find "S-1-16-12288" && goto=:gotAdmin 
The last command was unsuccessful because I'm not the admin and now I'll create and call vbs file...