Disk report with WMIC

Batch, ASP, JScript, Kixtart, etc.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 15 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
irwazr
Posts: 1
Last visit: Thu Nov 29, 2007 7:19 am

Disk report with WMIC

Post by irwazr »

Hi everyone,
I'm currently trying to create a disk usage report that receives a list of servers and outputs to a csv file. The report will be run as a daily scheduled task. Currently I'm using WMIC to query all the boxes, which are all W2K/W2K3. I'm not sure if there are other methods that get around the problem I've encountered so please feel free to suggest anything that helps :)

The problem I have is that WMIC returns disk usage values in bytes, which I'm sure you'll agree is totally useless to any human being with a calculator for a brain. I thought to output first to a temporary csv file then re-read the file with a for /f loop and parse the values with some equations so that excel would display the figures in GB. I came up with the following script, it generates correct values in most places but for some reason drops in carriage returns that break up the equations and send Excel up the wall :(

Hope someone out there can help. There is a surprisingly limited amount of info on the net regarding equations in batch script :(

Regards,
Warren

Code: Select all

SET SERVER_LIST=%1
SET OUTPUT_FILE=%2
SET GB_D=0
SET GB_E=0
FOR /F %%A IN (%SERVER_LIST%) DO WMIC /Node:%%A LogicalDisk Where DriveType="3" Get DeviceID,FileSystem,FreeSpace,Size /Format:csv | MORE /E +2 >> tmp-%OUTPUT_FILE%
echo Hostname,Drive,File System,Volume Label,Free Space (GB),Size (GB),Percent Free > %OUTPUT_FILE%
FOR /F "tokens=1,2,3,4,5,6 delims=," %%a IN (tmp-%OUTPUT_FILE%) DO echo %%a,%%b,%%c,%%f,=%%d/1024/1024/1024,=%%e/1024/1024/1024,=%%d/%%e*100 >> %OUTPUT_FILE%
del tmp-%OUTPUT_FILE%
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Disk report with WMIC

Post by jvierra »

The biggest rawback in WMIC is that it outputs in Unicode (It's internationalized) which will cause problems unless you use a utility to convert it if you need it in ANSI (single byte).
I believe you need to be sure "/Interactive:OFF" is set or it will wrap lines on output.

Jeff is right in suggesting PowerShell as, once learned, it is easier and more powerfull except when you need to run it a a machine where it can't be installed. Next I suggest vbscript with WMI which will be installed. WMIC isw very useful once you learn it's quirks and get over the lack of comprehensive documentation.

I have a script that converts US-EN Unicode to ANSI.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Disk report with WMIC

Post by jvierra »

Here is a better example of how to concatenate reports using WMIC. It also contains much information on how to use other poorly documented capabilities of WMIC. It comes with sample source code for BAT file the generates a composite listing.

http://www.microsoft.com/technet/techne ... 9/WMIData/

User avatar
mccoyster
Posts: 1
Last visit: Wed Mar 04, 2009 10:14 pm

Disk report with WMIC

Post by mccoyster »

I'm trying to run the command in Powershell as described, but I continue to get this response. Not sure where the problem is, besides at line 1 character 14. ; ) Any help would be greatly appreciated.

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)At line:1 char:14+ get-wmiobject <<<< win32_logicaldisk -filter "drivetype=3" -computer (get-content servers.txt) | Select SystemName,DeviceID,FileSystem,@Name="Freespace";Expression={$_.FreeSpace/1gb}},@{Name="Size";Expression={$_.Size/1gb}} | export-csv drivestats.csv -notype

User avatar
gimme
Posts: 1
Last visit: Thu Mar 26, 2009 12:27 pm

Disk report with WMIC

Post by gimme »

I have changed the original script so no temporary file is needed,
worked around 32 bit limitation of the SET command calculations.
Used find instead of more to eliminate empty space and headers.
The output is in MB.

Code: Select all

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
SET SERVER_LIST=%1
FOR /F "tokens=1-7 delims=," %%a in ('FOR /F %%A IN ^(%SERVER_LIST%^) DO @WMIC /Node:%%A LogicalDisk Where DriveType^=^"3^" Get DeviceID^,DriveType^,FileSystem^,FreeSpace^,Size^,VolumeSerialNumber /Format:csv ^| find ^"3^"') DO @(
SET tmp1=%%e
SET tmp2=%%f
SET /A Free_space=!tmp1:~0,-3!/1024*1000/1024
SET /A Total_space=!tmp2:~0,-3!/1024*1000/1024
echo %%a,%%b,%%c,!Free_space!,!Total_space!
)
ENDLOCAL
This topic is 15 years and 2 weeks old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked