首页 > 解决方案 > 在 Windows 批处理文件中静音 For 的命令输出

问题描述

如果存在 SRV 记录,则以下代码片段完全符合我的要求,即打印出带有 SRV 记录的每个服务器。但是,如果未找到 SRV 记录(在某些情况下这是预期的,我会看到错误。

:: Execute a Nslookup for the internal SRV Record and collect the FQDNs of the response
:TESTPUBLICDNS
for /f "usebackq tokens=1,2 delims=" %%f in (`c:\Windows\System32\nslookup.exe -q^=srv %publicSRV%%domain% ^| findstr /R /C:"hostname"`) do (
    set "hostFQDNFull=%%f"
    
    :: Split the result to extract just the FQDN of the servers in the SRV Record
    for /f "usebackq tokens=1,2 delims==" %%a in (`echo !hostFQDNFull!`) do set label=%%a&set hostFQDN=%%b
        set hostFQDN=!hostFQDN: =!
        echo !hostFQDN!
)

因此,如果存在 SRV 记录,我将得到:

server1.wdgets.com
server2.wdgets.com
server3.wdgets.com

但如果不是,我得到:

*** dnsserver.local can't find _sanitizedsrv._tcp.widgets.com: Non-existent domain

当结果是这样时,我想使“不存在”的域输出静音。我似乎无法弄清楚如何做到这一点。如果我将 For 循环重定向到 Null,我会丢失 Do 部分中的结果。

标签: windowsloopsfor-loopbatch-filecommand

解决方案


推荐阅读