1

I'm running a WMIC lookup against a series of remote client machines, pulling in Model and serial number.

For /F "tokens=*" %%b in ('wmic /node:%device% computersystem get Model /value^|find "Model"') do Set model=%%b

FOR /F "tokens=*" %%c in ('wmic /node:%device% bios GET serialnumber /value^|find "SerialNumber=" ') do set Serialnumber=%%c

The problem I have is that (for example) %serialnumber% is set to:SerialNumber=CNU8424GP3

I don't want the 'Serialnumber=', I just want the Serial number itself.

The only way I've found of stripping this is:

set SerialNumber=!serialnumber:SerialNumber=!

but this leaves an equals sign at the beginning of the line. So the final output is =CNUBFZXXY, what I would like to do is to remove the leading =, and I haven't been able to.

Any Suggestions?

5 Answers 5

3

Add delims== to your for statements and change tokens=* to tokens=1-2. The values you want will then be in the second for loop variables:

For /F "delims== tokens=1-2" %%b in ('wmic /node:%device% computersystem get Model /value^|find "Model"') do set model=%%c
For /F "delims== tokens=1-2" %%c in ('wmic /node:%device% bios GET serialnumber /value^|find "SerialNumber=" ') do set Serialnumber=%%d
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks, I can see what you're trying to get at here, however the exact example you've given above gets the info from >before< the = sign. Ie the normal output is SerialNumber=CNXXXBFW, and your code sets the variable as SerialNumber=SerialNumber, selecting only the first of these tokens. I've tried altering the Delims and Tokens but can't get it to just read the second token only. Any further ideas? And thanks for taking the time to look.
What value %device% have on your system? I don't have this var on my system, so I used localhost in place of %device% and this code works. I get "Latitude E6400" for %model% and a proper value for %SerialNumber%. Are you capturing all the text above, even the text that scrolls right? The set statements should be using %%c and %%d respectively.
%device% is read in from elsewhere and is a hostname for a networked machine as I'm running this against a list of remote devices.
Interestingly I've just run this against my own machine, and as you say, it works fine. Strange. I'm wondering if I can do this with a PSExec instead. Thanks for your help :)
Could the output from wmic be different for a remote machine than it is for a local machine? You may have to play with the tokens to get at what you need. Conceptually, my solution should work, it's just a matter of parsing the output so you can grab what you need.
|
2

Either split up the result using a for-statement with "delims==" (already answered), or trim up to and including the equal-sign – your example code was very close already:

set SerialNumber=!SerialNumber:SerialNumber=!
set SerialNumber=!SerialNumber:~1!

Comments

1

The snippet below will allow you to remove the first and last character. I've played a trick here and added a matching '=' at the end of the string (NOTE: I could have used any character). I couldn't find a way to get up to the last character, so the trick is to just add on any character at the end of the variable and then strip it with the 1,-1 which means skip the first charcter and the negative value for the right side means 'counting back from the right'.

set serialnumber=%serialnumber%=
set serialnumber=%serialnumber:1-1%

Hope this helps people looking to hack DOS batch scripts in the future. A good short description of string manipulation can be found here: DOS String Manipulation

Comments

0

It is possible to retrieve specific characters from a string variable.

Syntax %variable:~num_chars_to_skip% %variable:~num_chars_to_skip,num_chars_to_keep%

This can include negative numbers:

  %variable:~num_chars_to_skip, -num_chars_to_skip%
  %variable:~-num_chars_to_skip,num_chars_to_keep%

Example

 SET _test=012345
 SET _result=%_test:~1,6%
 ECHO %_result%          =12345

Comments

0

Just do this instead it's much easier. USE QUOTATION MARKS AROUND BOTH VARIABLE AND STRING on the IF statement

IF "%VARIABLE%"=="equalsign=123456" goto Z

:Z

SET VARIABLE=%VARIABLE:~10%

echo %VARIABLE%

The output should be 123456. No equal sign.

That should do it. Seriously, that was really making me go nuts trying to figure that out. Someone else recently helped me with that. Never thought it would be so easy lol.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.