使用IF条件

时间:2014-05-24 13:53:33

标签: windows batch-file cmd

我想查询远程计算机,如果在systeminfo输出中找到KB22334358,则退出,否则将主机名写入failed.txt

为什么这不起作用?

systeminfo /s "remotenamemachine" | find "KB22334358"
if %errorlevel% equ 1 goto nome else goto exit
:nome
systeminfo | find "Nome host" > C:\failed.txt
:exit
exit

1 个答案:

答案 0 :(得分:0)

似乎您下次连接时可能还需要使用/s开关。此外,您可能希望使用find使/i命令不区分大小写。您可能还希望附加到failed.txt文件,如下所示:

systeminfo /s "remotenamemachine" | find "KB22334358"
if %errorlevel% equ 1 goto nome else goto exit
:nome
systeminfo /s "remotenamemachine" | find /i "Nome host" >> C:\failed.txt
:exit
exit

或者你可以使用这个三重for /f循环,一次性完成所有这些:

KB_Checker.bat

@echo off
setlocal ENABLEDELAYEDEXPANSION
set comp_name=%*

for /f "usebackq tokens=1* delims=" %%I in (`
    systeminfo /s "%comp_name%"
`) do (
    for /f "usebackq tokens=2 delims=:" %%k in (`
        echo %%I ^| findstr /v "^)" ^| findstr "KB22334358"
    `) do (
        for /f "tokens=1* delims=" %%f in ('
            echo %%k ^| findstr /v "UTC"
        ') do (
            set temp_KB=%%f
        )
    )

    for /f "usebackq tokens=2 delims=:" %%h in (`
        echo %%I ^| findstr /i /C:"Nome host"
    `) do (
        for /f "tokens=1* delims=" %%f in ('
            echo %%h ^| findstr /v "UTC"
        ') do (
            set hm=%%f
        )
    )
) 2> nul

set KB=!temp_KB!
if not defined KB echo !hm! >> C:\failed.txt
exit

<强> 更新

我更新了上面的脚本以设置comp_name环境var。您可以将计算机名称传递给脚本,如下所示:KB_Checker.bat computer_1它将处理