Why I created the script
I needed a script that could notify users about their hard drive having to little free disk space. Many of our users didn't know or care about this and it became a problem when software updates could not become installed (many of them use laptops and are travelling a lot, so we could not force them to use file server storage).What is does
When you run the script you provide it with the amount of needed disk space in GB and in %. If the free disk space is lower than either of these values, the script will send a message to the logged on user.At the moment I only care about the system drive.
For this picture I set very high requirements to get the msg (100GB or 80% free) |
How I run it
I run this script as a scheduled task that runs on a 30 second logon delay and every hour when a user is logged on. It also runs as the logged on user to make sure the script can sent the MSG as the user to the user. To avoid the black cmd screen from popping up, I launch it with the invisible.vbs script.
The script
@ECHO OFF
:: Script get data from input and returns 0 if conditions met.
:: davpe67 2016-02-11
IF "%~1" == "" GOTO :SYNTAX
IF "%~2" == "" GOTO :SYNTAX
SET GBNEEDED=%~1
SET PERCENTNEEDED=%~2
SET TIMEOUT=%~3
IF "%~3" == "" SET TIMEOUT=1440
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=1-3" %%n IN ('WMIC LOGICALDISK GET Name^,Size^,FreeSpace^|findstr /i "c:"') DO (
SET FreeBytes=%%n
SET TotalBytes=%%p
)
SET /A TotalSpace=!TotalBytes:~0,-9!
SET /A FreeSpace=!FreeBytes:~0,-9!
SET /A TotalUsed=%TotalSpace% - %FreeSpace%
SET /A PercentUsed=(!TotalUsed!*100)/!TotalSpace!
SET /A PercentFree=100-!PercentUsed!
::ECHO Total space: %TotalSpace%GB
::ECHO Free space: %FreeSpace%GB
::ECHO Used space: %TotalUsed%GB
::ECHO Percent Used: %PercentUsed%%%
::ECHO Percent Free: %PercentFree%%%
IF %FREESPACE% LSS %GBNEEDED% SET NEEDMOREDISK=1
IF %PERCENTFREE% LSS %PERCENTNEEDED% SET NEEDMOREDISK=1
IF "%NEEDMOREDISK%" == "1" (
ECHO %COMPUTERNAME% is running low on diskspace on C: ^(%FREESPACE%GB ^/ %PERCENTFREE%%%^)>%TEMP%\msgfile.tmp
ECHO If there is not enough free space the computer will become slow >>%TEMP%\msgfile.tmp
ECHO becuse it needs more time to find sectors to write the data.>>%TEMP%\msgfile.tmp
ECHO A golden rule is that at least 10%% and^/or 10GB ^(whatever is biggest^)>>%TEMP%\msgfile.tmp
ECHO of free space is needed by workstations.>>%TEMP%\msgfile.tmp
ECHO.>>%TEMP%\msgfile.tmp
ECHO Some programs wont install or upgrade ^(old versions may be removed^)>>%TEMP%\msgfile.tmp
ECHO until you have cleared at least %GBNEEDED%GB and^/or %PERCENTNEEDED%%%.>>%TEMP%\msgfile.tmp
ECHO.>>%TEMP%\msgfile.tmp
ECHO Most users have way more data stored locally than they need to.>>%TEMP%\msgfile.tmp
ECHO Move files you are not working with to the home- or nobackup-server.>>%TEMP%\msgfile.tmp
ECHO If you need help to make more space, contact support@yourcompany.com>>%TEMP%\msgfile.tmp
MSG %USERNAME% /TIME:%TIMEOUT%<%TEMP%\msgfile.tmp
EXIT /B 911
)
EXIT /B 0
GOTO :EOF
:SYNTAX
ECHO.
ECHO %~nx0 ^<GBNEEDED^> ^<PERCENTNEEDED^> ^<TIMEOUT^>
ECHO.
ECHO GBNEEDED is number of GB och free disk needed
ECHO PERCENTNEEDED is how many percent of total disk need to be free
ECHO TIMEOUT is how long the user message will remain on the screen