Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Multiple wmic commands in a single batch file

Posted on 2016-09-06
7
Medium Priority
?
2,300 Views
Last Modified: 2016-09-08
Hi Guys,

Can anyone help me as to how I can store multiple wmic get commands into a single batch file.

The following commands are what I am looking to add in a single batch.

1) wmic os get csname
2) wmic cpu get name
3) wmic os get caption

Now I understand 1 & 3 can be seperated from commas but the Idea is to get wmic os & wmic cpu run in the same batch file and output it to a particular location say c:\hardware.

Thanks in advance.


EDIT: I did figure out how to do that in the meanwhile. Below is the code. However this is saving the output in the form of rows. How can I save the output in the form of columns. That is the computer_name should be in one column and its subsequent row should have the value. Similarly Name should be the next column and its subsequent row should be the model of the laptop etc.

Below is the code:
@echo off
if exist C:\dell\%computername%.csv del C:\dell\%computername%.csv
set outputfile="C:\dell\%computername%.csv"


hostname>>%outputfile%

wmic csproduct get name>>%outputfile%

wmic bios get serialnumber>>%outputfile%

wmic diskdrive get model>>%outputfile%

wmic path win32_physicalmedia get SerialNumber>>%outputfile%
goto :eof

Open in new window

0
Comment
Question by:Siddharth S
  • 3
  • 2
  • 2
7 Comments
 
LVL 29

Accepted Solution

by:
Michael Pfister earned 1000 total points
ID: 41785891
Batch is very limited here, you could go with Windows Powershell.

If thats no option, process the output with for:

for /f "delims== tokens=2" %%i in ('wmic csproduct get name /value') do SET CSPRODUCT=%%i
for /f "delims== tokens=2" %%i in ('wmic bios get serialnumber /value') do SET SERIAL=%%i
for /f "delims== tokens=2" %%i in ('wmic diskdrive get model /value') do SET DISK=%%i
for /f "delims== tokens=2" %%i in ('wmic path win32_physicalmedia get SerialNumber /value') do SET PMSERIAL=%%i
echo Hostname,Model,Serial,Disk,DiskSerial>>%outputfile%
echo %CSPRODUCT%,%SERIAL%,%DISK%,%PMSERIAL%>>%outputfile%

Open in new window

0
 
LVL 29

Expert Comment

by:Michael Pfister
ID: 41785892
Ok, sorry forgot your first few lines:
@echo off
if exist C:\dell\%computername%.csv del C:\dell\%computername%.csv
set outputfile="C:\dell\%computername%.csv"
for /f "delims== tokens=2" %%i in ('wmic csproduct get name /value') do SET CSPRODUCT=%%i
for /f "delims== tokens=2" %%i in ('wmic bios get serialnumber /value') do SET SERIAL=%%i
for /f "delims== tokens=2" %%i in ('wmic diskdrive get model /value') do SET DISK=%%i
for /f "delims== tokens=2" %%i in ('wmic path win32_physicalmedia get SerialNumber /value') do SET PMSERIAL=%%i
echo Hostname,Model,Serial,Disk,DiskSerial>>%outputfile%
echo %CSPRODUCT%,%SERIAL%,%DISK%,%PMSERIAL%>>%outputfile%

Open in new window


And I can't check here what happens in case the computer has for example more than one harddisk. I'm pretty sure it will fail or deliver incomplete results.
0
 

Author Comment

by:Siddharth S
ID: 41785974
Amazing. Thanks.. !! I will hold onto this for a day. I have a bit of modifications to do post which I will close the question. :)

In the meanwhile if I have any doubts in it, I will resume.

And yes, it is delivering no result when there are 2 Hard disks. But I dont think I need that bit.
0
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 
LVL 59

Expert Comment

by:Bill Prew
ID: 41785983
I'm trying something to try and get the disk (multiples) info as well, if you want to wait and see if I have success...

~bp
0
 

Author Comment

by:Siddharth S
ID: 41786005
Sure Bill. You helped me the last time as well :)

But I would ask you to wait on it as I will be modifying the code quite a bit to get the hardware info. We can try on it once I make changes to my code..
0
 
LVL 59

Assisted Solution

by:Bill Prew
Bill Prew earned 1000 total points
ID: 41786016
This should handle multiple drives, wasn't sure what you wanted to do with the other data that only occurs once, so I repeated it on each output line.  See what you think...

@echo off
setlocal

rem Define output file location
set OutputFile=%computername%.csv

rem Overwrite output file and add header line
echo "Model","Serial","DiskModel","DiskSerial">"%OutputFile%"

rem Gather all data desired
for /f "skip=2 tokens=2 delims=," %%a in ('wmic csproduct get name^,version /format:csv') do (
  for /f "skip=2 tokens=2 delims=," %%b in ('wmic bios get serialnumber^,version /format:csv') do (
    for /f "skip=2 tokens=2-3 delims=," %%c in ('wmic diskdrive get model^,serialnumber^,systemname /format:csv') do (
      rem Write merged results to output file
      echo "%%~a","%%~b","%%~c","%%~d">>"%OutputFile%"
    )
  )
)

Open in new window

~bp
1
 

Author Closing Comment

by:Siddharth S
ID: 41789316
Thanks both. Awesome. We do not have systems where 2 disks are present so I am good for now.

Awesome help as ever.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question