views:

40

answers:

2

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?

A: 

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
Patrick Cuff
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.
Patrick
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.
Patrick Cuff
%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.
Patrick
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 :)
Patrick
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.
Patrick Cuff
Yes - I've tried a multitude of combinations of the tokens, I don't under stand why it doesn't work though. The out put from running it against a remote device is the same as from the localhost...
Patrick
What OS(es) are you using?
Patrick Cuff
A: 

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
Edoctoor