Batch script doesn't work from contextual menu

-1

I have a batch script that uses Defender Control to toggle (enable and disable) Windows Defender. It works perfectly except when I add it to the right click context menu, it doesn't work. Is there something wrong with the script?

Defender Control works with this 2 command lines:

Defendercontrol.exe /E -- to enable Windows Defender

Defendercontrol.exe /D -- to disable Windows Defender

@echo off
REM get current status:
<"%userprofile%\defendercontrol.status" set /p status=
echo Currently: %status%
if "%status%"=="D" (
   defendercontrol.exe /E
   echo E>"%userprofile%\defendercontrol.status"
) else (
   defendercontrol.exe /D
   echo D>"%userprofile%\defendercontrol.status"
)

I tried adding elevation but still it doesn't work

@echo off
set ELEVATE_APP=Full command line without parameters for the app to run
set ELEVATE_PARMS=The actual parameters for the app
echo Set objShell = CreateObject("Shell.Application") >elevatedapp.vbs
echo Set objWshShell = WScript.CreateObject("WScript.Shell") >>elevatedapp.vbs
echo Set objWshProcessEnv = objWshShell.Environment("PROCESS") >>elevatedapp.vbs
echo objShell.ShellExecute "%ELEVATE_APP%", "%ELEVATE_PARMS%", "", "runas" >>elevatedapp.vbs
elveatedapp.vbs
DEL elevatedapp.vbs
REM get current status:
<"%userprofile%\defendercontrol.status" set /p status=
echo Currently: %status%
if "%status%"=="D" (
   defendercontrol.exe /E
   echo E>"%userprofile%\defendercontrol.status"
) else (
   defendercontrol.exe /D
   echo D>"%userprofile%\defendercontrol.status"
)

I convert the BAT to EXE using Bat To Exe Converter

Then I add it to the registry with this reg file.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Directory\Background\shell\Toggle Windows Defender]
"icon"="D:\\MY APPS\\SCRIPTS\\Toggle Windows Defender\\Toggle Windows Defender.exe"


[HKEY_CLASSES_ROOT\Directory\Background\shell\Toggle Windows Defender\command]
@="D:\\MY APPS\\SCRIPTS\\Toggle Windows Defender\\Toggle Windows Defender.exe"

freddie-o

Posted 2019-02-04T20:27:20.813

Reputation: 1

Please explain (1) why you have two scripts, (2) what they do, (3) how they do it, and (4) exactly what you mean by ‘‘It works perfectly except when I add it to the right click context menu, it doesn't work.’’  … … … … … … … … … … … … … … … … … … … … … … Please do not respond in comments; [edit] your question to make it clearer and more complete. – Scott – 2019-02-04T20:43:08.440

Add the full path of that exe file in batch script. – Biswapriyo – 2019-02-04T21:32:46.370

2

Welcome to Super User.  It would appear that you have accidentally created two accounts.  This will interfere with commenting, editing your own posts, and accepting an answer.  You should use the contact form and select “I need to merge user profiles” to have your accounts merged.  In order to merge them, you will need to provide links to the two accounts.  For your information, these are https://superuser.com/users/994081/freddie-o and https://superuser.com/users/994093/freddie-o.%E2%80%82 You’ll then be able to [edit] your question without needing to have your edits reviewed.

– Scott – 2019-02-04T22:02:35.577

Please explain (2) what your scripts do and (3) how they do it. (5) You have opened a can of worms. You have two (slightly) different scripts, and an EXE file generated from one of them. Do all three of these programs “work perfectly” when run from a CMD window? … … … … … … … … Please do not respond in comments; [edit] your question to make it clearer and more complete. – Scott – 2019-02-04T22:25:27.213

"%~f0:status" isnť a valid file name. – JosefZ – 2019-02-04T22:39:04.000

Most likely it's because Defender Control requires elevation. You can add elevation to your script or with Bat to Exe. – shawn – 2019-02-05T19:31:03.707

Answers

-1

The solution for making the script work from the context menu was to elevate it. Thanks shawn for your answer

@echo off
CLS
ECHO.

:init
setlocal DisableDelayedExpansion
set "batchPath=%~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 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%"
ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
"%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %*
exit /B

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

REM get current status:
<"%userprofile%\defendercontrol.status" set /p status=
echo Currently: %status%
if "%status%"=="D" (
   defendercontrol.exe /E
   echo E>"%userprofile%\defendercontrol.status"
) else (
   defendercontrol.exe /D
   echo D>"%userprofile%\defendercontrol.status"
)

freddie-o

Posted 2019-02-04T20:27:20.813

Reputation: 65