Link to home
Start Free TrialLog in
Avatar of Richard Brown
Richard BrownFlag for United States of America

asked on

Need help in writing a VB Script file

I am trying to write a script using VBScript that would automate the disk usage scan of a server every 30 days at 1:00 A.M. This script would check percentage of disk usage, space available and send an email to my computer that would display the finding. I need to ensure modularization to simplify the script. I would need comments to ensure that I can make changes as needed. Also, need error handling to send a message in case disk usage is over 55%.
Avatar of aikimark
Flag of United States of America image

What is your question?
Do you need help with the code you've written so far?
Does this have to be VBScript, or can it be written in Powershell?

If you are looking for professional help, please look at the list of top VBScript experts to see if they have enabled their Hire Me link.
This is really less than a few dozen lines in VBScript, and a very small task that can be part off a larger monitoring system. (Yes, I have indeed written this function into my own database monitoring solution.) Can you post what you have already written, and what part is not working, and we can help figure out what is missing or wrong with it?

I would also argue that checking every 30 days seems a bit useless. If you have a runaway log file or malware on the box, it can suck up disk space in minutes, not a month. Second, checking for 55 percent seems to be a strange point,as it leaves a majority of your disk space unused. It would make more sense to send a warning at 75 and am urgent error at 90 percent.
Avatar of Richard Brown


Here is what I have so far. Any suggestions would be helpful. Oh, and btw it is not working yet.
strComputer = "." 

	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
	Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk where DeviceID='c:'",,48) 
	For Each objItem in colItems 
		If len(objItem.VolumeName)>0 then
			TotalSize = CDbl(objItem.Size)/1024/1024/1024
			FreeSpace = CDbl(objItem.FreeSpace)/1024/1024/1024
			OccupiedSpace = CDbl(objItem.Size - objItem.FreeSpace)/1024/1024/1024
			DiskStatus = "-----------------------------------" & vbCrLf _
			& "Volume Name:" & vbTab & objItem.VolumeName  & vbCrLf _
			& "-----------------------------------" & vbCrLf _
			& "Total Size of the Disk:" & vbTab & vbTab _
			& FormatNumber(TotalSize)  & " GB" & vbCrLf _
			& "FreeSpace:" & vbTab & vbTab & vbTab _
			& FormatNumber(FreeSpace)  & " GB (" & round(((FreeSpace/TotalSize) * 100),2) & "%)" & vbCrLf _
			& "Occupied Space:" & vbTab & vbTab & vbTab _
			& FormatNumber(OccupiedSpace) & " GB (" & round(((OccupiedSpace/TotalSize) * 100),2) & "%)"
			Wscript.Echo DiskStatus
		end if
		Wscript.Echo "-----------------------------------"
		Response = MsgBox("Do you wish to send the disk status via Email?", vbYesNo)
		If Response = vbYes Then
			Wscript.Echo "Sending Email..... "
			GenerateEmail DiskStatus
			Wscript.Echo "Email Sent Out!"
		End If
		Wscript.Echo "Checking disk usage limits......"
		IF (OccupiedSpace/TotalSize) * 100 > 75 Then
			HandleOverUsage OccupiedSpace
			Wscript.Echo "Disk Usage under limits."
		End If
Function GenerateEmail(ByVal PassedDiskStatus)
	Set MyEmail=CreateObject("CDO.Message")

	MyEmail.Configuration.Fields.Item ("")=2
	'SMTP Server
	MyEmail.Configuration.Fields.Item ("")=""
	'SMTP Port
	MyEmail.Configuration.Fields.Item ("")=25 


	set MyEmail=nothing
End Function

Function HandleOverUsage(ByVal OccupiedSpace)
	Wscript.Echo "Disk Usage exceeded 75%. "
End Function

Open in new window

Looks perfect to me.  The disk space checking seems to be working just perfectly.  I assume that the text output and question about sending the Email s are for interactive testing only.  You obviously wouldn't have any need for this inside an automated process.

The Email is ALSO working just fine.  Obviously, you have to have the proper Email configuration entered into the app, but once you do, you get an Email that looks like this:

Volume Name:	OS
Total Size of the Disk:		465.17 GB
FreeSpace:			270.60 GB (58.17%)
Occupied Space:			194.57 GB (41.83%)

Open in new window

Another obvious improvement would be to set a variable as to whether the Email should go or not, and send it accordingly.  Further, yuou might want to customize the subject line a bit, such as "Disk Space Warning on Server NAME", but it is working perfectly as is.
Avatar of Bill Bach
Bill Bach
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, I have maxed out on my abilities. I need some help in going forward. I like all of your suggestions can you give me the code that would do those things?
Depends on your OS. Find the windows task scheduler and create a task that looks like "cscript diskcheck.vbs" or whatever you want to call the script file. If you need specific instructions, pay the exact os version, and I can find a virtual server with this version tomorrow.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.