代码之家  ›  专栏  ›  技术社区  ›  Abob

CMD批处理文件出现嵌套IF语句和变量问题

  •  0
  • Abob  · 技术社区  · 7 年前

    我正在编写一个cmd批处理文件,该文件查询windows注册表,然后返回一个包含输出的文件。我想查询的密钥是关于SCHANNEL的SSL。

    我希望输出读取正确,而不一定是特定的reg值,因此,如果该变量与xyz匹配,则将该值转换为变量,或者将另一个变量设置为“disabled”或echo“SSLv2 is disabled”,从而解释reg值是什么,例如,如果0x1是我希望输出为echo“SSLv2 is disabled”而不是“SSLv2 is 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 回复  |  直到 7 年前
        1
  •  0
  •   Magoo    7 年前
    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 。你的 )) 关闭 内部的 如果 声明,因此 else 未与配对 如果


    (我是一个爱大便的人……)

    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%
    

    易于理解的或者你需要的复杂程度。

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

        2
  •  0
  •   Compo    7 年前

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

    @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 ARK帮助您学习:

    @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