检查Windows批处理中的注册表值

时间:2014-04-23 11:11:18

标签: batch-file cmd

我想在.bat文件中检查一些Windows注册表项及其值的存在。

到目前为止,我设法检查存在:

@echo off

set SMB2_REGKEY=HKLM\SYSTEM\CurrentControlSet\services\LanmanWorkstation\Parameters
set SMB2_REGVAL1=FileInfoCacheLifetime
set SMB2_REGVAL2=FileNotFoundCacheLifetime
set SMB2_REGVAL3=DirectoryCacheLifetime

REM Check for presence of key first.
reg query %SMB2_REGKEY% /v %SMB2_REGVAL1% 2>nul || (echo Error! & exit /b 1)
reg query %SMB2_REGKEY% /v %SMB2_REGVAL2% 2>nul || (echo Error! & exit /b 1)
reg query %SMB2_REGKEY% /v %SMB2_REGVAL3% 2>nul || (echo Error! & exit /b 1)

我现在如何检查三个值(FileInfoCacheLifetime,FileNotFoundCacheLifetime,DirectoryCacheLifetime)的值是否设置为零?

1 个答案:

答案 0 :(得分:2)

@echo off
    setlocal enableextensions enabledelayedexpansion

    set "key=HKLM\SYSTEM\CurrentControlSet\services\LanmanWorkstation\Parameters"
    for %%v in (FileInfoCacheLifetime FileNotFoundCacheLifetime DirectoryCacheLifetime) do (
        set "%%~v="
        for /f "tokens=3" %%a in ('reg query "%key%" /v "%%~v" 2^>nul ^| find "REG_DWORD"') do set /a "%%~v=%%a"
        if not defined %%~v (
            echo %%~v is not defined
        ) else if not !%%~v! equ 0 (
            echo %%~v is not correctly defined
        ) else (
            echo %%~v is correctly defined
        )
    )

    endlocal