Solved

Low Hard Drive Space Notification

Posted on 2011-02-12
18
774 Views
Last Modified: 2013-11-21
Hi all

we are in Domain enviroment. We need to implement an idea of "Low disk Space Notification". The Idea is when any of the drives/volumes reaches a specified limit, it should notify the user.
can this be enhanced, I mean it scans the drive for large folders/files & notify the owner of that folder/file.

I need advise from experts on this, how we can achive this in best way. (thourgh SCCM or login script a utility etc..)
0
Comment
Question by:shahzamm
  • 9
  • 6
  • 2
  • +1
18 Comments
 
LVL 8

Expert Comment

by:jimsweb
ID: 34881398
Hope this helps:
usageLog = "C:\UsageLog.txt"
minCapacity = 0.05	' 5%
 
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set oDrive = fso.GetDrive("C")
curSpace = oDrive.FreeSpace
totSize = oDrive.TotalSize
 
curUsage = curSpace/totSize
curFree = 1 - curUsage
WScript.Echo "Current free space: " & FormatNumber(oDrive.FreeSpace,0) & vbLf & _
		   "Total size: " & FormatNumber(oDrive.TotalSize,0) & vbLf & _
		   "Current usage(%): " & FormatPercent(curUsage, 2) & vbLf & _
		   "Current free(%): " & FormatPercent(curFree, 2)
 
 
'-------------------------
' Check the capacity here
'-------------------------
if curFree < minCapacity then
	WScript.Echo "Too low!"
 
	' write log file here
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set f = fso.CreateTextFile(usageLog, True)
	f.WriteLine("Space too low!")
	f.Close
else
	WScript.Echo "OK!"
end if

Open in new window



credit: sungenwang
0
 
LVL 7

Author Comment

by:shahzamm
ID: 34881421
its showing the current status. is it possible if we need to use it as login script & it notifies only when there is low space, not one every login?
its showing space in bytes, while we can live with percentage and/or GBs.
it does not show which drive is having what space
0
 
LVL 91

Expert Comment

by:nobus
ID: 34881706
0
 
LVL 7

Author Comment

by:shahzamm
ID: 34882205
Thanks guys for quick response
we are not ready yet for powershell. still using VB..any suggestions very much appriciated..
0
 
LVL 91

Expert Comment

by:nobus
ID: 34882471
sorry - it's all i found
0
 
LVL 7

Author Comment

by:shahzamm
ID: 34885492
anyone else form community shed some light please
0
 
LVL 12

Expert Comment

by:Daz_1234
ID: 34886891
Hi shahzamm,

Below is a sample of a simple function I wrote some years ago.

The script basically gets the free space for the selected drive into a variable and outputs to the screen in a message box.  If you already have VBScripts that you use at logon, you should be able to very easily incorporate the function into an existing script.

Hope this helps,
Daz.

'Check space on C drive

sDriveToCheck = "C"
sSpace = fShowFreeSpace(sDriveToCheck)

MsgBox "Free space on " & sDriveToCheck & " = " & sSpace & " GB",64 , "C Drive Free Space Result"



Function fShowFreeSpace(strDrive)
    Dim fso, d, s
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set d = fso.GetDrive(strDrive)
    s = Round(d.FreeSpace / (1024 * 1024 * 1024), 2)
    fShowFreeSpace = s
End Function

Open in new window

0
 
LVL 7

Author Comment

by:shahzamm
ID: 34887230
Its really simple, this is what I am looking
sorry for asking more but I am not developing guru.
can it be modified
1. run at each login
2. if drive space is below 10% then notify...
3. else don't notify.
0
 
LVL 12

Expert Comment

by:Daz_1234
ID: 34887495

Add the following to your existing logon script (or get your logon script to call it).  I have assumed that you only want the 'C' drive checked.

If the drive has more than 10% free, the script will not output anything.  If you want to test it, play with the line:
iThresholdPercent = 10

Just to comment on the other possible requirement in your original post, scanning the drive for large files (or any files) is NOT advisable in a logon script as this could hold up the logon for many minutes.

Hope this helps
Daz.
'Check space on C drive

Dim sDriveToCheck, iThresholdPercent, sSpacePercent

sDriveToCheck = "C"
iThresholdPercent = 10

