TransWikia.com

Cmd script does not work properly

Super User Asked by AlphaInc. on February 26, 2021

I created a script that adds Compatibility Mode via cmd, but I’m having problems getting it to work:

Reg Add "HKLMSOFTWAREMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers" /v "%CD%mha2.exe" /t REG_SZ /d "WINXPSP3 256COLOR 640X480" /f

:: # For testing only:
   MkDir Success
   Exit
  • When executing the script via the GUI, directory Success gets created, but Compatibility Mode is not set; it does not matter if I run the script with/without Admin privileges
  • When executing the script with Admin privileges via the following, it does everything it should:
    Start "C:PathToscript.bat"
    

Why is this the case and how do I fix it?

2 Answers

@echo off && Setlocal EnableDelayedExpansion

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

echoRequesting 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=HKLMSOFTWAREMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers"
%__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 && ^
echoRequesting admin works || echoNot works, sorry... & %__AppDir__%timeout.exe /t -1 && endlocal && goto=:EOF

How about another command to simplify the verification of administrator privilege:

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...  

Answered by It Wasn't Me on February 26, 2021

After a few hours of work, I figured it out: Admin privileges are required to start the script when including a one-liner before Reg Add, as cmd will start it in %WinDir%system32.

  • In case somebody wants to do the same thng:
    @echo off
    
    :: # BatchGotAdmin
    :-------------------------------------
    REM # --> Check for permissions
      if "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
        >nul 2>&1 "%SystemRoot%SysWOW64cacls.exe" "%SystemRoot%SysWOW64configsystem"
      ) else (
        >nul 2>&1 "%SystemRoot%system32cacls.exe" "%SystemRoot%system32configsystem"
      )
    
    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 "HKLMSOFTWAREMicrosoftWindows NTCurrentVersionAppCompatFlagsLayers" /v "%CD%mha2.exe" /t REG_SZ /d "WIN98 256COLOR 640X480" /f
    

Answered by AlphaInc. on February 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP