Search code examples
batch-filecmdexporttokenexport-to-csv

Remotely get serial number and model from list file and export it to arranged excel


I saw a nice script Here - this code get the Serial Number and Model from list of Remote Computers and export it to csv file. The only problem is, that the export results have many separated lines instead of ONE line each computer.

I mean, i want to get this:

enter image description here

But i get that:

enter image description here

I also checked Here, but cant get it to work as described.

Here the Batch Code:

@echo off
del list.csv 2>nul
for /f %%i in (pclist.txt) do (
  for /f "tokens=2 delims==" %%M in ('wmic /node:"%%i" csproduct get name /value') do for %%m in (%%M) do (
    for /f "tokens=2 delims==" %%S in ('wmic /node:"%%i" bios get serialnumber /value') do for %%s in (%%S) do (
      >>list.csv echo %%i, %%s, %%m
    )
  )
)
type list.csv
pause

How do i make the results to export to one line (display SN and model) to each computer?


Solution

  • You can have a go at this.

    @echo off
    break>list.csv
    for /f "delims=" %%i in (pclist.txt) do call :method %%~i
    type list.csv
    goto :eof
    
    :method
    set "host_name=%~1"
    for /f "tokens=2* delims==" %%N in ('wmic /node:"%~1" csproduct get name /value') do set "name=%%N"
    for /f "tokens=2* delims==" %%S in ('wmic /node:"%~1" bios get serialnumber /value') do set "serial=%%S"
    (echo %host_name%,%serial%,%name%)>>list.csv
    

    We just get each of the values once, seeing as we do not incorporate for loops in for loops. Though we could have done this without setting variables, in this instance it makes it a little more readable.