简体   繁体   中英

Batch - WMIC Command output multiple lines add string (Computer Name)

I am trying to make a batch script which will output as follows for fixed-disk:

Computer Name, Drive Letter, Free Disk Space

the output can be one line in case there is only one drive or multiple-lines if there are more than one drive

now with following command, i am getting the output for a computer, but this does not give the computer name. I need to include computer name because i will run it against multiple computers by a script using for loop.

wmic /node:"%COMPUTERNAME%" LogicalDisk Where DriveType="3" Get DeviceID,FreeSpace

using batch script, i can output computer name, but it only appears above the disk-space lines

echo off
for /f %%i in (computers.txt) do (
echo %%i >>outputfile.txt
wmic /node:"%%i" LogicalDisk Where DriveType="3" Get DeviceID,FreeSpace' >>outputfile.txt
)

if the computer name is "COMPUTER1" and has 3 drives, this is coming as

COMPUTER1
DeviceID  FreeSpace
C:        <free space bytes>
D:        <free space bytes>
E:        <free space bytes>

what i want is:

ComputerName DeviceID FreeSpace
COMPUTER1    C:       <free space bytes>
COMPUTER1    D:       <free space bytes>
COMPUTER1    E:       <free space bytes>

event the column header can be excluded.

i believe this can be done by for loop, but not sure how. anyone can advise, will be appreciated. thanks.

you have to capture wmic output by for , then you can postprocess every line:

echo Computername  DeviceID  FreeSpace
for /f "delims=" %%a in ('wmic /node:"%%i" LogicalDisk Where DriveType^="3" Get DeviceID^,FreeSpace ^|find ":"') do @echo %%i  %%a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM