1

I am creating a script that will get the computer version and serial number, turn it into variables, and then combine them together to create the new hostname.

However, the WMIC command for the serial number returns "T300"-"FDHGFJ " Running just the serial number WMIC alone (without the does not "wmic csproduct get version") does not include the extra spaces.

I've tried looping it around one more time as other posts suggest but no luck.

Below is the full code.

@ECHO ON
PUSHD "%~dp0"

setlocal EnableDelayedExpansion
for /f "usebackq skip=1 tokens=*" %%i in (`wmic bios get serialnumber ^| findstr /r /v "^$"`) do set "serialn=%%i"

for /f "usebackq skip=1 tokens=2 delims= " %%a in (`wmic csproduct get version ^| findstr /r /v "^$"`) do set "modeln=%%a"

ECHO "%modeln%"-"%serialn%" >>test.txt
POPD
exit

I want the final result to be "T300"-"FDHGFJ" as it might get implemented into a task sequence.

  • 1
    Change the output format of [tag:WMIC] so that you're not getting it in its default space padded tabular format, _`CSV` perhaps_, or simply using /Value may give you an easily delimited return. – Compo May 06 '19 at 20:23
  • @Compo CSV still outputs a few spaces. However, I have to keep it as .txt for the time being. – Fabricio Martinez May 06 '19 at 20:37
  • 1
    Possible duplicate of [Get rid of spaces and tabs in wmic output](https://stackoverflow.com/questions/28672210/get-rid-of-spaces-and-tabs-in-wmic-output) – phuclv May 07 '19 at 02:09
  • 1
    there are a lot of duplicates: [Text garble in batch script for wmic command](https://stackoverflow.com/q/25599445/995714), [Mixed ascii and unicode output from script - how to get command to output all as ascii?](https://stackoverflow.com/q/44065913/995714), [For /F with wmic unwanted output](https://stackoverflow.com/q/34684734/995714), [Parsing the output of wmic in shell script](https://stackoverflow.com/q/19349241/995714)... – phuclv May 07 '19 at 02:09
  • You should still loop around a second time (as you said you have already tried) in order to get rid of orphaned carriage-returns and to no longer need `findstr`. YOu could write the output ([`>`](https://ss64.com/nt/syntax-redirection.html)) to a file just to check (with a hex. editor) whether there are really spaces or if there are other characters... – aschipfl May 07 '19 at 07:27

3 Answers3

0

You can try a bit of a hack by setting the variable only if not defined. I removed "tokens=*" and "delims=" as that will get the entire line. We then just do substitution on whitespace.

@echo off
set serialn=
set modeln=
for /f "usebackq skip=1" %%i in (`wmic bios get serialnumber`) do if not defined serialn set "serialn=%%i"
for /f "usebackq skip=1" %%a in (`wmic csproduct get version`) do if not defined modeln set "modeln=%%a"

echo "%modeln: =%"-"%serialn: =%">>test.txt

Note the output of second string on my device wmic csproduct get version has only one value and therefore I had to change the string, if yours really has 2 tokens then you should use your original string:

for /f "usebackq skip=1 tokens=2" %%a in (`wmic csproduct get version`) do if not defined modeln set "modeln=%%a"
Gerhard
  • 22,678
  • 7
  • 27
  • 43
0

The wmic command with its get verb might pad the returned data by trailing SPACEs, which I assume you want to have removed without removing SPACEs in the returned values themselves.

To achieve this you need to change the output format by adding the VALUE option, like this:

@echo off
setlocal EnableDelayedExpansion
cd /D "%~dp0."
for /F "tokens=1* delims==" %%I in ('wmic BIOS get SerialNumber /VALUE') do for /F "delims=" %%K in ("%%J") do set "serialn=%%K"
for /F "tokens=1* delims==" %%I in ('wmic CSProduct get Version /VALUE') do for /F "delims=" %%K in ("%%J") do set "modeln=%%K"
>> "test.txt" echo "%modeln%"-"%serialn%"
endlocal
exit /B

In addition I changed the following:

  • I placed another for /F loop inside of the one that parses the wmic output in order to avoid Unicode-to-ASCII/ANSI conversion artefacts by for /F, like orphaned carriage-return characters;
  • I replaced the pushd/popd pair by cd /D, because setlocal/endlocal already localises the environment, including the current working directory;
  • I added an explicit endlocal just to explicitly end the environment localisation (although this would be done upon termination of the batch script anyway);
  • I replaced exit by exit /B in order to only quit the batch file but not the parent cmd instance;
  • I put the redirection portion >> "test.txt" in front of the echo command in order to avoid the trailing space between the last closing " and >> (in your code) to be returned too;
aschipfl
  • 33,626
  • 12
  • 54
  • 99
0

As my initial comment regarding using /Value has already been implemented into an answer, this one expands upon using the CSV format:

@Echo Off
SetLocal DisableDelayedExpansion
Set "DWM=%__AppDir__%wbem"

Rem Fixes 'Invalid XSL format (or) file name' errors in some Windows versions.
Set "CSV="
For /F "Delims=" %%A In ('"Dir /B/S/A-D "%DWM%\csv.xsl" 2>Nul"'
)Do If Not Defined CSV Set "CSV=%%A"
If Not Defined CSV Exit /B

Set "Serial#=Null"
For /F "Skip=2Tokens=1*Delims=," %%A In (
    '""%DWM%\wmic.exe" BIOS Get SerialNumber /Format:"%CSV%" 2>Nul"'
)Do For /F Tokens^=* %%C In ("%%~B")Do Set "Serial#=%%~C"

Set "Model#=Null"
For /F "Skip=2Tokens=1*Delims=," %%A In (
    '""%DWM%\wmic.exe" CSProduct Get Version /Format:"%CSV%" 2>Nul"'
)Do For /F Tokens^=* %%C In ("%%~B")Do Set "Model#=%%~C"

Echo "%Model#%"-"%Serial#%">>"test.txt"
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39