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%.
rbrown860Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
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.
0
Bill BachPresident and Btrieve GuruCommented:
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.
0
rbrown860Author Commented:
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

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Bill BachPresident and Btrieve GuruCommented:
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.
0
Bill BachPresident and Btrieve GuruCommented:
Or, to simplify the code:
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) & "%)" & vbCrLf _
			& "-----------------------------------"
			Wscript.Echo DiskStatus

			Wscript.Echo "Checking disk usage limits......"
			IF (OccupiedSpace/TotalSize) * 100 > 75 Then
				GenerateEmail DiskStatus
			Else
				Wscript.Echo "Disk Usage under limits."
			End If
		end if
	Next
	
Function GenerateEmail(ByVal PassedDiskStatus)
	Set MyEmail=CreateObject("CDO.Message")
	MyEmail.Subject="Disk Usage On Server XXXX Over Limit"
	MyEmail.From="username@domain.com"	
	MyEmail.To="username@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")="smtpservername"
	'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

Open in new window


All that is left is to create a scheduled task that runs as needed (I'd think about running this every 4 hours or so, at least) to call CScript and pass it the vbs script, and you're done.
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rbrown860Author Commented:
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?
0
Bill BachPresident and Btrieve GuruCommented:
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.
0
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.