拆分和比较字符串与批处理

时间:2014-12-11 21:59:03

标签: windows batch-file

我基本上需要从netdom查询fsmo中获取此输出:

Schema Master           server1.domain.local
Domain Naming Master    server1.domain.local
PDC                     server1.domain.local
RID Pool Manager        server1.domain.local
Infrastructure Master   server1.domain.local

并检查我正在运行我的脚本的服务器,实际上是PDC持有者。所以基本上逻辑是:

  • 获取当前主机名并存储在变量netdom query fsmo | find "PDC"中并存储在变量
  • 将实际主机名与查询中的主机名进行比较
  • 之上的变量
  • 如果这样做是真的,如果不这样做。

我真的不熟悉批处理(更多的是PowerShell人),并且想知道是否有一种简单的方法可以批量实现这一点?

2 个答案:

答案 0 :(得分:1)

这应该让你开始。它假定netdom query fsmo是您运行的确切命令,并完全按照您的问题中的说明生成输出。

@ECHO OFF
SETLOCAL

REM Look for PDC entry. If it is found, take the last text value.
REM Note that FINDSTR is case sensitive without the /I switch.
FOR /F "usebackq tokens=1,2 delims= " %%A IN (`netdom query fsmo ^| FINDSTR /B PDC`) DO SET PdcServer=%%B

REM See if we found it.
IF "%PdcServer%"=="" GOTO :EOF 

REM If we get here, there is an entry.
ECHO PDC Server is %PdcServer%

REM Compare to the current server.
SET CurrentServer=%ComputerName%.%UserDnsDomain%
ECHO This Server name is %CurrentServer%

IF "%PdcServer%"=="%CurrentServer%" (
    REM This is the PDC server. Do Something.
) ELSE (
    REM Not the PDC server.
)

ENDLOCAL

答案 1 :(得分:1)

理论上,

for /f "tokens=1,2" %%a in ('netdom query fsmo') do if "%%a"=="PDC" if "%%b"=="actualhostname" (dothis) else (dothat)

如果您直接在提示符下运行,请将所有%%缩减为%