sSpacePercent = fGetFreeSpace(sDriveToCheck)

If sSpacePercent < iThresholdPercent Then
    MsgBox "W A R N I N G:  Your '" & sDriveToCheck & "' drive is " & 100 - sSpacePercent & "% full.  Please delete any unwanted files as soon as possible.", vbExclamation + vbSystemModal, "Drive '" & sDriveToCheck & "' free space Warning!"
End If


Function fGetFreeSpace(strDrive)
    Dim fso, oDrive, dblSpace, dblTotalSize, temp, iPercent
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oDrive = fso.GetDrive(strDrive)
    dblTotalSize = oDrive.TotalSize
    dblSpace = oDrive.FreeSpace
    temp = dblSpace / dblTotalSize * 100
    iPercent = CInt(temp)
    fGetFreeSpace = iPercent
End Function

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 7

Author Comment

by:shahzamm
ID: 34894051
Just one last thing..not sure if doable
fyi: we have one HDD with C: & D: partitions. user profile are in D: & system is in C:

a script which run with a delay after login
check the current user profile size. warn if size is more than 50% of total partition size (D:).

This will reduce the burdon over IT support to run a utility & then find which folder is taking space

Thank you very much for your support
0
 
LVL 12

Expert Comment

by:Daz_1234
ID: 34895359
Possible solution for your additional requirement:

1. save the following in a script called 'ProfileCheck.vbs'
Dim intThresholdPercent, intProfilePercent, intPause

intPause = 20000  '# 20 Seconds
WScript.Sleep intPause

intThresholdPercent = 50

intProfilePercent = fGetCurrentUserProfilePercent()

If intProfilePercent > intThresholdPercent Then
    MsgBox "W A R N I N G:  Your profile is using " & intProfilePercent & "% of the volume.", vbExclamation + vbSystemModal, "Profile size Warning!"
End If

Function fGetCurrentUserProfilePercent()
    Dim fso, wshShell, oDrive, sProfileFolder, oProfileFolder, sProfileDrive, dblProfileSize, dblTotalSize, temp, iPercent
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set wshShell = CreateObject("WScript.Shell")
    sProfileFolder = wshShell.ExpandEnvironmentStrings("%USERPROFILE%")
    sProfileDrive = Left(sProfileFolder, 1)
    Set oProfileFolder = fso.GetFolder(sProfileFolder)
    Set oDrive = fso.GetDrive(sProfileDrive)
    dblTotalSize = oDrive.TotalSize
    dblProfileSize = oProfileFolder.Size
    temp = dblProfileSize / dblTotalSize * 100
    iPercent = CInt(temp)
    fGetCurrentUserProfilePercent = iPercent
End Function

Open in new window


2. Add the following lines into one of your VBScript logon scripts:
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run "WScript.exe ""Path\To\ProfileCheck.vbs""", 1, False

Open in new window


The 'False' in the line above means that calling the ProfileCheck.vbs will not halt the logon script.

Regards,
Daz
0
 
LVL 7

Author Comment

by:shahzamm
ID: 35010923
@Daz
its working on some machines perfectly with an error on 28, 5. permission denied.. but on some its just the error 28,5 permission denied. what could be this
0
 
LVL 12

Expert Comment

by:Daz_1234
ID: 35010967
Hi,

What is giving the error, drive free space check or profile size check?

Could you please post the script that giving the error, and identify what is line 28 (if it is not obvious)?

Regards,
Daz.
0
 
LVL 7

Author Comment

by:shahzamm
ID: 35014484
attached is the error for your script for the script below. its your provided script but I changed size limit to test.

Dim intThresholdPercent, intProfilePercent, intPause

intPause = 20000  '# 20 Seconds
WScript.Sleep intPause

intThresholdPercent = 90

intProfilePercent = fGetCurrentUserProfilePercent()

If intProfilePercent > intThresholdPercent Then
    MsgBox "W A R N I N G:  Your profile is using " & intProfilePercent & "% of the volume.", vbExclamation + vbSystemModal, "Profile size Warning!"
End If

