Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for secstat.vbs

(view source code of secstat.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim arrMonth, arrOSVer
  4. Dim blnWUAUServ, blnWUOK
  5. Dim dtmLastCheck
  6. Dim intLC, intLD, intLI, intMonth, intToday
  7. Dim colItems, objItem, objReg, objWMISvc
  8. Dim strComputer, strLastBoot, strLastCheck, strLastDownload
  9. Dim strLastInstall, strLB, strMsg, strRegKey, strWUMsg
  10.  
  11. Const HKLM = &H80000002
  12.  
  13. arrMonth  = Array( "Error", "January", "February", "March", "April", "May", "June", _
  14.                   "July", "August", "September", "October", "November", "December" )
  15. blnWUOK   = True
  16.  
  17. With WScript.Arguments
  18. 	If .Named.Count > 0 Then
  19. 		If .Named.Exists( "?" ) And .Named.Count = 1 Then
  20. 			Syntax ""
  21. 		Else
  22. 			Syntax "Invalid command line switches"
  23. 		End If
  24. 	End If
  25. 	If .Unnamed.Count > 1 Then
  26. 		Syntax "Invalid number of command line arguments"
  27. 	End If
  28. 	If .Unnamed.Count = 1 Then
  29. 		strComputer = UCase( .Unnamed(0) )
  30. 	Else
  31. 		strComputer = "."
  32. 	End If
  33. End With
  34.  
  35. ' First check the OS version: XP (5.1) SP2 is the minimum required
  36. Set objWMISvc = GetObject( "winmgmts://" & strComputer & "/root/cimv2" )
  37. Set colItems = objWMISvc.ExecQuery( "Select * from Win32_OperatingSystem", , 48 )
  38. For Each objItem in colItems
  39. 	' Read and store date and time of last boot
  40. 	strLB = Left( objItem.LastBootUpTime, 14 )
  41. 	strLastBoot = Left( strLB, 4 )    & "-" & Mid( strLB, 5, 2 )  & "-" _
  42. 	            & Mid( strLB, 7, 2 )  & " " & Mid( strLB, 9, 2 )  & ":" _
  43. 	            & Mid( strLB, 11, 2 ) & ":" & Mid( strLB, 13, 2 )
  44. 	' Read and check Windows version
  45. 	arrOSVer = Split( objItem.Version, "." )
  46. 	If arrOSVer(0) < 5 Then
  47. 		Syntax "This script requires Windows XP SP2 or later." & vbCrLf _
  48. 		     & "       OS version detected: " & objItem.Caption & " SP " _
  49. 		     & objItem.ServicePackMajorVersion & "." _
  50. 		     & objItem.ServicePackMinorVersion & "."
  51. 	End If
  52. 	If arrOSVer(0) = 5 Then
  53. 		If arrOSVer(1) = 0 Then
  54. 			Syntax "This script requires Windows XP SP2 or later"
  55. 		End If
  56. 		If objItem.ServicePackMajorVersion < 2 Then
  57. 			Syntax "This script requires Windows XP SP2 or later"
  58. 		End If
  59. 	End If
  60. Next
  61.  
  62. ' Check if Windows Update service is running
  63. Set colItems = objWMISvc.ExecQuery( "Select * From Win32_Service Where Name='wuauserv'", , 48 )
  64. For Each objItem in colItems
  65. 	If objItem.State = "Running" Then
  66. 		blnWUAUServ = True
  67. 	Else
  68. 		blnWUAUServ = False
  69. 	End If
  70. Next
  71.  
  72. ' Custom error handling is required, since many properties to
  73. ' be queried may be invalid for the installed security products
  74. 'On Error Resume Next
  75.  
  76. ' Connect to the local or remote SecurityCenter through WMI
  77. Set objWMISvc = GetObject( "winmgmts:{impersonationLevel=impersonate}!//" _
  78.               & strComputer & "/root/SecurityCenter" )
  79. If Err Then
  80. 	If strComputer = "." Then
  81. 		Syntax "Could not connect to SecurityCenter"
  82. 	Else
  83. 		Syntax "Could not connect to SecurityCenter on " & strComputer
  84. 	End If
  85. End If
  86.  
  87. ' Query the installed AntiVirus product
  88. Set colItems = objWMISvc.ExecQuery( "SELECT * FROM AntiVirusProduct" )
  89. If Err Then
  90. 	Syntax "No AntiVirus product detected by WMI"
  91. End If
  92.  
  93. strMsg = vbCrLf & "AntiVirus:" & vbCrLf
  94. If InStr( UCase( WScript.FullName ), "\CSCRIPT.EXE" ) Then strMsg = strMsg & "=========="
  95.  
  96. For Each objItem in colItems
  97. 	With objItem
  98. 		'WScript.Echo .companyName
  99. 		'WScript.Echo .instanceGuid
  100. 		strMsg = strMsg & vbCrLf & .displayName & ",  Version " & .versionNumber
  101. 		If .onAccessScanningEnabled Then
  102. 			strMsg = strMsg & vbCrLf & "On-access scanning is enabled"
  103. 		Else
  104. 			strMsg = strMsg & vbCrLf & "Warning: on-access scanning is disabled!"
  105. 		End If
  106. 		If .productUptoDate Then
  107. 			strMsg = strMsg & vbCrLf & "Virus definitions are up-to-date"
  108. 		Else
  109. 			strMsg = strMsg & vbCrLf & "Warning: virus definitions are NOT up-to-date!"
  110. 		End If
  111. 	End With
  112. Next
  113.  
  114. ' Query the installed Firewall product
  115. Set colItems = objWMISvc.ExecQuery( "SELECT * FROM FirewallProduct" )
  116. If Err Then
  117. 	Syntax "No firewall detected by WMI"
  118. End If
  119.  
  120. strMsg = strMsg & vbCrLf & vbCrLf & "Firewall:" & vbCrLf
  121. If InStr( UCase( WScript.FullName ), "\CSCRIPT.EXE" ) Then strMsg = strMsg & "========="
  122.  
  123. For Each objItem in colItems
  124. 	With objItem
  125. 		'WScript.Echo .companyName
  126. 		'WScript.Echo .instanceGuid
  127. 		strMsg = strMsg & vbCrLf & .displayName & ", Version " & .versionNumber
  128. 		If .enabled Then
  129. 			strMsg = strMsg & vbCrLf & "Firewall is enabled"
  130. 		Else
  131. 			strMsg = strMsg & vbCrLf & "Warning: firewall is disabled!"
  132. 		End If
  133. 	End With
  134. Next
  135.  
  136. strMsg = strMsg & vbCrLf & vbCrLf & "Windows Update:" & vbCrLf
  137. If InStr( UCase( WScript.FullName ), "\CSCRIPT.EXE" ) Then strMsg = strMsg & "==============="
  138.  
  139. ' Check if the Windows Update service is running
  140. If blnWUAUServ Then
  141. 	strMsg = strMsg & vbCrLf & "Windows Update Service is active"
  142. Else
  143. 	strMsg = strMsg & vbCrLf & "Warning: Windows Update Service NOT active!"
  144. 	blnWUOK = False
  145. End If
  146.  
  147. ' Windows Update results are read from the registry
  148. Set objReg = GetObject( "winmgmts:{impersonationLevel=impersonate}!//" _
  149.            & strComputer & "/root/default:StdRegProv" )
  150.  
  151. strRegKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\Results\"
  152.  
  153. ' Read the time and date of the last successful download
  154. objReg.GetStringValue HKLM, strRegKey & "Download", "LastSuccessTime", strLastDownload
  155. strWUMsg = vbCrLf & "Last Download : " & strLastDownload
  156. ' Convert the time and date string to a long integer
  157. intLD = Left( strLastDownload, 10 )
  158. intLD = Replace( IntLD, " ", "" )
  159. intLD = Replace( intLD, "-", "" )
  160. intLD = Replace( intLD, ":", "" )
  161. intLD = CLng( intLD )
  162.  
  163. ' Read the time and date of the last successful install
  164. objReg.GetStringValue HKLM, strRegKey & "Install",  "LastSuccessTime", strLastInstall
  165. strWUMsg = strWUMsg & vbCrLf & "Last Install  : " & strLastInstall
  166. ' Convert the time and date string to a long integer
  167. intLI = Left( strLastInstall, 10 )
  168. intLI = Replace( intLI,  " ", "" )
  169. intLI = Replace( intLI, "-", "" )
  170. intLI = Replace( intLI, ":", "" )
  171. intLI = CLng( intLI )
  172.  
  173. ' Insert the line with the last reboot date and time
  174. strWUMsg = strWUMsg & vbCrLf & "Last Reboot   : " & strLastBoot
  175.  
  176. ' Read the time and date of the last successful check for new updates
  177. objReg.GetStringValue HKLM, strRegKey & "Detect",   "LastSuccessTime", strLastCheck
  178. strWUMsg = strWUMsg & vbCrLf & "Last Check    : " & strLastCheck
  179. ' Convert the time and date string to a long integer
  180. intLC = Left( strLastCheck, 10 )
  181. intLC = Replace( intLC, " ", "" )
  182. intLC = Replace( intLC, "-", "" )
  183. intLC = Replace( intLC, ":", "" )
  184. intLC = CLng( intLC )
  185. ' Convert the long integer to a true date
  186. intMonth = CInt( "1" & Mid( intLC, 5, 2 ) ) - 100
  187. dtmLastCheck = CDate( Mid( intLC, 7, 2 ) & " " & arrMonth(intMonth) & " " & Left( intLC, 4 ) )
  188.  
  189. On Error Goto 0
  190.  
  191. ' WU check includes the following requirements:
  192. ' * Automatic Updates service must be running
  193. ' * last successful download must be BEFORE last successful install
  194. ' * last reboot must be AFTER last successful install
  195. ' * last check must be no more than 7 days ago
  196. ' If any of these tests fails, all intermediate results will be displayed,
  197. ' if all tests are passed, a brief summary will be displayed
  198. If intLD > intLI Then
  199. 	strWUMsg = strWUMsg & vbCrLf & "Warning: last install was unsuccessful!"
  200. 	blnWUOK  = False
  201. End If
  202.  
  203. If DateDiff( "d", dtmLastCheck, Now ) > 7 Then
  204. 	strWUMsg = strWUMsg & vbCrLf & "Warning: the last check was " & DateDiff( "d", dtmLastCheck, Now ) & " days ago!"
  205. 	blnWUOK  = False
  206. End If
  207.  
  208. If CLng( Left( strLB, 8 ) ) < intLI Then
  209. 	strWUMsg = strWUMsg & vbCrLf & "Warning: a reboot is required!"
  210. 	blnWUOK  = False
  211. End If
  212.  
  213. If blnWUOK Then strWUMsg = vbCrLf & "Windows Update seems to be functioning OK"
  214.  
  215. WScript.Echo strMsg & strWUMsg
  216.  
  217. Set objReg    = Nothing
  218. Set colItems  = Nothing
  219. Set objWMISvc = Nothing
  220.  
  221.  
  222. Sub Syntax( myMsg )
  223. 	If myMsg <> "" Then
  224. 		strMsg = vbcrlf & "Error: " & myMsg & vbCrLf
  225. 	Else
  226. 		strMsg = ""
  227. 	End If
  228. 	strMsg = strMsg & vbCrLf _
  229. 	       & "SecStat.vbs,  Version 2.01 for Windows XP SP2 and later" _
  230. 	       & vbCrLf _
  231. 	       & "Display a SecurityCenter and Windows Update status overview for any computer" _
  232. 	       & vbCrLf & vbCrLf _
  233. 	       & "Usage:  SECSTAT.VBS  [ computer ]" _
  234. 	       & vbCrLf & vbCrLf _
  235. 	       & "Where:  ""computer""   is an optional remote computer name" _
  236. 	       & vbCrLf _
  237. 	       & "                     (default is the local computer)" _
  238. 	       & vbCrLf & vbCrLf _
  239. 	       & "Inspired by an entry in Alejandro Campos Magencio's blog:" _
  240. 	       & vbCrLf _
  241. 	       & "http://blogs.msdn.com/alejacma/archive/2008/05/12/how-" _
  242. 	       & vbCrLf _
  243. 	       & "to-get-antivirus-information-with-wmi-vbscript.aspx." _
  244. 	       & vbCrLf & vbCrLf _
  245. 	       & "Use WBEMTEST to find all properties for specific products." _
  246. 	       & vbCrLf & vbCrLf _
  247. 	       & "Written by Rob van der Woude" _
  248. 	       & vbCrLf _
  249. 	       & "http://www.robvanderwoude.com"
  250. 	WScript.Echo strMsg
  251. 	WScript.Quit 1
  252. End Sub
  253.  

page last modified: 2024-02-26; loaded in 0.0253 seconds