Solved

Multiple wmic commands in a single batch file

Posted on 2016-09-06
7
501 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 53

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 53

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

831 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