Function fGetCurrentUserProfilePercent()
    Dim fso, wshShell, oDrive, sProfileFolder, oProfileFolder, sProfileDrive, dblProfileSize, dblTotalSize, temp, iPercent
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set wshShell = CreateObject("WScript.Shell")
    sProfileFolder = wshShell.ExpandEnvironmentStrings("%USERPROFILE%")
    sProfileDrive = Left(sProfileFolder, 1)
    Set oProfileFolder = fso.GetFolder(sProfileFolder)
    Set oDrive = fso.GetDrive(sProfileDrive)
    dblTotalSize = oDrive.TotalSize
    dblProfileSize = oProfileFolder.Size
    temp = dblProfileSize / dblTotalSize * 100
    iPercent = CInt(temp)
    fGetCurrentUserProfilePercent = iPercent
End Function

scripterror.JPG
0
 
LVL 12

Expert Comment

by:Daz_1234
ID: 35015752
Hi,

My script works as a non-admin user on XP.

You did not say what OS, but from your screenshot of the error it is a flavour of Vista or Windows 7.  I do not have Vista, but will fire my Windows 7 VM to have a look.

What OS are you using and is it 32 or 64 bit?

Daz
0
 
LVL 7

Author Comment

by:shahzamm
ID: 35016107
32 bit vista & win 7
0
 
LVL 12

Accepted Solution

by:
Daz_1234 earned 500 total points
ID: 35016577
Ok it looks like we've hit a WSH bug.  If you want to get the Size of a folder you can get permission denied even if you have full control. The same error does not happen with other scripting methods (.Net or Powershell).

This forum conversation describes the problem well:
http://www.edugeek.net/forums/coding/6409-filesystemobject-folder-why-permission-denied.html

Please try the amended code below that should work for all 32-bit Windows OS.

As I said before, it won't show any message unless it hits the threshold, which is configured in line 9 of my amended script below.

Hope this helps,
Daz.

Option Explicit

Dim fso, intThresholdPercent, intProfilePercent, intPause
Set fso = CreateObject("Scripting.FileSystemObject")

intPause = 20000  '# 20 Seconds
WScript.Sleep intPause

intThresholdPercent = 90

intProfilePercent = fGetCurrentUserProfilePercent()

If intProfilePercent >= intThresholdPercent Then
    MsgBox "W A R N I N G:  Your profile is using " & intProfilePercent & "% of the volume.", vbExclamation + vbSystemModal, "Profile size Warning!"
End If

Function fGetCurrentUserProfilePercent()
    Dim wshShell, oDrive, sProfileFolder, sProfileDrive, dblProfileSize, dblTotalSize, temp, iPercent
    Set wshShell = CreateObject("WScript.Shell")
    sProfileFolder = wshShell.ExpandEnvironmentStrings("%USERPROFILE%")
    sProfileDrive = Left(sProfileFolder, 1)
    Set oDrive = fso.GetDrive(sProfileDrive)
    dblTotalSize = oDrive.TotalSize
    dblProfileSize = fGetFolderSize(sProfileFolder)
    temp = dblProfileSize / dblTotalSize * 100
    iPercent = CInt(temp)
    fGetCurrentUserProfilePercent = iPercent
End Function

Function fGetFolderSize(sFolderName)
    'Credit: This function adapted from: http://serverfault.com/questions/109881/get-user-profile-size-in-vbscript
    On Error Resume Next

    Dim oFolder, oSubFolder, dblSize, blHasSubFolders

    dblSize = 0
    blHasSubfolders = False

    Set oFolder = fso.GetFolder(sFolderName)
    ' Try the non-recursive way first (potentially faster?)
    Err.Clear
    dblSize = oFolder.Size

    If Err.Number <> 0 then     ' Did not work; do recursive way:
        For Each oSubfolder in oFolder.SubFolders
            dblSize = dblSize + fGetFolderSize(oSubfolder.Path)
            blHasSubfolders = True
        Next

        If Not blHasSubfolders then
            dblSize = oFolder.Size
        End If
    End If

    fGetFolderSize = dblSize

    Set oFolder = Nothing        ' release object
End Function

Open in new window

0
 
LVL 7

Author Closing Comment

by:shahzamm
ID: 35042676
Thanks Daz..Exactly what I was looking for
0

Featured Post

Do email signature updates give you a headache?

Do you feel like you are constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Join & Write a Comment

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
Know what services you can and cannot, should and should not combine on your server.
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now