Asked
Updated
Viewed
22.5k times

I am trying to create a small script that will allow me to pull relevant data from a PC using a batch file and then have that batch file export the data to a .csv file.

Here is the info I would be gathering with commands in this batch file:

@echo off
hostname
wmic csproduct get name, vendor, version
wmic bios get serialnumber
getmac
Systeminfo | findstr /C:BIOS
systeminfo | findstr /B /C:"OS Name" /C:"OS Version"
wmic os get osarchitecture
pause

Here is a sample of what I would expect the CSV file to look like:

hostname,name,vendor,version,serialnumber,macaddress,bios,osname,osversion,osarchitecture

Is there a way to export this data from the batch file to CSV? If not with a batch file, is there an alternate way I can do this with VBScript or CScript?

  • 0
    Can you provide a sample of what you expect the CSV file to look like? — spork
  • 0
    I'm just looking for an easy, no-frills export into a table that will show Hostname, Product Name, Product Vendor, Product Version, Device Serial Number, MAC Address, BIOS Version/Date, OS Name, OS Version, and OS Architecture. I have updated the above with the example CSV output I would expect. — Megallica
  • 0
    So you're expecting only one "entry" in your exported table, right? One row with all the columns accounted for? Your best bet here is probably to use a combination of findstr and for /f loops to parse the appropriate tokens from the output of each command, stash them in local variables and then echo everything to a text file. The downside is that the for-loop syntax in Windows batch scripts is pretty whacky, but it gets the job done. Having said that, if you're able to do this in something more robust such as Python or Perl, I'd recommend that instead, since regular expressions will make your life easier. — spork
  • 0
    Yep, that is all I need. So I tried to do a simple test with hostname, FOR /F "tokens=1 delims= " %%G IN ('%_hostname_cmd% ^find "FCRS"') DO echo Result is [%%G] in (C:\Users\Cesadmin\Desktop) pause. The script runs and does not error, however, it does not create a text file. The FCRS that it's looking for is the prefix to every PC on my site. Where did I go wrong? This is my first time attempting this, so please bear with my ignorance 🙂 — Megallica
  • 0
    To direct output to a file, use the > operator to create/overwrite a file, and >> to append. A simple example using just hostname (you can modify the command to your liking): for /F "tokens=1 delims= " %%G in ('hostname') do ( echo Result is [%%G] > sample.txt ) This will direct the output of the "echo" command to sample.txt. — spork
  • 0
    Ok so I tested the command and it worked, but only once... I cannot get it to create another text file even if I change the name of the output doc. Any thoughts? If I use: for /F "tokens=* delims= " %%G in ('wmic csproduct get name, vendor, version') do (echo [%%G] > SystemInfo.txt Can I pull the data from the same command or do I have to separate out the name/vendor/version? Lastly, if I made all of the commands in succession EX: for /F "tokens=1 delims= " %%G in ('hostname') do (echo [%%G] > SystemInfo.txt and then for /F "tokens=* delims= " %%G in ('wmic csproduct get name, vendor, version') do (echo [%%G] > SystemInfo.txt Will this output to the same file or overwrite as each command runs? Thanks again. Your assistance is really appreciated! — Megallica
  • 0
    You're missing the closing parentheses for each loop; it helps to use multiple lines to improve readability. Also you're overwriting the file each time; use ">>" to append to an existing file after the first command. for /F "tokens=1 delims= " %%G in ('hostname') do ( echo [%%G] > SystemInfo.txt ) and for /F "tokens=* delims= " %%G in ('wmic csproduct get name, vendor, version') do ( echo [%%G] >> SystemInfo.txt ) Also, if you're pulling multiple tokens from a line, they'll be dumped into successive variables, starting with the one in the FOR loop, i.e. the first token will be in %%G, the second one in %%H, and so forth. — spork
  • 0
    Thanks Spork! Got it all working correctly. I do have one last question. When you run 'getmac' it pulls the MAC address and adapter state for all adapters on the system. What delims would I need to only export the active adapters? This is what is currently exported: Physical Address Transport Name =================== ========================================================== 3C-97-0E-A9-7B-79 Media disconnected 6C-88-14-BB-84-1C \Device\Tcpip_{A1C49BAE-3AA2-4016-83FC-451E9B5F5AB3} F8-2F-A8-E3-49-6B Media disconnected 00-60-73-53-B5-74 \Device\Tcpip_{44CF952C-B243-424E-92B3-3461D0A6A4E9} I only want the Header and 2nd and 4th MAC addresses (Wired and Wireless NIC) Would it also be possible to export only the IP address from IP Config the same way? Thanks — Megallica
  • 0
    You need to split up the output as reasonably as possible, then use some conditional logic to eliminate the unwanted entries. On my machine, inactive adapters show up with "N/A" as the transport, so I included that check as well, but you can eliminate it if you don't need it. I found this to be a little easier with EnableDelayedExpansion turned on (warning: batch script voodoo): @echo off setlocal enableextensions enabledelayedexpansion echo physicaladdress,transportname for /F "skip=3 tokens=1,2" %%G in ('getmac') do ( set __mac=%%G set __transport=%%H if /i not "!__transport!"=="N/A" ( if /i not "!__transport!"=="Media disconnected" ( echo !__mac!,!__transport! ) ) ) endlocal I didn't bother parsing the headers since you can just echo those manually. The script above produces this for me: physicaladdress,transportname 74-E3-D1-CA-4D-F5,\Device\Tcpip_{F1D51C7D-6552-4640-8D2C-55A3F8DD196B} 10-72-5D-4D-5D-82,\Device\Tcpip_{E4DF4431-3F5B-4E57-8E7A-83B1EB34BF71} Extracting IP's from ipconfig would follow roughly the same logic -- you'll just have more data to deal with and conditions to check for. — spork
add a comment
0

1 Answer

  • Votes
  • Oldest
  • Latest
Answered
Updated

Here is the batch script that is allowing me to export data from the batch file to CSV:

@echo off
Set Configout=\YourNetworkShare\File.txt
SET ConfigOutCred=/USER:USERNAME@DOMAIN /Pass:PASSWORD
color 0c
echo.
echo Exporting System Info...
Date /T >>%Configout% %ConfigOutCred%
    for /F "tokens=1 delims= " %%G in ('hostname') do (
      echo "%%G" >> %Configout%
    )
(Systeminfo | findstr /C:BIOS)>>%Configout%
(systeminfo | findstr /B /C:"OS Name" /C:"OS Version")>>%Configout%
setlocal enableextensions enabledelayedexpansion
for /F "skip=3 tokens=1,2" %%L in ('getmac') do (
 set __mac=%%L
 set __transport=%%M
 if /i not "!__transport!"=="N/A" (
  if /i not "!__transport!"=="Media disconnected" (
   if /i not "!__transport!"=="Media" (
   echo "%%L" >> %Configout%
   )
  )
 )
)
endlocal
    for /F "tokens=2 delims=:" %%F in ('ipconfig^|find "IPv4 Address"') do (
     set __IP=%%F
      echo "%%F" >> %Configout%
    )
    for /F "tokens=1,2,3 delims= " %%H in ('wmic csproduct get name') do (
      echo "%%H" >> %Configout%
    )
    for /F "tokens=1,2,3 delims= " %%I in ('wmic csproduct get vendor') do (
      echo "%%I" >> %Configout%
    )
    for /F "tokens=1,2,3 delims= " %%J in ('wmic csproduct get version') do (
      echo "%%J" >> %Configout%
    )
    for /F "tokens=1,2,3 delims= " %%K in ('wmic bios get serialnumber') do (
      echo "%%K" >> %Configout%
    )
wmic os get osarchitecture>>%Configout%
echo.
echo System Info has been exported!
: notepad.exe \YourNetworkShare\File.txt
pause
exit

Using the above batch file will output the following info:

Current Date
PC Hostname
BIOS Version and Date
OS Name
OS Version and SP
MAC Addresses of active adapters
IP Addresses of active adapters
PC Model
PC Brand
PC Version
PC Serial Number
OS Architecture (32-Bit or 64-Bit)

After it grabs this data, it will create a text file in whichever network folder specified with fully qualified domain credentials. When it is run on subsequent machines, it will append the text file and add it to the bottom (With the OS Arch. and the Date making an easy separator to eyeball).

Note: I left the 'pause' and 'notepad' strings in for testing if anyone likes.

You rock Spork! Everything is doing what I need.

  • 0
    Glad you got it all working. Windows batch scripting is pretty much the wonkiest, most non-intuitive thing when it comes to scripting. Cheers! 🍻 — spork
add a comment
0