【问题标题】:Batch File: Syntax for IF ELSE批处理文件:IF ELSE 的语法
【发布时间】:2014-11-18 08:23:44
【问题描述】:

我的环境中有一台旧的 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");
}

谢谢

【问题讨论】:

  • 是什么让您无法使用 Google?而不是写下所有这些,你可以搜索......

标签: windows batch-file if-statement syntax


【解决方案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.

【讨论】:

  • 非常感谢这个 npocmaka。它是我需要的开始。我会让你知道我是怎么过的。再次感谢。
  • 几乎可以正常工作 - 只是遇到了问题
  • 几乎可以正常工作 - 只是遇到问题 - 如果 %nlquery% equ "DCName01"。当我在 CMD 中运行完整命令时,它返回 4 行结果。如何让脚本从 4 行结果中仅搜索“DCNAme01”。我试过 equ "(Star)DCNAME01(Star)" (我正在尝试使用开始符号,但编辑器将字体更改为粗体,我是这个论坛的新手 :))。星星没有工作。谢谢
  • @StuBow75 - 您可以使用FINDFINDSTR 过滤结果(我将编辑结果)。如果无法将字符串与通配符进行比较。如果您知道确切的行需要它会更好....
  • 我对 FINDSTR 进行了一些研究,但只能找到有关使用文件搜索的信息。我会试试上面的新代码,然后告诉你。再次感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-14
  • 1970-01-01
  • 2021-06-14
  • 2022-01-11
相关资源
最近更新 更多