问题
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
%%ato retrieve the model%%bto remove the ending carriage return in the value returned (wmicbehaviour)%%mto 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