38895

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

Question:

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.

Answer1:

<pre class="lang-dos prettyprint-override">@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

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

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

<pre class="lang-dos prettyprint-override">>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

<pre class="lang-dos prettyprint-override">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

<pre class="lang-dos prettyprint-override">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

<pre class="lang-dos prettyprint-override">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.

Answer2:

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

Recommend

  • Persisting client side data using JavaScript? [duplicate]
  • Find all files and copy to another destination keeping the folder structure
  • Dynamic forms in Django hosted by GAE
  • How to set a password for application in iOS [closed]
  • How to form hash from the contents of the following file?
  • Testing if a number has a decimal value using only the iostream library
  • How to implement database functionality effectively?
  • How can I find synonyms in estimated frequency order using JWNL(Wordnet Library)?
  • Why do the values inside the PriorityQueue change after the poll method in java? [duplicate]
  • SSIS - Is there a Data Flow Source component that will handle CSV files where the column order may c
  • GAE increase recepients emailed limit
  • Tap and hold to save image in UIWebview
  • “Eclipse-BundleShape: dir” header is not working and still the plugins are exported as JAR's
  • batch script to list local hard drives and then do a dir command on each drive
  • Absolute div overlay iframe borders?
  • Batch check and replace string then wait for a process
  • Visual Studio - Default code generation is disabled
  • CDbCriteria condition from another CDbCriteria
  • Move Files to Folders with same name
  • How to save the output of an interactive command-line program in a Unix-like shell?
  • Move Files to Folders with same name
  • merge two matrix and its attributes in matlab
  • python setattr for dynamic method creator with decorator
  • Javascript - Regular expressions, what to do now that its all true
  • Use Django's models in a Scrapy project (in the pipeline)
  • Delete shares excluding C$ D$ ADMIN$… etc
  • Add entity to json in mvc controller
  • C fprintf/fscanf optimising speed for big files
  • Adding text to start of each new line in a .txt file
  • Creating a batch file to delete multiple, separated subdirectories
  • how to make folder name with yesterday's date?
  • Repair wrong translation
  • Read a variable from a text file
  • Getting Powershell variable value in batch script
  • Obtain access token for both Microsoft Graph and individual service API endpoints (Outlook REST APIs
  • Identity Server by leastprivilege doesn't work properly on Azure
  • Batch: Set one directory up path in variable
  • how to change api_token column in token guard
  • Copy to all folders batch file?
  • C++ and preprocessor macros: variadic type