Solved

Multiple wmic commands in a single batch file

Posted on 2016-09-06
7
216 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 28

Accepted Solution

by:
Michael Pfister earned 250 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 28

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 52

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 52

Assisted Solution

by:Bill Prew
Bill Prew earned 250 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
0
 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now