?
Solved

Multiple wmic commands in a single batch file

Posted on 2016-09-06
7
Medium Priority
?
1,844 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
LVL 57

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 57

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
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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

718 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