简体   繁体   English

批量拆分和比较字符串

[英]Splitting and comparing strings with batch

I essentially need to take this output from a netdom query fsmo: 我本质上需要从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

And check that the server I am running my script against, is in fact the PDC holder. 并检查我运行脚本的服务器是否实际上是PDC持有者。 So essentially the logic would be: 因此,实质上的逻辑是:

  • Get current hostname and store in variable netdom query fsmo | find "PDC" 获取当前主机名并将其存储在变量netdom query fsmo | find "PDC" netdom query fsmo | find "PDC" and store in variable netdom query fsmo | find "PDC"并存储在变量中
  • Compare the actual hostname against the hostname in the query variable above 将实际的主机名与上面的查询变量中的主机名进行比较
  • IF true do this, IF false do that. 如果为真,则为false。

I'm really not that familiar with batch (more of a PowerShell guy) and was wondering if there is an easy way to achieve this in batch? 我真的不那么熟悉批处理(更多是PowerShell方面的人),并且想知道是否有简单的方法来批量实现?

This should get you started. 这应该使您入门。 It assumes that netdom query fsmo is the exact command you run and produces the output exactly as noted in your question. 它假定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

In theory, 理论上,

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

If you are running directly from the prompt, reduce all %% to % . 如果直接从提示运行,请将所有%%减少到%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM