Avatar of Richard Brown
Richard Brown
Flag 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%.
Visual Basic ClassicProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
Martin Liss

8/22/2022 - Mon
aikimark

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.
Bill Bach

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.
Richard Brown

ASKER
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
		Else
			Wscript.Echo "Disk Usage under limits."
		End If
	Next
	
Function GenerateEmail(ByVal PassedDiskStatus)
	Set MyEmail=CreateObject("CDO.Message")
	MyEmail.Subject="Subject"
	MyEmail.From="name@domain.com"	
	MyEmail.To="recipient@domain.com"
	MyEmail.TextBody=PassedDiskStatus

	MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing")=2
	'SMTP Server
	MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver")="smtp.server.com"
	'SMTP Port
	MyEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport")=25 

	MyEmail.Configuration.Fields.Update
	MyEmail.Send

	set MyEmail=nothing
End Function

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

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Bill Bach

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.
ASKER CERTIFIED SOLUTION
Bill Bach

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Richard Brown

ASKER
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?
Bill Bach

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.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Martin Liss

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.