Mar 25, 2008

VBScript - 檢查 Disk 空間, 並寄送通知信

Dim fso, d, s, oArgs, drvPath, threshold, fspace, ret, cmdstr
Const cdoSendUsingPort = 2
Set oArgs=WScript.Arguments
'Set objNewMail = WScript.CreateObject("CDONTS.NewMail")
sch = "http://schemas.microsoft.com/cdo/configuration/"
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item(sch & "sendusing") = 2 ' cdoSendUsingPort
.Item(sch & "smtpserver") = "192.168.2.14"
.update
End With
If oArgs.Count < 3 Then Call DisplayUsage
drvPath=oArgs(0)
threshold=CDbl(oArgs(1))
aceno=oArgs(2)+"0151"
'on error resume next

Set fso = CreateObject("Scripting.FileSystemObject")
Set d = fso.GetDrive(fso.GetDriveName(drvPath))
fspace=Round((d.FreeSpace / 1024 /1024),2)
desc="Free Space " + CStr(fspace) + "M < " + CStr(threshold) + "M"
if (fspace < threshold) and (Not IsEmpty(d)) then thistime=cstr(tran(Year(now)))+"/"+cstr(tran(Month(now)))+"/"+cstr(tran(Day(now)))+"" thistime=thistime+cstr(tran(Hour(now)))+":"+cstr(tran(Minute(now)))+":"+cstr(tran(Second(now)))
mail_subject="SAM" + aceno + ": " + drvPath + " " + desc
strBody="start_time=" + thistime
strBody=strBody+"system_type=Windows"
strBody=strBody+"node_name=" + drvPath
strBody=strBody+"object_name=DISK SPACE"
strBody=strBody+"desc=" + desc
Set cdoMessage = CreateObject("CDO.Message")
With cdoMessage
Set .Configuration = cdoConfig
.From = "test@gmail.com"
.To = "test@gmail.com"
.Subject = mail_subject
.htmlbody = strBody
.Send
End With
Set cdoMessage = Nothing
Set cdoConfig = nothing
end if

WScript.Quit

Sub DisplayUsage
dim strUsage
strUsage="Usage: GetFreeSpace [drive] [threshold(MB)] [alert level]" +chr(13)+chr(10)
strUsage=strUsage+"Example: GetFreeSpace \\192.168.1.1\c$ 5000 1"
WScript.Echo strUsage
WScript.Quit
End Sub
function tran(tt)
if int(tt)<10 then
tran="0"&tt
else
tran=tt
end if
end function

0 意見: