Friday, July 18, 2008

Querying Disk Space on Remote Servers using Batch with WMIC

Time to check the disk free space in GB and percentage on a server volume...

WMIC(Windows Management Instrumentation Command-line) makes another appearance!

Thanks to Tipsmark for this syntax (Response number 17 in this post). I added the /node switch and some error handling / usage to have this batch file work on remote machines.

@ECHO OFF
IF "%~1"=="" goto help
IF "%~2"=="" goto help

@SETLOCAL ENABLEEXTENSIONS
@SETLOCAL ENABLEDELAYEDEXPANSION

@FOR /F "tokens=1-3" %%n IN ('"WMIC /node:"%1" LOGICALDISK GET Name,Size,FreeSpace | find /i "%2""') DO @SET FreeBytes=%%n & @SET TotalBytes=%%p

@SET /A TotalSpace=!TotalBytes:~0,-9!
@SET /A FreeSpace=!FreeBytes:~0,-10!
@SET /A TotalUsed=%TotalSpace% - %FreeSpace%
@SET /A PercentUsed=(!TotalUsed!*100)/!TotalSpace!
@SET /A PercentFree=100-!PercentUsed!

IF %TotalSpace% LSS 0 goto error

@ECHO Total space: %TotalSpace%GB
@ECHO Free space: %FreeSpace%GB
@ECHO Used space: %TotalUsed%GB
@ECHO Percent Used: %PercentUsed%%%
@ECHO Percent Free: %PercentFree%%%

@SET TotalSpace=
@SET FreeSpace=
@SET TotalUsed=
@SET PercentUsed=
@SET PercentFree=
goto end

:error
echo.
echo *** Invalid server or drive specified ***
echo.
goto help

:help
echo.
echo diskfree.cmd
echo.
echo Queries remote server for free disk space.
echo Specify a MACHINENAME and a drive letter to be queried
echo.
echo Example: diskfree.cmd MACHINENAME c:
echo.
goto end


:end


Here's an example of the script being run with a target computer named 'LARS', checking for free space on the [F:] volume:





If parameters are not passed or passed incorrectly(wrong drive letter) the script outputs the following or similar:

15 comments:

Anonymous said...

Did you overlook SNMP for a reason?
Every Windows server has the ability to run an SNMP server. It can be found the the standard Windows Components, and configured using the the properties of the SNMP Service itself.

The SNMP Service on Windows uses many of the standard SNMP OIDs for Network interfaces, Disks, Memory, etc.

Lars Rasmussen said...

WMI is enabled by default, and SNMP is not. That being said, SNMP is used for monitoring disk space in our shop.

Overlooked? Nope.

I still use SNMP - we poll on an interval with our monitoring system and fire off alerts(emails) to mobile devices. When responding to a disk space alert, this script is used for a quick percentage calculation. I guess I could wait another 5 or 15 minutes for SNMP to poll again, but I'd rather go back to sleep when paged in the middle of the night.

Maxim said...

Can this CMD file be used with parameters, inputs and outputs? For instance, I would like to run it on a bunch of hosts (e.g. >freespace.tst)
Thank you

Lars Rasmussen said...

maxmax, let me get back to you in a few days.

Note: Try using DIRUSE.EXE for the directory space calculations. I would suggest running this command as multiple parallel queries, though, unless how long it takes isn't of great concern.

Diruse Examples: File and Storage Services
http://technet.microsoft.com/en-us/library/cc786699(WS.10).aspx

Download details: Windows 2000 Resource Kit Tool: Directory Disk Usage(Diruse.exe)
http://www.microsoft.com/downloads/details.aspx?familyid=955D7F2F-73D9-4018-9DD7-42DA210E62EE&displaylang=en

Ed Gonzalez said...

Is there a way to feed this batch file a list of servers and have it write the results to a text file?

Lars Rasmussen said...

Ed and maxmax, here's your answer: http://larsrasmussen.blogspot.com/2010/12/free-space-on-all-servers-for-all-fixed.html

prodipan said...

"Ed and maxmax, here's your answer: http://larsrasmussen.blogspot.com/2010/12/free-space-on-all-servers-for-all-fixed.html"
Sorry to bother you Lars,but i work in infrastructure outsourcing,and my client does not allow power shell.Any way to do this using batch or wmi?

