Search code examples
windowsbatch-filewmic

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


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.


Solution

  • @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.