2015-04-01 30 views
0

尋找寫一個腳本,將查詢wmic computersystem獲取模型,如果值爲true繼續批處理文件,如果不是真的回聲不兼容的系統退出,這是我到目前爲止Windows批處理:查詢wmic computersystem獲取模型,如果等於true繼續使用windows批處理

@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 

即時通訊與腳本,但從來沒有用過,如果有這麼有點丟失在這一個。

任何幫助將非常歡迎。

回答

3
@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%] 

for環路

  • %%a檢索模型
  • %%b以去除值結束回車返回(wmic行爲)
  • %%m遍歷列表允許的型號

如果任何允許的模型與使用wmic檢索到的模型匹配,則代碼跳轉到起始標籤,否則,內部for循環結束,並且如果未找到任何匹配,則腳本結束。

這可以簡化爲

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

凡條件執行操作用於確定是否findstr命令無法發現的任何模型,並且取消執行。

當然,你可以使用的if /else級聯,但語法是有點不同

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 

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 
    ) 
) 

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 
    ) 
) 

或任何其他組合/風格更適合你的代碼,但你必須考慮到這個地方括號中的t很重要。左括號if需要與if命令位於同一行。 if右括號必須與else子句(如果存在)位於同一行。左括號else需要與else子句位於同一行。

+0

喜MC ND,謝謝您的回答我嘗試了第一個選項,改爲「MODEL1」我的模式「DH55HC__」但我得到的是一個閃爍的光標 – 2015-04-01 11:03:53

+0

@ErikRichardson,我已經直接複製並粘貼代碼在答案中,用我的電腦模型替換其中一個模型,它就可以工作。你做了什麼額外的改變? – 2015-04-01 11:10:23

+0

奇怪我在其他一些機器上測試過它,它的工作原理非常感謝 – 2015-04-01 11:37:31

0

所有@MC ND解決方案都是有效的。 但你可以試試這個。

@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 
+1

我的電腦型號是「HP Compaq 8100 Elite CMT PC」。也許你需要一些引號。 – 2015-04-01 11:28:06

+0

Thanks #Corrected – SachaDee 2015-04-01 11:32:09