嵌套IF语句和变量

时间:2018-03-24 13:51:25

标签: batch-file cmd registry

我正在编写一个cmd批处理文件,用于查询Windows注册表,然后使用输出报告回文件。我想查询的密钥是围绕SSL的SCHANNEL。

我希望输出读取正确而不一定是特定的寄存器值,因此如果该变量与xyz匹配,则将值转换为变量,或者将另一个变量设置为“禁用”'或者回声' SSLv2被禁用'所以更确切地说是对reg值的解释,例如,如果0x1是我希望输出回显的值,则SSLv2被禁用'不是' SSLv2是0x1'。

如果根本没有存在注册表项,我也难以嵌套if语句,即空或只显示' SSLv2已启用'。

以下是客户端SSLv2的一个示例。

For /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set mydate=%%a-%%b-%%c)
For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)
FOR /F "tokens=* USEBACKQ" %%F IN (`hostname`) DO SET hostname=%%F
set hostnamefolder=%~dp0\%hostname%
mkdir %hostnamefolder%\logs
mkdir %hostnamefolder%\logs\files
mkdir %hostnamefolder%\logs\sceenshots
set workingdir=%~dp0

set Logfile=%hostnamefolder%\%hostname%_BRSIS.txt
set curdir=%~dp0
If Exist %Logfile% Del %Logfile%
@echo On
setlocal ENABLEEXTENSIONS

REM ======================================
REM SSLv2 configuration for Client:
REM ======================================

FOR /F "usebackq skip=2 tokens=1,3" %%A IN (`reg query "HKLM\System\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\SSL 2.0\Server" /v DisabledByDefault 2^>nul`) DO (
    set ValueName=%%A
    set ValueValue=%%B
)

echo %valueName%
echo %ValueValue%

if defined ValueName (
        if %ValueValue% EQU 0x1 (
            echo SSLv2 protocol for client connections is disabled >> %Logfile%
        ) else (
        if %valueValue% EQU 0x0 (
            echo SSLv2 protocol for client connections is enabled >> %Logfile%
        )) else (
            echo SSLv2 protocol for client connections are enabled >> %Logfile%
        )

我不是程序员,所以请原谅我的编码,但我很想学习。谢谢你提前。

2 个答案:

答案 0 :(得分:0)

if defined ValueName (
   if defined Valuevalue (
     if "%ValueValue%" EQU "0x1" (
        echo SSLv2 protocol for client connections is disabled >> %Logfile%
     ) else (
        if "%valueValue%" EQU "0x0" (
          echo SSLv2 protocol for client connections is enabled >> %Logfile%
        ) else (
          echo SSLv2 protocol for client connections are enabled >> %Logfile%
        )
     )
  ) else (
   echo valuevalue is not defined
  )
) else (
 echo valuename is not defined
)

关键是匹配缩进级别。您的代码缺少终端)以关闭最外面的if。您的))关闭了两个内部if语句,因此else未与if配对。

(我是个嗜好者...)

set "message=SSLv2 protocol for client connections are enabled"
if defined valuename if defined valuevalue (
   if "%valueValue%" EQU "0x1" set "message=SSLv2 protocol for client connections is enabled"
   if "%valueValue%" EQU "0x0" set "message=SSLv2 protocol for client connections is enabled"
)
echo %message%>>%logfile%

简单。或者根据需要复杂。

最好清楚地说明在什么情况下你想要的输出是什么。

答案 1 :(得分:0)

以下是一个可能有用的示例:

@Echo Off
Set "LogFile=Output.log"
Set "RK=HKLM\System\CurrentControlSet\Control\SecurityProviders"
Set "RK=%RK%\SCHANNEL\Protocols\SSL 2.0\Server"
Set "RV=DisabledByDefault"
Set "SO=SSLv2 protocol for client connections "
Set "DV=does not exist"
For /F "EOL=H Tokens=2*" %%A In ('Reg Query "%RK%" /V "%RV%" 2^>Nul'
) Do If "%%B"=="0x1" (Set "DV=is enabled") Else Set "DV=is disabled"
(Echo %SO%%DV%)>"%Logfile%"

这是你的代码重做了缩进和Rem arks来帮助你学习:

@Echo Off
Rem Ensure this script is in the current direcotry
CD /D "%~dp0"
Rem Setting local variables for use in this script
    Rem Date and time stamps
        Rem Undefine any existing variable named DS
        Set "DS="
    For /F "Tokens=1-5 Delims=/: " %%A In ('RoboCopy/NJH /L "\|" Null'
    ) Do If Not Defined DS (Set "DS=%%A-%%B-%%C" & Set "TS=%%D%%E")
    Rem Hostname
    For /F "Delims=" %%A In ('HostName') Do Set "HN=%%A"
    Rem Host folder
    Set "HF=%CD%\%HN%"
    Rem Log file
    Set "LF=%HF%\%HN%_BRSIS.txt"
    Rem SSLv2 registry key
    Set "RK=HKLM\System\CurrentControlSet\Control\SecurityProviders"
    Set "RK=%RK%\SCHANNEL\Protocols\SSL 2.0\Server"
    Rem SSLv2 target value
    Set "RV=DisabledByDefault"
    Rem Common SSLv2 output string
    Set "SO=SSLv2 protocol for client connections "
    Rem default SSLv2 output string ending [for missing value]
    Set "DV=does not exist"
Rem Creating necessary directories if missing
If Not Exist "%HF%\logs\files\" MD "%HF%\logs\files"
If Not Exist "%HF%\logs\sceenshots\" MD "%HFr%\logs\sceenshots"
Rem SSLv2 configuration for Client:
    Rem Check registry for SSLv2 key value
    For /F "EOL=H Tokens=2*" %%A In ('Reg Query "%RK%" /V "%RV%" 2^>Nul'
    ) Do If "%%B"=="0x1" (
        Rem define variable for SSLv2 output string ending [for enabled value]
        Set "DV=is enabled") Else (
        Rem define variable for SSLv2 output string ending [for disabled value]
        Set "DV=is disabled")
    Rem Output resulting string to log file
    (Echo %SO%%DV%)>"%LF%"
Rem Rest of code goes here