0

I am working on a script in Windows to get system information , I want to store the result of each command in a bat variable, then pass them to php variables.

Here the script that I tried:

for /f  "tokens=1 delims=[" %a in ('ver') do SET OS=%a
for /f  "skip=1 tokens=1 delims=|" %a in ('wmic os get Name') do SET version=%a
for /f  "tokens=2 delims=[" %a in ('ver') do SET kernel= %a
for /f  "skip=1 tokens=1 delims= " %a in ('wmic os get osarchitecture') do SET arq=%a
for /f  "skip=1 tokens=1 delims= " %a in ('wmic computersystem get numberofprocessors') do SET numero_cpu=%a
for /f  "skip=1 tokens=1 delims= " %a in ('wmic cpu get manufacturer') do SET fabricante_cpu=%a
for /f  "skip=1 tokens=1,2 delims=" %a in ('wmic cpu get Name') do SET nombre_cpu=%a
for /f  "skip=1 tokens=1 delims=" %a in ('wmic cpu get CurrentClockSpeed') do SET frequence_cpu=%a
for /f  "skip=1 tokens=1 delims=" %a in ('wmic bios get name') do SET name_bios=%a
for /f  "skip=1 tokens=1 delims=" %a in ('wmic bios get version') do SET version_bios=%a
for /f  "skip=1 tokens=1 delims=" %a in ('wmic bios get serialnumber') do SET serial_bios=%a
for /f  "skip=1 tokens=1 delims=" %a in ('wmic bios get manufacturer') do SET fabricant_bios=%a
for /f  "skip=1 tokens=1 delims=" %a in ('wmic NIC get Description') do SET red=%a 
for /f  "skip=1 tokens=1 delims=" %a in ('wmic SOUNDDEV get productname') do SET audio=%a
for /f  "skip=1 tokens=1 delims=" %a in ('wmic PATH Win32_videocontroller GET description') do SET video= %a

ECHO %OS%:%version%:%kernel%:%arq%:%numero_cpu%:%fabricante_cpu%:%nombre_cpu%:%frequence_cpu%:%name_bios%:%version_bios%:%serial_bios%:%fabricant_bios%:%red%audio%:%video%:

Thank you!

1 Answer 1

0

wmic behaviour: each output line ends with 0x0D0D0A instead of common 0x0D0A. This causes problem parsing wmic output with for /F loop: even seemingly empty line contains that leading 0x0D (carriage return) and therefore the first token is identified there.

Proof (note escaped ^, comma):

==>for /F "delims=" %g in ('wmic os get caption^, name /value') do @echo 1st="%g"
"st="
"st="
"st="Caption=Microsoft Windows 8.1
"st="Name=Microsoft Windows 8.1|C:\Windows|\Device\Harddisk1\Partition2
"st="
"st="
"st="

==>

Solvable using an additional (nested) loop as used in the :getwmic subroutine (a subroutine approach chosen to limit multiple coding). The for loop parameters are

  • %%g to retrieve the name=value lines only (see /value switch and findstr);
  • %%G to retrieve the desired value (2nd token %%H) and to remove the ending carriage return in the value returned.

Supply two line parameters to the :getwmic procedure (referenced %1 and %2 there):

  • a simple wmic query (enclosed in a pair of double quotes) querying the only property of a wmi class, e.g. "wmic os get Name";
  • an environment variable name the queried property should be assigned to, e.g. version.

Next script gives some examples only; all set commands are merely echoed for debugging purposes; remove all echo no sooner than debugged. Note that

  • OS variable already exists; hence consider another name (OSx);
  • use set "variable=value" syntax with double quotes to escape some characters of a special meaning in batch scripting, e.g. |, &, <, > etc., and ensure there are no (accidentally forgotten) trailing white spaces;
  • | pipe is escaped with ^ in for /f ... /value ^|findstr "="').

The script:

@ECHO OFF >NUL

for /f "tokens=1 delims=[" %%a in ('ver') do echo SET "OSx=%%a"
for /f "tokens=2 delims=[]" %%a in ('ver') do echo SET "kernel=%%a%"

call :getwmic  "wmic os get Name" version
for /f "tokens=1 delims=|" %%a in ("%version%") do echo SET "version=%%a%

call :getwmic "wmic os get osarchitecture" arq
call :getwmic "wmic computersystem get numberofprocessors" numero_cpu
call :getwmic "wmic cpu get manufacturer" fabricante_cpu
call :getwmic "wmic cpu get Name" nombre_cpu
call :getwmic "wmic PATH Win32_videocontroller GET description" video

goto :yoursequel

:getwmic
for /F "delims=" %%g in ('%~1 /value ^|findstr "="') do (
  for /F "tokens=1* delims==" %%G in ("%%g") do echo set "%~2=%%H"
)
exit /B

:yoursequel
ECHO %OS%:%version%:%kernel%:%arq%:%numero_cpu%:%fabricante_cpu%:%nombre_cpu%:%frequence_cpu%:%name_bios%:%version_bios%:%serial_bios%:%fabricant_bios%:%red%audio%:%video%:

Resources (required reading):

1
  • I'll take a look of that, I'll read it with calm ! Thanks for your answer!
    – Asis
    Jun 10, 2015 at 11:00

Not the answer you're looking for? Browse other questions tagged .