批处理文件:IF ELSE的语法

时间:2014-11-18 08:23:45

标签: windows batch-file if-statement syntax

我的环境中有一台旧的Windows Server 2000服务器,不会对服务器2012 DC进行身份验证。虽然我们将此服务器升级,但我必须确保它始终指向Server 2008 R2 DC。我提出的最佳解决方案是一个基本的批处理文件,我将每隔5分钟运行一次(计划任务),首先检查它所指向的DC并在必要时进行更改。我还想将这些记录到标有时间和日期的文本文件

我知道我想编写的脚本,但不知道在批处理文件中用于IF ELSE的语法。您可以忽略命令实际执行的操作我对语法

更感兴趣

我试图用JavaScript编写脚本

if(nltest /server:ServerName123 /sc_query:branch === DCName01) 
{
\\I would like it to do nothing apart from write to a text file that it succeeded

    Print to a text file ("Ponting to DCNAME01 with time and date");
{
else
{
\\I would like it to run the following command to point the server to the correct DC

nltest /server:ServerName123 /sc_reset:branch\DCName01;
Print to a text file "("Changed to DCNAME01 with time and date");
}

感谢

1 个答案:

答案 0 :(得分:1)

@echo off

rem set nltest query result  to variable
for /f "tokens=* delims=" %%# in ('nltest /server:ServerName123 /sc_query:branch') do (

    set "nlquery=%%#"
)


if "%nlquery%" equ "DCName01" (

    echo Ponting to DCNAME01 with time and date >some.file
) else (
   nltest /server:ServerName123 /sc_reset:branch\DCName01
   echo Changed to DCNAME01 with time and date >some.file   
)

修改 - using conditional execution

@echo off

nltest /server:ServerName123 /sc_query:branch | find /i "DCName01" 2>nul 1>nul && (
    echo Ponting to DCNAME01 with time and date >some.file
) || (
    nltest /server:ServerName123 /sc_reset:branch\DCName01
    echo Changed to DCNAME01 with time and date >some.file  
)

More info.