Say you’ve got a script and you want to run something in that script that only runs if you’re on a specific type of machine, what can we do to find out what machine we are on?
Some might say “use vbscript and do a wmi call”. Well yes you could do that, but that’s needlessly hard! Use this simple command instead…
wmic csproduct get name
On my machine that returns two lines, one saying Name
and another with my machine’s model name: HP Compaq dc7600 Small Form Factor
How cool is that!! Basically here we’re using a WMI command line tool. There’s lots to it, just type: wmic /?
So to use this in a script we’ll need to check for the existence of a particular machine name being returned, we can use the find command a different way and return the number of lines with a specific word in it. If we get 1 or more lines with that word then we’re on the machine we’re looking for.
Here’s an answer:
wmic csproduct get name | find /c /i "7600"
On my machine that returns a 1
, so we just need to parse that into a variable and use a simple if statement and we’re there:
@echo off
for /f "delims==" %%a in ('wmic csproduct get name ^| find /c /i "7600"') do set /a machine=%%a
if %machine% geq 1 (
echo Running on a 7600 machine
) else (
echo Not running on a 7600 machine
)
[script now works, thanks to the comment pointing to the pipe-char issue]
You could modify that to do a goto
to jump to another part of your script I guess. Just change the "7600"
part for something that uniquely identifies the machine you’re looking for in your environment.
This works on XP and Vista. I think it should work in WinPE too as long as you have the WMI add-in installed.
— Updated 2011-10-06 —
And now here’s a better version of my script which will help people who want to use it to batch things a bit more easily!
@ECHO OFF
REM do a wmi query to get the info we want and put it in a variable
FOR /F "tokens=2 delims==" %%A IN ('WMIC csproduct GET Name /VALUE ^| FIND /I "Name="') DO SET machine=%%A
ECHO Computer model: "%machine%"
REM Now we have the model in a variable we can do some logic and run commands, for example…
REM Watch for stray spaces at the end, take out all spaces with: SET machine=%machine: =%
IF /I “%machine%” == “Latitude E6410” (
REM do something specific for an E6410
) ELSE (
REM do something for other types
)
— Updated 2013-09-21: newer version of this article using PowerShell here —