基于WMIC获取机型的Windows批处理脚本分支执行问题
Alright, let's break down your two Windows batch scripting needs related to WMIC and device models—here's practical, tested code to get you sorted:
1. Batch Script to Fetch Model Numbers from Multiple Devices
If you need to pull device models from multiple remote machines, WMIC's /node parameter lets you target specific hosts directly. Here's a robust script to handle this:
@echo off setlocal enabledelayedexpansion :: Replace with your target devices (hostnames, IPs, or reference a text file) set "target_devices=PC-WORKSTATION-01 192.168.1.50 LAPTOP-SALES-03" for %%d in (%target_devices%) do ( echo === Checking Device: %%d === :: Nested for loops fix WMIC's extra blank line output quirk for /f "tokens=2 delims==" %%a in ('wmic /node:%%d computersystem get model /value 2^>nul') do ( for /f "delims=" %%b in ("%%~a") do ( echo Model: %%b ) ) :: Handle cases where WMIC can't reach the device if errorlevel 1 echo [!] Failed to retrieve model for %%d (offline/access denied) echo. ) endlocal pause
Quick Notes:
- Use a text file for many devices: If you have a long list of machines, replace the
set "target_devices=..."line with a loop that reads from a file:for /f "delims=" %%d in (device_list.txt) do ( :: Same inner WMIC loop as above ) - Suppress errors: The
2^>nulhides annoying error messages when a device is unreachable.
2. Run Model-Specific Scripts Automatically
Your existing script starts correctly by grabbing the model—let's expand it to match models and launch the corresponding setup/config scripts. Here's a refined version:
@echo off setlocal enableextensions disabledelayedexpansion :: First, get a clean model string (fixes WMIC's weird formatting) set "device_model=" for /f "tokens=2 delims==" %%a in ('wmic computersystem get model /value') do ( for /f "delims=" %%b in ("%%~a") do ( set "device_model=%%b" ) ) :: Check if we successfully retrieved the model if not defined device_model ( echo [!] Failed to detect device model. pause exit /b 1 ) echo Detected Device Model: %device_model% :: Match model to its corresponding script (add/remove cases as needed) if /i "%device_model%"=="HP t520 Flexible Series TC" ( call "HP-T520-Setup.bat" ) else if /i "%device_model%"=="HP t610 WW Thin Client" ( call "HP-T610-Config.bat" ) else if /i "%device_model%"=="Dell OptiPlex 7090" ( call "Dell-OptiPlex-7090-Tweaks.bat" ) else ( echo [!] No matching script found for model: %device_model% pause exit /b 0 ) :: Optional confirmation after script runs echo ✓ Completed setup for %device_model% endlocal pause
Key Improvements:
- Case-insensitive matching: The
/iflag inifensures matches work regardless of uppercase/lowercase (e.g., "hp t520" = "HP T520"). - Clean variable storage: We store the model in
device_modelfirst for easier readability and reuse. - Proper script execution: Using
callensures the original batch resumes after the target script finishes (instead of exiting).
For Long Model Lists:
If you have dozens of models to match, a loop-based approach is cleaner than a long chain of if statements:
:: Define model-script pairs in one place for %%pair in ( "HP t520 Flexible Series TC=HP-T520-Setup.bat" "HP t610 WW Thin Client=HP-T610-Config.bat" "Dell OptiPlex 7090=Dell-OptiPlex-7090-Tweaks.bat" ) do ( for /f "tokens=1,* delims==" %%m in ("%%~pair") do ( if /i "%device_model%"=="%%m" ( call "%%n" goto :script_executed ) ) ) :script_executed
内容的提问来源于stack exchange,提问作者donald mackenzie
相关产品推荐
相关产品推荐

