磁盤空間檢測腳本

  1. '********************************************************************* 
  2. '* File: Check Free Space 
  3. '* Created: July , 2011 
  4. '* Last Modified:   July 6, 2011 
  5. '* Version:     1.0 
  6. '* 
  7. '* Main Function: Check Free Space and Send to Administrator 
  8. '* 
  9. '* 
  10. '* Create by Mark.Xu 
  11. '* 
  12. '********************************************************************** 
  13.  
  14. Option Explicit 
  15. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  16. 'Declare variables 
  17. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  18. Const FREE_SPACE_PERCENT = 99 
  19. 'If (FreeSpace / DriverSize) <= FREE_SPACE_PERCENT then Send Mail to Administrator 
  20. Const HARD_DISK = 3 
  21.  
  22. Dim strSMTP 
  23. Dim strSender 
  24. Dim strSendto 
  25. Dim strTitle 
  26. Dim strContext 
  27. Dim strAttachment1 
  28. Dim mailarray(5,0) 'The mail information to array 
  29.  
  30. Dim strSendmail 'The result of sendmail 
  31. Dim strComputer 'The computer 
  32. Dim strHostname 'The hostname 
  33.  
  34. Dim objWmiService,colDisks,objdisk 
  35. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  36. 'Give Value to variables 
  37. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  38. strHostname = getHostname() 
  39. strSMTP = "192.168.1.1" 
  40. strSender = "[email protected]" 
  41. strSendto = "[email protected]" 
  42. strTitle = strHostname & " Check Free Space is Less than " & FREE_SPACE_PERCENT & "% at " & strnowtime(Now) 
  43. strContext = "" 
  44. strAttachment1 = "" 
  45. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  46. 'Check the Free Space for each driver 
  47. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  48. strComputer = "."  
  49. Set objWMIService = GetObject("winmgmts:" _  
  50.     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")  
  51. Set colDisks = objWMIService.ExecQuery _  
  52.     ("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")  
  53. For Each objDisk in colDisks  
  54.     If objDisk.FreeSpace / objDisk.Size <= (FREE_SPACE_PERCENT/100) Then 
  55.         strContext = strContext & _ 
  56.                     "DeviceID: "& objDisk.DeviceID & vbTab & convertByte(objDisk.Size) & vbCr &_ 
  57.                     "Free Disk Space: "&  vbTab & convertByte(objDisk.FreeSpace) & vbCr &_  
  58.                     "Free Disk Space Percent: "  & _ 
  59.                     FormatPercent(objDisk.FreeSpace / objDisk.Size) & vbCr & vbcr 
  60.     End if  
  61. Next 
  62. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  63. 'To Send Mail 
  64. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  65. If strContext <> "" Then 
  66.     mailarray(0,0) = strSMTP 
  67.     mailarray(1,0) = strSender  
  68.     mailarray(2,0) = strSendto  
  69.     mailarray(3,0) = strTitle  
  70.     mailarray(4,0) = strContext 
  71.     mailarray(5,0) = strAttachment1 
  72.     strSendmail = SendMail(mailarray) 
  73. End If 
  74.  
  75. '******************************************************************** 
  76. '* 
  77. '* Function SendMail(arrayMail2d) 
  78. '* Purpose: To Send mail 
  79. '* Input:    
  80. '*       
  81. '* Output:   
  82. '* Notes:   
  83. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  84. 'mailarray format 
  85. '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
  86. 'mailarray(0,0) is SMTP 
  87. 'mailarray(1,0) is Sender 
  88. 'mailarray(2,0) is Sendto 
  89. 'mailarray(3,0) is Title 
  90. 'mailarray(4,0) is Textbody 
  91. 'mailarray(5,0) is Attachment,if nothing then be null 
  92. 'Dim mailarray(5,1) 
  93. 'mailarray(0,0) = "smtp.mailtest.local" 
  94. 'mailarray(1,0) = "[email protected] 
  95. 'mailarray(2,0) = "[email protected] 
  96. 'mailarray(3,0) = "Server ReStart"  
  97. 'mailarray(4,0) = "OK ReStart" 
  98. 'mailarray(5,1) = "D:\script_center.exe" 
  99. '*           
  100. '* 
  101. '******************************************************************** 
  102.  
  103. Function SendMail(arrayMail2d) 
  104. On Error Resume Next 
  105. Dim objMessage 
  106. Dim y 
  107. Set objMessage = CreateObject("CDO.Message"
  108. objMessage.Subject = arrayMail2d(3,0) 
  109. objMessage.Sender = arrayMail2d(1,0) 
  110. objMessage.To = arrayMail2d(2,0) 
  111. objMessage.TextBody = arrayMail2d(4,0) 
  112. For y = 0 To UBound(arraymail2d,2) 
  113. If Trim(arraymail2d(5,y)) <> "" Then 
  114. objMessage.AddAttachment arraymail2d(5,y) 
  115. Else 
  116. 'WScript.Echo arraymail2d(5,y) 
  117. End If 
  118. Next 
  119.  
  120. '==This section provides the configuration information for the remote SMTP server.  
  121. objMessage.Configuration.Fields.Item _  
  122.     ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2  
  123. 'Name or IP of Remote SMTP Server  
  124. objMessage.Configuration.Fields.Item _  
  125.     ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = arrayMail2d(0,0)  
  126.  'Server port (typically 25)  
  127. objMessage.Configuration.Fields.Item _  
  128.     ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25  
  129. objMessage.Configuration.Fields.Update   
  130. objMessage.Send  
  131. If Err.Number = 0 then 
  132. SendMail = Err.Number 
  133. Else  
  134. SendMail = Err.Number & "," & Err.Description 
  135. Err.Clear 
  136. End If 
  137. On Error Goto 0 
  138. End Function 
  139.  
  140.  
  141. '******************************************************************** 
  142. '* 
  143. '* Function getHostname() 
  144. '* Purpose: To get hostname 
  145. '* Input:    
  146. '*       
  147. '* Output:   
  148. '* Notes:   
  149. '******************************************************************** 
  150. Function getHostname() 
  151. Dim objshell 
  152. Dim strRegValue 
  153. Set objshell = CreateObject("Wscript.Shell"
  154. strRegValue = "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Hostname" 
  155. getHostname = objshell.RegRead(strRegValue) 
  156. set objshell = Nothing 
  157. End Function 
  158.  
  159. '******************************************************************** 
  160. '* 
  161. '* Function strnowtime() 
  162. '* Purpose: Convert the time at now to String 
  163. '* Input:   strNow as Date 
  164. '* Output:  strnowtime as String 
  165. '* Notes:   ex "0103_2359"  
  166. '* 
  167. '******************************************************************** 
  168. Function strnowtime(strNow) 
  169. Dim strYear 
  170. Dim strMonth 
  171. Dim strDay 
  172. Dim strhour 
  173. Dim strminute 
  174. strYear = Year(strnow) 
  175. StrMonth = Month(strnow) 
  176. StrDay = Day(strnow) 
  177. Strhour = Hour(strnow) 
  178. Strminute = Minute(strnow) 
  179. If Len(strMonth) = 1 Then  
  180.     StrMonth = "0" & strMonth  
  181. End If 
  182. If Len(strDay) = 1 Then  
  183.     StrDay = "0" & strDay  
  184. End If  
  185. If Len(Strhour) = 1 Then  
  186.     strhour = "0" & strhour  
  187. End If 
  188. If Len(Strminute) = 1 Then  
  189.     Strminute = "0" & strminute  
  190. End If 
  191. strnowtime = strYear & strMonth & strDay & "_" & strhour & strminute 
  192. End Function 
  193.  
  194.  
  195. '******************************************************************** 
  196. '* 
  197. '* Function convertByte(intByte) 
  198. '* Purpose: Output the string size with convert 
  199. '* Input:    
  200. '*       
  201. '* Output:   
  202. '* Notes:  1000 = 1000 (Byte) 
  203. '         10000 = 9.77 (KB) 
  204. '       1000000 = 9.54 (MB) 
  205. '   10000000000 = 9.31 (GB) 
  206. '******************************************************************** 
  207. Function convertByte(intByte) 
  208. If intByte /1024 > 1 Then 
  209.     If intByte /1024/1024 > 1 Then 
  210.         If intByte /1024/1024/1024 > 1 Then 
  211.             convertByte =   FormatNumber((intByte /1024/1024/1024),2) & " (GB)" 
  212.         Else 
  213.             convertByte = FormatNumber((intByte /1024 /1024),2) & " (MB)"    
  214.         End If 
  215.                  
  216.     Else 
  217.         convertByte = FormatNumber((intByte /1024),2) & " (KB)" 
  218.     End If 
  219.  
  220. Else 
  221.     convertByte = intByte & " (Byte)" 
  222. End If  
  223. End Function 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章