批处理脚本无法通过上下文菜单运行

批处理脚本无法通过上下文菜单运行

我有一个使用批处理脚本防守方控制切换(启用和禁用)Windows Defender。它运行正常,但当我将其添加到右键单击上下文菜单时,它不起作用。脚本有问题吗?

Defender Control 使用以下 2 个命令行:

Defendercontrol.exe /E——启用 Windows Defender

Defendercontrol.exe /D - 禁用 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"
)

我尝试增加海拔,但仍然不起作用

@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"
)

我使用以下方法将 BAT 转换为 EXEBat 到 Exe 转换器

然后我用这个 reg 文件将其添加到注册表中。

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"

答案1

使脚本从上下文菜单运行的解决方案是提升它。谢谢肖恩你的回答

@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"
)

相关内容