0

I have written a piece of code in Batch script which is somewhat like :

( 
    wmic.exe /node:x.x.x.x computersystem get name
    wmic.exe /node:x.x.x.x computersystem get domain 
    wmic.exe /node:x.x.x.x computersystem get manufacturer 
    wmic.exe /node:x.x.x.x computersystem get model 
    wmic.exe /node:x.x.x.x computersystem get username 
    wmic.exe /node:x.x.x.x computersystem get systemtype
) >> file.txt

The contents of file.txt are :

ABC123  
xyz.com  
Hewlett-Packard  
HP xw4400 Workstation  
ABC123\Administrator  
x64-based PC 

I want above information to be stored in CSV format as follows :

ABC123 , xyz.com , Hewlett-Packard , HP xw4400 Workstation , ABC123\Administrator , x64-based PC 

How can this be achieved?

2 Answers 2

1
@echo off
for /f "skip=2 delims=" %%a in (
    'wmic /node:localhost computersystem get name^,domain^,manufacturer^,model^,username^,systemtype /format:csv'
) do (
    for /f "tokens=2-7 delims=," %%b in ("%%a") do (
        echo(%%e , %%b , %%c , %%d , %%g , %%f
    )
) >> file.txt

In case you doub why the two for commands, the output of the WMIC contains an aditional carriage return character that has to be removed.

Sign up to request clarification or add additional context in comments.

6 Comments

Perfect! What if I wish to add IP address and MAC Address too?
@MandarShinde That data is available with wmic nicconfig. But you will need to first determine from which nic you want the data, if ip4 or ip6 or both. The way to get the data is the same that the posted code. Retrieve one set of fields and store them in variables, retrieve the second set and store in variables. Then output the variables content into the output file.
What if I have to execute another wmic command (say, wmic cpu) and append it's output next to the output above mentioned command (wmic computersystem) so that they are seen in the same row?
@MandarShinde, the same as above. In the for commands, instead of echoing the data, save the values inside variables. When you have all the required information in variables, then echo %var1%,%var2%,... >> file.txt
Your script mentioned above works absolutely fine. But I, being a novice in Batch Scripting, am not able to understand certain things. First of all, meaning of "tokens=2-7". When I changed it to "tokens=1-6", the output changed. Second thing is, in the echo statement, you used %%e , %%b , %%c , %%d , %%g , %%f. Why not alphabetically %%b , %%c , %%d , %%e , %%f , %%g? (Might be a silly doubt, apologies for that.)
|
0

Something like this:

FOR /F %%i IN ('wmic computersystem get name') DO SET N=%%i
FOR /F %%i IN ('wmic computersystem get domain') DO SET D=%%i
echo %N%;%D% > output.csv

5 Comments

Tried this. It is just displaying series of commas in the output file.
If you typed it at the console, you need to change %% into %. The way it is written is correct if you save it in a batch file and run it - not for typing in by hand.
No, I typed that in a batch file and executed it.
Try copying it and pasting it, rather than typing it.
Same results. Only commas are being displayed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.