如何将批处理文件写入 return WMIC 结果

How to write a batch file to return a WMIC result

如果主板制造商是 ASUSTek COMPUTER INC.,如何将批处理文件写入 return true,如果不使用 wmic 命令,如何将批处理文件写入 return false?

命令如下:

wmic baseboard get Manufacturer

它 returns:

Manufacturer
ASUSTeK COMPUTER INC.

而我只需要比较字符串ASUSTeK COMPUTER INC..

要捕获 wmic 命令的输出,请使用 for /F:

set "BOARD="
for /F "skip=1 delims=" %%I in ('
    wmic BaseBoard get Manufacturer
') do (
    for /F "delims=" %%J in ("%%I") do (
        set "BOARD=%%J"
    )
)
rem // Compare retrieved string:
if /I "%BOARD%"=="ASUSTeK COMPUTER INC." (
    echo True
) else (
    echo False
)

两个嵌套的 for /F 循环是正确转换 Unicode wmic 输出所必需的。


但是,您也可以直接过滤 wmic 输出,如下所示:

wmic BaseBoard where "Manufacturer='ASUSTeK COMPUTER INC.'" get Manufacturer 2>&1 > nul | find /V "" > nul && (echo False) || (echo True)

where clause does the filtering; if no match is found, No Instance(s) Available. is returned on the STD_ERR stream (handle 2). The expression 2>&1 > nul suppresses the STD_OUT stream (handle 1) and redirects STD_ERR to STD_OUT instead, so find is going to receive it; the search expression /V "" finds a match when the stream is not empty. find returns an exit code of 0 if a match is found and 1 otherwise; the operators &&|| 检查退出代码并有条件地执行相应的 echo 命令。