Anonymous said...

Thank your for that great Script!

Unknown said...

I made a small change so this can be run directly from the desktop avoiding opening a command prompt to run the batch:

1) add the following two lines

set /p computername=What is the ComputerName?
set /p Drive=What is the Drive letter (add colon)?

2) modify the variables in the FOR:

@FOR /F "tokens=1-3" %%n IN ('"WMIC /node:"%ComputerName%" LOGICALDISK GET Name,Size,FreeSpace | find /i "%Drive%""') DO @SET FreeBytes=%%n & @SET TotalBytes=%%p

3)add a pause after the reporting

Now you can just double click the batch file on your desktop, add the parameters, and it runs.

Lars Rasmussen said...

Felix Cruz, I'm glad this still being used 5+ years after the original post.

Paul Sterley said...

I'm interesting in using this script.
There are many methods available to check disk space, but I like the simplicity of this one, and the fact that as you said, WMIC is enabled by default everywhere (unlike powershell which I see you moved to later).

But, this script seems to have some math errors. For example, a disk that has a capacity of 536,870,879,232 bytes is listed by this script as being 536 GB capacity instead of its actual capacity of 499 GB.

Is this easy to adjust? Perhaps some basic division to get gigabytes from the byte count?

Thanks for your time.

Anonymous said...

Hello Lars,

Thank you for the thread - it is as good as new.

I am having the same question as Paul.

It is enough to look at the "Capacity" in the "Properties" of the drive to discover the same issue.

Then we just 536,870,879,232/499 and it gives us about 1075893545.

Next if we check drives of the different sizes and do the same division again and again...

But the result would not be the same - it varies with the drive size and it do not ressemble with the number 1024...

However, Windows somehow does this conversion?

Best Regards,
Boris

Anonymous said...

Hello Lars,

To correct my yesterday comment:
I forgot that a Kilobyte is 1024 Bytes, MegaByte is 1024 KiloBytes and GigaByte is 1024 MegaBytes.
Then 1024** 3=1073741824

536870879232 Bytes / 1073741824=499.999969482421875 GB

That looks like the size figure we see in the partition properties in Windows.

Best Regards,
Boris

Paul said...

Can you assist me, cannot get this to work

Windows 2008 Server
@ECHO OFF
IF "%~1"=="" goto help
IF "%~2"=="" goto help

@SETLOCAL ENABLEEXTENSIONS
@SETLOCAL ENABLEDELAYEDEXPANSION

@FOR /F "tokens=1-3" %%n IN ('"WMIC /node:"%ComputerName%" LOGICALDISK GET Name,Size,FreeSpace | find /i "%Drive%""') DO @SET FreeBytes=%%n & @SET TotalBytes=%%p
@SET /A TotalSpace=!TotalBytes:~0,-9!
@SET /A FreeSpace=!FreeBytes:~0,-10!
@SET /A TotalUsed=%TotalSpace% - %FreeSpace%
@SET /A PercentUsed=(!TotalUsed!*100)/!TotalSpace!
@SET /A PercentFree=100-!PercentUsed!

IF %TotalSpace% LSS 0 goto error

@ECHO Total space: %TotalSpace%GB
@ECHO Free space: %FreeSpace%GB
@ECHO Used space: %TotalUsed%GB
@ECHO Percent Used: %PercentUsed%%%
@ECHO Percent Free: %PercentFree%%%

@SET TotalSpace=500
@SET FreeSpace=100
@SET TotalUsed=400
@SET PercentUsed=10
@SET PercentFree=5
@SET /p computername=Bigstix
@SET /p Drive=c:
goto end

:error
echo.
echo *** Invalid server or drive specified ***
echo.
goto help

:help
echo.
echo diskfree.cmd
echo.
echo Queries remote server for free disk space.
echo Specify a MACHINENAME and a drive letter to be queried
echo.
echo Example: diskfree.cmd MACHINENAME c:
echo.
goto end


:end

Unknown said...

Nice Blog..

there is Some helpful hint to making space in your drive or PC.
1. How to free up disk space.
2. disk cleanup
3. how to free up disk space on windows 10