Windows Batch : query wmic computersystem get model if equals true continue with windows batch

喜你入骨 提交于 2019-12-13 07:43:02

问题


Looking to write a script that will query wmic computersystem get model and if the value is true continue with batch file if not true echo Un-compatible system exiting, this is what i have so far

    @echo off

        for /F "skip=1" %%? in ('wmic computersystem get model') do "(

      if %%?' equ Test' ('goto start') else (

      if %%?' equ test1' ('goto start') else (

      if %%?' equ test2' ('goto start') else (

      if %%?' equ test3' ('goto start') else (

      echo un-compatible System Exiting...>nul)))))"

:start

start of script

im generally ok with scripting but never used if statements so kinda lost on this one.

Any help will be very welcomed.


回答1:


@echo off
    setlocal enableextensions disabledelayedexpansion

    for /f "tokens=2 delims==" %%a in (
        'wmic computersystem get model /value'
    ) do for /f "delims=" %%b in ("%%~a") do for %%m in (
        "model1" "model2" "model3" "model4"
    ) do if /i "%%~b"=="%%~m" (
        set "model=%%~m"
        goto start
    )

    echo un-compatible system
    goto :eof

:start
    echo Start of script for model [%model%]

Where the for loops are

  • %%a to retrieve the model
  • %%b to remove the ending carriage return in the value returned (wmic behaviour)
  • %%m to iterate over the list of allowed models

If any of the allowed models matches the one retrieved with wmic, the code jumps to the start label, else, the inner for loop ends and as no match has been found the script ends.

This can be simplified as

>nul (wmic computersystem get model |findstr /i /l /c:"model1" /c:"model2" /c:"model3")||(
    echo un-compatible system
    goto :eof
)
echo compatible system

Where a conditional execution operation is used to determine if the findstr command fails to found any of the models and cancel the execution.

Of course, you can use the cascade of if /else, but the syntax is a little different

for /f "tokens=2 delims==" %%a in (
    'wmic computersystem get model /value'
) do for /f "delims=" %%b in ("%%~a") do (
    if /i "%%~b"=="test1" goto start 
    if /i "%%~b"=="test2" goto start 
    if /i "%%~b"=="test4" goto start 
)
echo un-compatible system
goto :eof

or

for /f "tokens=2 delims==" %%a in (
    'wmic computersystem get model /value'
) do for /f "delims=" %%b in ("%%~a") do (
           if /i "%%~b"=="test1" ( goto start 
    ) else if /i "%%~b"=="test2" ( goto start 
    ) else if /i "%%~b"=="test3" ( goto start
    ) else (
        echo un-compatible system
        goto :eof
    )
)

or

for /f "tokens=2 delims==" %%a in (
    'wmic computersystem get model /value'
) do for /f "delims=" %%b in ("%%~a") do (
    if /i "%%~b"=="test1" ( 
        goto start 
    ) else if /i "%%~b"=="test2" ( 
        goto start 
    ) else if /i "%%~b"=="test3" ( 
        goto start
    ) else (
        echo un-compatible system
        goto :eof
    )
)

or whatever other combination/style that better fits your code, but you have to take in consideration that the placement of the parenthesis matters. The if opening parenthesis needs to be on the same line that the if command. The if closing parenthesis needs to be in the same line that the else clause (if present). The else opening parenthesis needs to be in the same line that the else clause.




回答2:


All @MC ND solutions are workfull. But you can give this a try.

@echo off

::Defining the supported model
set $test="935gcm-s2c" "45687-98a" "57687-sdf"

for /f "tokens=2 delims==" %%a in ('wmic computersystem get model /value') do set "$Mod=%%a"
for %%a in (%$test%) do if /i %%a=="%$mod%" goto:ok
echo System not suppoprted
exit/b

:ok
echo System supported 


来源:https://stackoverflow.com/questions/29387036/windows-batch-query-wmic-computersystem-get-model-if-equals-true-continue-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!