Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2083 lines
94 KiB

  1. '********************************************************************
  2. '*
  3. '* Copyright (c) Microsoft Corporation. All rights reserved.
  4. '*
  5. '* Module Name: EVENTQUERY.vbs
  6. '*
  7. '* Abstract: Enables an administrator to query/view all existing
  8. '* events in a given event log(s).
  9. '*
  10. '*
  11. '********************************************************************
  12. ' Global declaration
  13. OPTION EXPLICIT
  14. ON ERROR RESUME NEXT
  15. Err.Clear
  16. '----------------------------------------------------------------
  17. ' Start of localization Content
  18. '----------------------------------------------------------------
  19. ' the filter operators specified by the user
  20. CONST L_OperatorEq_Text = "eq"
  21. CONST L_OperatorNe_Text = "ne"
  22. CONST L_OperatorGe_Text = "ge"
  23. CONST L_OperatorLe_Text = "le"
  24. CONST L_OperatorGt_Text = "gt"
  25. CONST L_OperatorLt_Text = "lt"
  26. ' the filters as given by the user
  27. CONST L_UserFilterDateTime_Text = "datetime"
  28. CONST L_UserFilterType_Text = "type"
  29. CONST L_UserFilterUser_Text = "user"
  30. CONST L_UserFilterComputer_Text = "computer"
  31. CONST L_UserFilterSource_Text = "source"
  32. CONST L_UserFilterDateCategory_Text = "category"
  33. CONST L_UserFilterId_Text = "id"
  34. ' Define default values
  35. CONST L_ConstDefaultFormat_Text = "TABLE"
  36. ' Define other format values
  37. CONST L_Const_List_Format_Text = "LIST"
  38. CONST L_Const_Csv_Format_Text = "CSV"
  39. ' the text displayed in columns when no output is obtained for display
  40. CONST L_TextNa_Text = "N/A"
  41. CONST L_TextNone_Text = "None"
  42. ' the following texts are used while parsing the command-line arguments
  43. ' (passed as input to the function component.getArguments)
  44. CONST L_MachineName_Text = "Server Name"
  45. CONST L_UserName_Text = "User Name"
  46. CONST L_UserPassword_Text = "User Password"
  47. CONST L_Format_Text = "Format"
  48. CONST L_Range_Text = "Range"
  49. CONST L_Filter_Text = "Filter"
  50. CONST L_Log_Text = "Logname"
  51. ' the column headers used in the output display
  52. CONST L_ColHeaderType_Text = "Type"
  53. CONST L_ColHeaderDateTime_Text = "Date Time"
  54. CONST L_ColHeaderSource_Text = "Source"
  55. CONST L_ColHeaderCategory_Text = "Category"
  56. CONST L_ColHeaderEventcode_Text = "Event"
  57. CONST L_ColHeaderUser_Text = "User"
  58. CONST L_ColHeaderComputerName_Text = "ComputerName"
  59. CONST L_ColHeaderDesription_Text = "Description"
  60. ' variable use to concatenate the Localization Strings.
  61. ' Error Messages
  62. Dim UseCscriptErrorMessage
  63. Dim InvalidParameterErrorMessage
  64. Dim InvalidFormatErrorMessage
  65. Dim InvalidCredentialsForServerErrorMessage
  66. Dim InvalidCredentialsForUserErrorMessage
  67. Dim InvalidSyntaxErrorMessage
  68. Dim InvalidInputErrorMessage
  69. Dim InvalidORSyntaxInFilterErrorMessage
  70. Dim InvalidSyntaxMoreNoRepeatedErrorMessage
  71. UseCscriptErrorMessage = L_UseCscript1_ErrorMessage & vbCRLF & _
  72. L_UseCscript2_ErrorMessage & vbCRLF & vbCRLF & _
  73. L_UseCscript3_ErrorMessage & vbCRLF & _
  74. L_UseCscript4_ErrorMessage & vbCRLF & vbCRLF & _
  75. L_UseCscript5_ErrorMessage
  76. CONST L_HelpSyntax1_Message = "Type ""%1 /?"" for usage."
  77. CONST L_HelpSyntax2_Message = "Type ""%2 /?"" for usage."
  78. CONST L_InvalidParameter1_ErrorMessage = "ERROR: Invalid Argument/Option - '%1'."
  79. InvalidParameterErrorMessage = L_InvalidParameter1_ErrorMessage & vbCRLF & L_HelpSyntax2_Message
  80. CONST L_InvalidFormat1_ErrorMessage = "ERROR: Invalid 'FORMAT' '%1' specified."
  81. InvalidFormatErrorMessage = L_InvalidFormat1_ErrorMessage & vbCRLF & L_HelpSyntax2_Message
  82. CONST L_InvalidRange_ErrorMessage = "ERROR: Invalid 'RANGE' '%1' specified."
  83. CONST L_Invalid_ErrorMessage = "ERROR: Invalid '%1'."
  84. CONST L_InvalidType_ErrorMessage = "ERROR: Invalid 'TYPE' '%1' specified for the 'FILTER' '%2'."
  85. CONST L_InvalidUser_ErrorMessage = "ERROR: Invalid 'USER' '%1' specified for the 'FILTER '%2'."
  86. CONST L_InvalidId_ErrorMessage = "ERROR: Invalid 'ID' '%1' specified for the 'FILTER' '%2'."
  87. CONST L_InvalidFilter_ErrorMessage = "ERROR: Invalid 'FILTER' '%1' specified for the 'FILTER' '%2'."
  88. CONST L_InvalidFilterFormat_ErrorMessage = "ERROR: The FILTER '%1' is not in the required format."
  89. CONST L_InvalidFilterOperation_ErrorMessage = "ERROR: Invalid FILTER operator '%1' specified for the filter '%2'."
  90. CONST L_InvalidCredentialsForServer1_ErrorMessage = "ERROR: Invalid Syntax. /U can be specified only when /S is specified."
  91. InvalidCredentialsForServerErrorMessage = L_InvalidCredentialsForServer1_ErrorMessage & vbCRLF & L_HelpSyntax1_Message
  92. CONST L_InvalidCredentialsForUser1_ErrorMessage = "ERROR: Invalid Syntax. /P can be specified only when /U is specified."
  93. InvalidCredentialsForUserErrorMessage = L_InvalidCredentialsForUser1_ErrorMessage & vbCRLF & L_HelpSyntax1_Message
  94. CONST L_InvalidOperator_ErrorMessage = "ERROR: Invalid operator specified for the range of dates in the 'DATETIME' filter."
  95. CONST L_InvalidDateTimeFormat_ErrorMessage = "ERROR: Invalid 'DATETIME' format specified. Format:mm/dd/yy(yyyy),hh:mm:ssAM(/PM)"
  96. CONST L_ExecuteQuery_ErrorMessage = "ERROR: Unable to execute the query for the '%1' log."
  97. CONST L_LogDoesNotExist_ErrorMessage = "ERROR: The log file '%1' does not exist."
  98. CONST L_InstancesFailed_ErrorMessage = "ERROR: Unable to get the log details from the system."
  99. CONST L_InvalidSyntax1_ErrorMessage = "ERROR: Invalid Syntax."
  100. InvalidSyntaxErrorMessage = L_InvalidSyntax1_ErrorMessage & vbCRLF & L_HelpSyntax1_Message
  101. CONST L_InvalidInput1_ErrorMessage = "ERROR: Invalid input. Please check the input Values."
  102. InvalidInputErrorMessage = L_InvalidInput1_ErrorMessage & vbCRLF & L_HelpSyntax1_Message
  103. CONST L_ObjCreationFail_ErrorMessage = "ERROR: Unexpected Error , Query failed. "
  104. CONST L_InfoUnableToInclude_ErrorMessage = "ERROR: Unable to include the common module""CmdLib.Wsc""."
  105. CONST L_NoHeaderaNotApplicable_ErrorMessage = "ERROR: /NH option is allowed only for ""TABLE"" and ""CSV"" formats."
  106. CONST L_InValidServerName_ErrorMessage = "ERROR: Invalid Syntax. System name cannot be empty."
  107. CONST L_InValidUserName_ErrorMessage = "ERROR: Invalid Syntax. User name cannot be empty. "
  108. CONST L_InvalidORSyntaxInFilter1_ErrorMessage = "ERROR: Invalid 'OR' operation is specified for the filter."
  109. CONST L_InvalidORSyntaxInFilter2_ErrorMessage = "'OR' operation valid only for filters TYPE and ID."
  110. InvalidORSyntaxInFilterErrorMessage = L_InvalidORSyntaxInFilter1_ErrorMessage & vbCRLF & L_InvalidORSyntaxInFilter2_ErrorMessage
  111. CONST L_InvalidSyntaxMoreNoRepeated1_ErrorMessage = "ERROR: Invalid Syntax. '%1' option is not allowed more than 1 time(s)."
  112. InvalidSyntaxMoreNoRepeatedErrorMessage = L_InvalidSyntaxMoreNoRepeated1_ErrorMessage & vbCRLF & L_HelpSyntax2_Message
  113. ' Hints given in case of errors
  114. CONST L_HintCheckConnection_Message = "ERROR: Please check the system name, credentials and WBEM Core."
  115. ' Informational messages
  116. CONST L_InfoNoRecordsInFilter_Message = "INFO: No records available for the '%1' log with the specified criteria."
  117. CONST L_InfoNoRecords_Message = "INFO: No records available for the '%1' log."
  118. CONST L_InfoNoLogsPresent_Message = "INFO: No logs are available in the system."
  119. CONST L_InfoDisplayLog_Message = "Listing the events in '%1' log of host '%2'"
  120. ' Cscript usage strings
  121. CONST L_UseCscript1_ErrorMessage = "This script should be executed from the Command Prompt using CSCRIPT.EXE."
  122. CONST L_UseCscript2_ErrorMessage = "For example: CSCRIPT EVENTQUERY.vbs <arguments>"
  123. CONST L_UseCscript3_ErrorMessage = "To set CScript as the default application to run .vbs files run the following"
  124. CONST L_UseCscript4_ErrorMessage = " CSCRIPT //H:CSCRIPT //S"
  125. CONST L_UseCscript5_ErrorMessage = "You can then run ""EVENTQUERY.vbs <arguments>"" without preceding the script with CSCRIPT."
  126. ' Contents for showing help for Usage
  127. CONST L_ShowUsageLine00_Text = "No logs are available on this system for query."
  128. CONST L_ShowUsageLine01_Text = "EVENTQUERY.vbs [/S system [/U username [/P password]]] [/FI filter]"
  129. CONST L_ShowUsageLine02_Text = " [/FO format] [/R range] [/NH] [/V] [/L logname | *]"
  130. CONST L_ShowUsageLine03_Text = "Description:"
  131. CONST L_ShowUsageLine04_Text = " The EVENTQUERY.vbs script enables an administrator to list"
  132. CONST L_ShowUsageLine05_Text = " the events and event properties from one or more event logs."
  133. CONST L_ShowUsageLine06_Text = "Parameter List:"
  134. CONST L_ShowUsageLine07_Text = " /S system Specifies the remote system to connect to."
  135. CONST L_ShowUsageLine08_Text = " /U [domain\]user Specifies the user context under which the"
  136. CONST L_ShowUsageLine09_Text = " command should execute."
  137. CONST L_ShowUsageLine10_Text = " /P password Specifies the password for the given"
  138. CONST L_ShowUsageLine11_Text = " user context."
  139. CONST L_ShowUsageLine12_Text = " /V Specifies that the detailed information"
  140. CONST L_ShowUsageLine13_Text = " should be displayed in the output."
  141. CONST L_ShowUsageLine14_Text = " /FI filter Specifies the types of events to"
  142. CONST L_ShowUsageLine15_Text = " filter in or out of the query."
  143. CONST L_ShowUsageLine16_Text = " /FO format Specifies the format in which the output"
  144. CONST L_ShowUsageLine17_Text = " is to be displayed."
  145. CONST L_ShowUsageLine18_Text = " Valid formats are ""TABLE"", ""LIST"", ""CSV""."
  146. CONST L_ShowUsageLine19_Text = " /R range Specifies the range of events to list."
  147. CONST L_ShowUsageLine20_Text = " Valid Values are:"
  148. CONST L_ShowUsageLine21_Text = " 'N' - Lists 'N' most recent events."
  149. CONST L_ShowUsageLine22_Text = " '-N' - Lists 'N' oldest events."
  150. CONST L_ShowUsageLine23_Text = " 'N1-N2' - Lists the events N1 to N2."
  151. CONST L_ShowUsageLine24_Text = " /NH Specifies that the ""Column Header"" should"
  152. CONST L_ShowUsageLine25_Text = " not be displayed in the output."
  153. CONST L_ShowUsageLine26_Text = " Valid only for ""TABLE"" and ""CSV"" formats."
  154. CONST L_ShowUsageLine27_Text = " /L logname Specifies the log(s) to query."
  155. CONST L_ShowUsageLine28_Text = " /? Displays this help/usage."
  156. CONST L_ShowUsageLine29_Text = " Valid Filters Operators allowed Valid Values"
  157. CONST L_ShowUsageLine30_Text = " ------------- ------------------ ------------"
  158. CONST L_ShowUsageLine31_Text = " DATETIME eq,ne,ge,le,gt,lt mm/dd/yy(yyyy),hh:mm:ssAM(/PM)"
  159. CONST L_ShowUsageLine32_Text = " TYPE eq,ne ERROR, INFORMATION, WARNING,"
  160. CONST L_ShowUsageLine33_Text = " SUCCESSAUDIT, FAILUREAUDIT"
  161. CONST L_ShowUsageLine34_Text = " ID eq,ne,ge,le,gt,lt non-negative integer"
  162. CONST L_ShowUsageLine35_Text = " USER eq,ne string"
  163. CONST L_ShowUsageLine36_Text = " COMPUTER eq,ne string"
  164. CONST L_ShowUsageLine37_Text = " SOURCE eq,ne string"
  165. CONST L_ShowUsageLine38_Text = " CATEGORY eq,ne string"
  166. CONST L_ShowUsageLine39_Text = "NOTE: Filter ""DATETIME"" can be specified as ""FromDate-ToDate"""
  167. CONST L_ShowUsageLine40_Text = " Only ""eq"" operator can be used for this format."
  168. CONST L_ShowUsageLine41_Text = "Examples:"
  169. CONST L_ShowUsageLine42_Text = " EVENTQUERY.vbs "
  170. CONST L_ShowUsageLine43_Text = " EVENTQUERY.vbs /L system "
  171. CONST L_ShowUsageLine44_Text = " EVENTQUERY.vbs /S system /U user /P password /V /L *"
  172. CONST L_ShowUsageLine45_Text = " EVENTQUERY.vbs /R 10 /L Application /NH"
  173. CONST L_ShowUsageLine46_Text = " EVENTQUERY.vbs /R -10 /FO LIST /L Security"
  174. CONST L_ShowUsageLine47_Text = " EVENTQUERY.vbs /R 5-10 /L ""DNS Server"""
  175. CONST L_ShowUsageLine48_Text = " EVENTQUERY.vbs /FI ""Type eq Error"" /L Application"
  176. CONST L_ShowUsageLine49_Text = " EVENTQUERY.vbs /L Application"
  177. CONST L_ShowUsageLine50_Text = " /FI ""Datetime eq 06/25/00,03:15:00AM-06/25/00,03:15:00PM"""
  178. CONST L_ShowUsageLine51_Text = " EVENTQUERY.vbs /FI ""Datetime gt 08/03/00,06:20:00PM"" "
  179. CONST L_ShowUsageLine52_Text = " /FI ""Id gt 700"" /FI ""Type eq warning"" /L System"
  180. CONST L_ShowUsageLine53_Text = " EVENTQUERY.vbs /FI ""Type eq error OR Id gt 1000 """
  181. '-------------------------------------------------------------------------
  182. ' END of localization content
  183. '-------------------------------------------------------------------------
  184. ' Define constants
  185. CONST CONST_ERROR = 0
  186. CONST CONST_CSCRIPT = 2
  187. CONST CONST_SHOW_USAGE = 3
  188. CONST CONST_PROCEED = 4
  189. CONST CONST_ERROR_USAGE = 5
  190. CONST CONST_NO_MATCHES_FOUND = 0
  191. ' Define the Exit Values
  192. CONST EXIT_SUCCESS = 0
  193. CONST EXIT_UNEXPECTED = 255
  194. CONST EXIT_INVALID_INPUT = 254
  195. CONST EXIT_METHOD_FAIL = 250
  196. CONST EXIT_INVALID_PARAM = 999
  197. CONST EXIT_INVALID_PARAM_DEFAULT_OPTION_REPEATED = 777
  198. ' Define default values
  199. CONST CONST_ARRAYBOUND_NUMBER = 10
  200. CONST CONST_ID_NUMBER = 65535
  201. ' Define namespace and class names of wmi
  202. CONST CONST_NAMESPACE_CIMV2 = "root\cimv2"
  203. CONST CLASS_EVENTLOG_FILE = "Win32_NTEventlogFile"
  204. ' for blank line in help usage
  205. CONST EmptyLine_Text = " "
  206. ' Define the various strings used in the script
  207. '=============================================
  208. ' the valid options supported by the script
  209. CONST OPTION_SERVER = "s"
  210. CONST OPTION_USER = "u"
  211. CONST OPTION_PASSWORD = "p"
  212. CONST OPTION_FORMAT = "fo"
  213. CONST OPTION_RANGE = "r"
  214. CONST OPTION_NOHEADER = "nh"
  215. CONST OPTION_VERBOSE = "v"
  216. CONST OPTION_FILTER = "fi"
  217. CONST OPTION_HELP = "?"
  218. CONST OPTION_LOGNAME = "l"
  219. ' the property names on which the user given filters are applied
  220. CONST FLD_FILTER_DATETIME = "TimeGenerated"
  221. CONST FLD_FILTER_TYPE = "Type"
  222. CONST FLD_FILTER_USER = "User"
  223. CONST FLD_FILTER_COMPUTER = "ComputerName"
  224. CONST FLD_FILTER_SOURCE = "SourceName"
  225. CONST FLD_FILTER_CATEGORY = "CategoryString"
  226. CONST FLD_FILTER_ID = "EventCode"
  227. CONST FLD_FILTER_EVENTTYPE = "EventType"
  228. ' Define matching patterns used in validations
  229. CONST PATTERNFORMAT = "^(table|list|csv)$"
  230. CONST PATTERNTYPE = "^(ERROR|INFORMATION|WARNING|SUCCESSAUDIT|FAILUREAUDIT)$"
  231. ' Property values on which the user is given for the filter TYPE is applied
  232. CONST PATTERNTYPE_ERROR = "ERROR"
  233. CONST PATTERNTYPE_WARNING = "WARNING"
  234. CONST PATTERNTYPE_INFORMATION = "INFORMATION"
  235. CONST PATTERNTYPE_SUCCESSAUDIT = "SUCCESSAUDIT"
  236. CONST PATTERNTYPE_FAILUREAUDIT = "FAILUREAUDIT"
  237. CONST FLDFILTERTYPE_SUCCESSAUDIT = "audit success"
  238. CONST FLDFILTERTYPE_FAILUREAUDIT = "audit failure"
  239. ' Define EventType
  240. CONST EVENTTYPE_ERROR = "1"
  241. CONST EVENTTYPE_WARNING = "2"
  242. CONST EVENTTYPE_INFORMATION = "3"
  243. CONST EVENTTYPE_SUCCESSAUDIT = "4"
  244. CONST EVENTTYPE_FAILUREAUDIT = "5"
  245. ' the operator symbols
  246. CONST SYMBOL_OPERATOR_EQ = "="
  247. CONST SYMBOL_OPERATOR_NE = "<>"
  248. CONST SYMBOL_OPERATOR_GE = ">="
  249. CONST SYMBOL_OPERATOR_LE = "<="
  250. CONST SYMBOL_OPERATOR_GT = ">"
  251. CONST SYMBOL_OPERATOR_LT = "<"
  252. ' Define matching patterns used in validations
  253. CONST PATTERN_RANGE = "^\d*-?\d+$"
  254. CONST PATTERN_FILTER = "^([a-z]+)([\s]+)([a-z]+)([\s]+)([\w+]|[\W+]|\\)"
  255. CONST PATTERN_DATETIME = "^\d{1,2}\/\d{1,2}\/\d{2,4},\d{1,2}:\d{1,2}:\d{1,2}(A|P)M$"
  256. CONST PATTERN_INVALID_USER = "\|\[|\]|\:|\||\<|\>|\+|\=|\;|\,|\?|\*"
  257. CONST PATTERN_ID = "^(\d+)$"
  258. CONST PATTERN_DATETIME_RANGE = "^\d{1,2}\/\d{1,2}\/\d{2,4},\d{1,2}:\d{1,2}:\d{1,2}(A|P)M\-\d{1,2}\/\d{1,2}\/\d{2,4},\d{1,2}:\d{1,2}:\d{1,2}(A|P)M$"
  259. ' Define UNC format for server name
  260. CONST UNC_Format_Servername = "\\"
  261. ' Define const for filter separation when OR is specified in filter
  262. CONST L_OperatorOR_Text = " OR "
  263. ' Variable to trap local if already connection in wmiconnect function
  264. Dim blnLocalConnection
  265. blnLocalConnection = False 'defalut value
  266. ' to include the common module
  267. Dim component ' object to store common module
  268. Set component = CreateObject( "Microsoft.CmdLib" )
  269. If Err.Number Then
  270. WScript.Echo(L_InfoUnableToInclude_ErrorMessage)
  271. WScript.Quit(EXIT_METHOD_FAIL)
  272. End If
  273. ' referring the script host to common module
  274. Set component.ScriptingHost = WScript.Application
  275. ' Check whether the script is run using CScript
  276. If CInt( component.checkScript() ) <> CONST_CSCRIPT Then
  277. WScript.Echo (UseCscriptErrorMessage)
  278. WScript.Quit(EXIT_UNEXPECTED)
  279. End If
  280. ' Calling the Main function
  281. Call VBMain()
  282. ' end of the Main
  283. Wscript.Quit(EXIT_SUCCESS)
  284. '********************************************************************
  285. '* Sub: VBMain
  286. '*
  287. '* Purpose: This is main function to starts execution
  288. '*
  289. '*
  290. '* Input/ Output: None
  291. '********************************************************************
  292. Sub VBMain()
  293. ON ERROR RESUME NEXT
  294. Err.clear
  295. ' Declare variables
  296. Dim intOpCode ' to check the operation asked for, Eg:Help etc
  297. Dim strMachine ' the machine to query the events from
  298. Dim strUserName ' the user name to use to query the machine
  299. Dim strPassword ' the password for the user to query the machine
  300. Dim strFormat ' format of display, default is table
  301. Dim strRange ' to store the range of records specified
  302. Dim blnNoHeader ' flag to store if header is not required
  303. Dim blnVerboseDisplay ' flag to verify if verbose display is needed
  304. ReDim arrFilters(5) ' to store all the given filters
  305. Dim objLogs ' a object to store all the given logfles
  306. ' Initialize variables
  307. intOpCode = 0
  308. strFormat = L_ConstDefaultFormat_Text
  309. strRange = ""
  310. blnNoHeader = FALSE
  311. blnVerboseDisplay = FALSE
  312. Set objLogs = CreateObject("Scripting.Dictionary")
  313. If Err.Number Then
  314. WScript.Echo (L_ObjCreationFail_ErrorMessage)
  315. WScript.Quit(EXIT_METHOD_FAIL)
  316. End If
  317. ' setting Dictionary object compare mode to VBBinaryCompare
  318. objLogs.CompareMode = VBBinaryCompare
  319. ' Parse the command line
  320. intOpCode = intParseCmdLine(strMachine, _
  321. strUserName, _
  322. strPassword, _
  323. arrFilters, _
  324. strFormat, _
  325. strRange, _
  326. blnVerboseDisplay, _
  327. blnNoHeader, _
  328. objLogs)
  329. If Err.number then
  330. ' error in parsing the Command line
  331. component.vbPrintf InvalidInputErrorMessage ,Array(Ucase(Wscript.ScriptName))
  332. WScript.Quit(EXIT_UNEXPECTED)
  333. End If
  334. ' check the operation specified by the user
  335. Select Case intOpCode
  336. Case CONST_SHOW_USAGE
  337. ' help asked for
  338. Call ShowUsage()
  339. Case CONST_PROCEED
  340. Call ShowEvents(strMachine, strUserName, strPassword, _
  341. arrFilters, strFormat, strRange, _
  342. blnVerboseDisplay, blnNoHeader, objLogs)
  343. ' completed successfully
  344. WScript.Quit(EXIT_SUCCESS)
  345. Case CONST_ERROR
  346. ' print common help message.
  347. component.vbPrintf L_HelpSyntax1_Message, Array(Ucase(Wscript.ScriptName))
  348. Wscript.Quit(EXIT_INVALID_INPUT)
  349. Case CONST_ERROR_USAGE
  350. ' help is asked help with some other parameters
  351. component.vbPrintf InvalidSyntaxErrorMessage, Array(Ucase(Wscript.ScriptName))
  352. WScript.Quit(EXIT_INVALID_INPUT)
  353. Case EXIT_INVALID_PARAM_DEFAULT_OPTION_REPEATED
  354. 'More no of times input values specified.message is captured at parser level so exit only with code.
  355. Wscript.Quit(EXIT_INVALID_PARAM)
  356. Case Else
  357. 'Invalid input values specified.
  358. component.vbPrintf InvalidSyntaxErrorMessage, Array(Ucase(Wscript.ScriptName))
  359. Wscript.Quit(EXIT_INVALID_PARAM)
  360. End Select
  361. End Sub
  362. '*************************** End of Main **************************
  363. '********************************************************************
  364. '* Function: intParseCmdLine
  365. '*
  366. '* Purpose: Parses the command line arguments to the variables
  367. '*
  368. '* Input:
  369. '* [out] strMachine machine to query events from
  370. '* [out] strUserName user name to connect to the machine
  371. '* [out] strPassword password for the user
  372. '* [out] arrFilters the array containing the filters
  373. '* [out] strFormat the display format
  374. '* [out] strRange the range of records required
  375. '* [out] blnVerboseDisplay flag to verify if verbose display is needed
  376. '* [out] blnNoHeader flag to verify if noheader display is needed
  377. '* [out] objLogs to store all the given logfles
  378. '* Output: Returns CONST_PROCEED, CONST_SHOW_USAGE or CONST_ERROR
  379. '* Displays error message and quits if invalid option is asked
  380. '*
  381. '********************************************************************
  382. Private Function intParseCmdLine( ByRef strMachine, _
  383. ByRef strUserName, _
  384. ByRef strPassword, _
  385. ByRef arrFilters, _
  386. ByRef strFormat, _
  387. ByRef strRange, _
  388. ByRef blnVerboseDisplay, _
  389. ByRef blnNoHeader,_
  390. ByRef objLogs)
  391. ON ERROR RESUME NEXT
  392. Err.Clear
  393. Dim strUserGivenArg ' to temporarily store the user given arguments to script
  394. Dim strTemp ' to store temporary values
  395. Dim intArgIter ' to count the number of arguments given by user
  396. Dim intArgLogType ' to count number of log files specified - Used in ReDim
  397. Dim intFilterCount ' to count number of filters specified - Used in ReDim
  398. Dim blnHelp ' to check if already Help is specified
  399. Dim blnFormat ' to check if already Format is specified
  400. Dim blnRange ' to check if already Range is specified
  401. Dim blnServer ' to check if already Server is specified
  402. Dim blnPassword ' to check if already Password is specified
  403. Dim blnUser ' to check if already User is specified
  404. strUserGivenArg = ""
  405. intArgLogType = 0
  406. intFilterCount = 0
  407. intArgIter = 0
  408. 'default values
  409. blnHelp = False
  410. blnPassword = False
  411. blnUser = False
  412. blnServer = False
  413. blnFormat = False
  414. ' Retrieve the command line and set appropriate variables
  415. Do While intArgIter <= Wscript.arguments.Count - 1
  416. strUserGivenArg = Wscript.arguments.Item(intArgIter)
  417. IF Left( strUserGivenArg,1) = "/" OR Left( strUserGivenArg,1) = "-" Then
  418. strUserGivenArg = Right( strUserGivenArg,Len(strUserGivenArg) -1 )
  419. Select Case LCase(strUserGivenArg)
  420. Case LCase(OPTION_SERVER)
  421. 'If more than 1 time(s) is spcecified
  422. If blnServer =True Then
  423. component.vbPrintf InvalidSyntaxMoreNoRepeatedErrorMessage, Array(Wscript.arguments.Item(intArgIter), Ucase(Wscript.ScriptName))
  424. intParseCmdLine = EXIT_INVALID_PARAM_DEFAULT_OPTION_REPEATED
  425. Exit Function
  426. End If
  427. If Not component.getArguments(L_MachineName_Text, strMachine, intArgIter, FALSE) Then
  428. intParseCmdLine = CONST_ERROR
  429. Exit Function
  430. End If
  431. blnServer =True
  432. intArgIter = intArgIter + 1
  433. Case LCase(OPTION_USER)
  434. 'If more than 1 time(s) is spcecified
  435. If blnUser =True Then
  436. component.vbPrintf InvalidSyntaxMoreNoRepeatedErrorMessage, Array(Wscript.arguments.Item(intArgIter), Ucase(Wscript.ScriptName))
  437. intParseCmdLine = EXIT_INVALID_PARAM_DEFAULT_OPTION_REPEATED
  438. Exit Function
  439. End If
  440. If Not component.getArguments(L_UserName_Text, strUserName, intArgIter, FALSE) Then
  441. intParseCmdLine = CONST_ERROR
  442. Exit Function
  443. End If
  444. blnUser =True
  445. intArgIter = intArgIter + 1
  446. Case LCase(OPTION_PASSWORD)
  447. 'If more than 1 time(s) is spcecified
  448. If blnPassword =True Then
  449. component.vbPrintf InvalidSyntaxMoreNoRepeatedErrorMessage, Array(Wscript.arguments.Item(intArgIter), Ucase(Wscript.ScriptName))
  450. intParseCmdLine = EXIT_INVALID_PARAM_DEFAULT_OPTION_REPEATED
  451. Exit Function
  452. End If
  453. If Not component.getArguments(L_UserPassword_Text, strPassword, intArgIter, FALSE) Then
  454. intParseCmdLine = CONST_ERROR
  455. Exit Function
  456. End If
  457. blnPassword =True
  458. intArgIter = intArgIter + 1
  459. Case LCase(OPTION_FORMAT)
  460. 'If more than 1 time(s) is spcecified
  461. If blnFormat =True Then
  462. component.vbPrintf InvalidSyntaxMoreNoRepeatedErrorMessage, Array(Wscript.arguments.Item(intArgIter), Ucase(Wscript.ScriptName))
  463. intParseCmdLine = EXIT_INVALID_PARAM_DEFAULT_OPTION_REPEATED
  464. Exit Function
  465. End If
  466. If Not component.getArguments(L_Format_Text,strFormat, intArgIter, FALSE) Then
  467. intParseCmdLine = CONST_ERROR
  468. Exit Function
  469. End If
  470. blnFormat =True
  471. intArgIter = intArgIter + 1
  472. Case LCase(OPTION_RANGE)
  473. 'If more than 1 time(s) is spcecified
  474. If blnRange =True Then
  475. component.vbPrintf InvalidSyntaxMoreNoRepeatedErrorMessage, Array(Wscript.arguments.Item(intArgIter), Ucase(Wscript.ScriptName))
  476. intParseCmdLine = EXIT_INVALID_PARAM_DEFAULT_OPTION_REPEATED
  477. Exit Function
  478. End If
  479. If Not component.getArguments(L_Range_Text,strRange, intArgIter,TRUE) Then
  480. intParseCmdLine = CONST_ERROR
  481. Exit Function
  482. End If
  483. blnRange =True
  484. intArgIter = intArgIter + 1
  485. Case LCase(OPTION_NOHEADER)
  486. 'If more than 1 time(s) is spcecified
  487. If blnNoHeader =True Then
  488. component.vbPrintf InvalidSyntaxMoreNoRepeatedErrorMessage, Array(Wscript.arguments.Item(intArgIter), Ucase(Wscript.ScriptName))
  489. intParseCmdLine = EXIT_INVALID_PARAM_DEFAULT_OPTION_REPEATED
  490. Exit Function
  491. End If
  492. blnNoHeader = TRUE
  493. intArgIter = intArgIter + 1
  494. Case LCase(OPTION_VERBOSE)
  495. 'If more than 1 time(s) is spcecified
  496. If blnVerboseDisplay =True Then
  497. component.vbPrintf InvalidSyntaxMoreNoRepeatedErrorMessage, Array(Wscript.arguments.Item(intArgIter), Ucase(Wscript.ScriptName))
  498. intParseCmdLine = EXIT_INVALID_PARAM_DEFAULT_OPTION_REPEATED
  499. Exit Function
  500. End If
  501. blnVerboseDisplay = TRUE
  502. intArgIter = intArgIter + 1
  503. Case LCase(OPTION_FILTER)
  504. If Not component.getArguments(L_Filter_Text, strTemp, intArgIter, FALSE) Then
  505. intParseCmdLine = CONST_ERROR
  506. Exit Function
  507. End If
  508. arrFilters(intFilterCount) = strTemp
  509. intFilterCount = intFilterCount + 1
  510. intArgIter = intArgIter + 1
  511. If ((intFilterCount MOD 5) = 0) Then
  512. ReDim PRESERVE arrFilters(intFilterCount + 5)
  513. End If
  514. Case LCase(OPTION_HELP)
  515. If blnHelp =True then
  516. intParseCmdLine = EXIT_INVALID_PARAM
  517. Exit Function
  518. End If
  519. blnHelp =True
  520. intParseCmdLine = CONST_SHOW_USAGE
  521. intArgIter = intArgIter + 1
  522. Case LCase(OPTION_LOGNAME)
  523. If Not component.getArguments(L_Log_Text, strTemp, intArgIter, FALSE) Then
  524. intParseCmdLine = CONST_ERROR
  525. Exit Function
  526. Else
  527. If NOT objLogs.Exists(LCase(strTemp)) Then
  528. objLogs.Add LCase(strTemp), -1
  529. End If
  530. intArgIter = intArgIter + 1
  531. End if
  532. Case Else
  533. ' invalid switch specified
  534. component.vbPrintf InvalidParameterErrorMessage, Array(Wscript.arguments.Item(intArgIter),Ucase(Wscript.ScriptName))
  535. Wscript.Quit(EXIT_INVALID_INPUT)
  536. End Select
  537. Else
  538. ' invalid argument specified
  539. component.vbPrintf InvalidParameterErrorMessage, Array(Wscript.arguments.Item(intArgIter),Ucase(Wscript.ScriptName))
  540. Wscript.Quit(EXIT_INVALID_INPUT)
  541. End IF
  542. Loop '** intArgIter <= Wscript.arguments.Count - 1
  543. ' preserving the array with current dimension
  544. ReDim PRESERVE arrFilters(intFilterCount-1)
  545. ' if no logs specified for query
  546. If (ObjLogs.Count = 0 ) Then
  547. ObjLogs.Add "*", -1
  548. End If
  549. ' check for invalid usage of help
  550. If blnHelp and intArgIter > 1 Then
  551. intParseCmdLine = CONST_ERROR_USAGE
  552. Exit Function
  553. End If
  554. 'check with default case : no arguments specified
  555. If IsEmpty(intParseCmdLine) Then
  556. intParseCmdLine = CONST_PROCEED
  557. End If
  558. End Function
  559. '********************************************************************
  560. '* Function: ValidateArguments
  561. '*
  562. '* Purpose: Validates the command line arguments given by the user
  563. '*
  564. '* Input:
  565. '* [in] strMachine machine to query events from
  566. '* [in] strUserName user name to connect to the machine
  567. '* [in] strPassword password for the user
  568. '* [in] strFormat the display format
  569. '* [in] strRange the range of records required
  570. '* [in] blnNoHeader flag to verify if noheader display is needed
  571. '* [out] arrFilters the array containing the filters
  572. '*
  573. '* Output: Returns true if all valid else displays error message and quits
  574. '* Gets the password from the user if not specified along with User.
  575. '*
  576. '********************************************************************
  577. Private Function ValidateArguments (ByVal strMachine, _
  578. ByVal strUserName, _
  579. ByVal strPassword, _
  580. ByRef arrFilters, _
  581. ByVal strFormat, _
  582. ByVal strRange,_
  583. ByVal blnNoHeader)
  584. ON ERROR RESUME NEXT
  585. Err.Clear
  586. Dim arrTemp ' to store temporary array values
  587. ' Check if invalid Server name is given
  588. If NOT ISEMPTY(strMachine) THEN
  589. If Trim(strMachine) = vbNullString Then
  590. WScript.Echo (L_InValidServerName_ErrorMessage)
  591. WScript.Quit(EXIT_INVALID_INPUT)
  592. End If
  593. End If
  594. 'Check if invalid User name is given
  595. If NOT ISEMPTY(strUserName) THEN
  596. If Trim(strUserName) = vbNullString Then
  597. WScript.Echo (L_InValidUserName_ErrorMessage )
  598. WScript.Quit(EXIT_INVALID_INPUT)
  599. End If
  600. End If
  601. ' ERROR if user is given without machine OR
  602. ' password is given without user
  603. If ((strUserName <> VBEmpty) AND (strMachine = VBEmpty)) Then
  604. component.vbPrintf InvalidCredentialsForServerErrorMessage, Array(Ucase(Wscript.ScriptName))
  605. WScript.Quit(EXIT_INVALID_INPUT)
  606. ElseIf ((strPassword <> VBEmpty) AND (strUserName = VBEmpty))Then
  607. component.vbPrintf InvalidCredentialsForUserErrorMessage, Array(Ucase(Wscript.ScriptName))
  608. WScript.Quit(EXIT_INVALID_INPUT)
  609. End If
  610. ' only table, list and csv display formats allowed
  611. ' PATTERNFORMAT '"^(table|list|csv)$"
  612. If CInt(component.matchPattern(PATTERNFORMAT,strFormat)) = CONST_NO_MATCHES_FOUND Then
  613. component.vbPrintf InvalidFormatErrormessage, Array(strFormat ,Ucase(Wscript.ScriptName))
  614. WScript.Quit(EXIT_INVALID_INPUT)
  615. End If
  616. ' check : -n header is specified for format of 'LIST' option
  617. If blnNoHeader =True and Lcase(strFormat) = Lcase(L_Const_List_Format_Text) then
  618. WScript.Echo (L_NoHeaderaNotApplicable_ErrorMessage)
  619. WScript.Quit(EXIT_INVALID_INPUT)
  620. End If
  621. If Len(Trim(strRange)) > 0 Then
  622. ' range is specified, valid formats are N, -N or N1-N2
  623. ' PATTERN_RANGE '"^(\d+|\-\d+|\d+\-\d+)$"
  624. If CInt(component.matchPattern(PATTERN_RANGE, strRange)) = CONST_NO_MATCHES_FOUND Then
  625. component.vbPrintf L_InvalidRange_ErrorMessage, Array(strRange)
  626. WScript.Quit(EXIT_INVALID_INPUT)
  627. Else
  628. strRange = CLng(Abs(strRange))
  629. 'this err an be trappped when N1-N2 option is given
  630. If Err.Number Then
  631. arrTemp = split(strRange, "-", 2, VBBinaryCompare)
  632. If CLng(arrTemp(0)) => CLng(arrTemp(1)) Then
  633. ' invalid range
  634. component.vbPrintf L_InvalidRange_ErrorMessage, Array(strRange)
  635. WScript.Quit(EXIT_INVALID_INPUT)
  636. End If
  637. Err.Clear 'if no invalid range N1-N2 clear the error
  638. Else
  639. If Abs(strRange) = 0 Then
  640. component.vbPrintf L_InvalidRange_ErrorMessage, Array(strRange)
  641. WScript.Quit(EXIT_INVALID_INPUT)
  642. End If
  643. End If
  644. End If
  645. End If
  646. ValidateArguments = TRUE
  647. End Function
  648. '********************************************************************
  649. '* Function: ValidateFilters
  650. '*
  651. '* Purpose: Validates the filters given by the user.
  652. '*
  653. '* Input: [in] Objservice the service object
  654. '* Input: [out] arrFilters the array containing the filters
  655. '*
  656. '* Output: If filter is invalid, displays error message and quits
  657. '* If valid, filter is prepared for the query and returns true
  658. '*
  659. '********************************************************************
  660. Private Function ValidateFilters(ByRef arrFilters ,ByVal ObjService)
  661. ON ERROR RESUME NEXT
  662. Err.Clear
  663. Dim j ' to use in the loop
  664. Dim strFilter ' to store the user given filter (Eg:"Type eq Error")
  665. Dim arrTempProp ' to store the temporary array filterproperty
  666. Dim arrTempOperAndVal ' to store the temporary array filteroperator and filtervalue
  667. Dim strTemp ' to store temporary values
  668. Dim arrTemp ' to store temporary values of datetime when Range is given (Date1-Date2)
  669. Dim strFilterProperty ' the filter criteria that is specified (Eg:Type, ID)
  670. Dim strFilterOperation ' the operation specified (Eg: eq, gt)
  671. Dim strFilterValue ' the filter value specified
  672. Dim objInstance ' to refer to the instances of the objEnumerator
  673. Dim objEnumerator ' to store the results of the query is executed
  674. Dim strTempQuery ' string to make query
  675. Dim strTimeZone ' to store the TimeZone of the Queried system
  676. Dim strSign ' to store "+|-" sign value of TimeZone
  677. ' validate each filter stored in the array
  678. For j = 0 to UBound(arrFilters)
  679. strFilter = arrFilters(j)
  680. 'check eigther "OR" is pesent inthe filter value
  681. 'Example : "type eq warning " OR " type eq error" [to support ORing in Filter Switch]
  682. 'Make a flag in this case "blnOR" present/not
  683. 'split it by "OR" SEND as No. of Array elements
  684. Dim blnOR 'boolean to refer 'OR' operation is specified
  685. Dim strArrFilter 'string to store array of filters if OR is specified
  686. blnOR=False 'Initialise to False
  687. If UBOUND(Split(LCase(strFilter),LCase(L_OperatorOR_Text)) ) > 0 Then
  688. 'setting the flag if " OR " specified in filter
  689. blnOR =TRUE
  690. 'split with "OR"
  691. strArrFilter = Split(LCase(strFilter),LCase(L_OperatorOR_Text))
  692. Else
  693. 'make single dimention array UBOUND = 0
  694. strArrFilter = Array(strFilter)
  695. End If
  696. Dim k ' to use in the loop
  697. Dim strTempFilter ' used to format Query string
  698. 'process the array for validatation
  699. 'UBOUND = 0 say normal filter specified
  700. For k = 0 to UBound(strArrFilter)
  701. If UBound(strArrFilter) > 0 then
  702. strFilter =strArrFilter(k)
  703. Else
  704. 'this is the first element allways
  705. strFilter =strArrFilter(0)
  706. End If
  707. ' check if 3 parameters are passed as input to filter
  708. ' PATTERN_FILTER "^([a-z]+)([\s]+)([a-z]+)([\s]+)(\w+)"
  709. strFilter = Trim( strFilter ) ' trim the value
  710. If CInt(component.matchPattern(PATTERN_FILTER, strFilter)) <= 0 Then
  711. component.vbPrintf L_InvalidFilterFormat_ErrorMessage, Array(strFilter)
  712. WScript.Quit(EXIT_INVALID_INPUT)
  713. End If
  714. ' This to eliminate any no.of blank Char(s) between three valid input values
  715. ' i.e..filter "property ---operation ----value"
  716. ' first SPLIT the space delimiter string into array size of 2.
  717. ' and get the property value
  718. arrTempProp = split(Trim(strFilter)," ",2,VBBinaryCompare)
  719. strFilterProperty = arrTempProp(0)
  720. ' now trim it and again SPLIT the second element of arrTempProp into an array of size 2.
  721. ' and get the operation and value
  722. arrTempOperAndVal = split(Trim(arrTempProp(1))," ",2,VBBinaryCompare)
  723. strFilterOperation = arrTempOperAndVal(0)
  724. strFilterValue = Ltrim(arrTempOperAndVal(1))
  725. If LCase(strFilterProperty) = LCase(L_UserFilterDateTime_Text) OR _
  726. LCase(strFilterProperty) = LCase(L_UserFilterId_Text) Then
  727. ' the following are valid operators
  728. If LCase(strFilterOperation) = LCase(L_OperatorEq_Text) OR _
  729. LCase(strFilterOperation) = LCase(L_OperatorNe_Text) OR _
  730. LCase(strFilterOperation) = LCase(L_OperatorGe_Text) OR _
  731. LCase(strFilterOperation) = LCase(L_OperatorLe_Text) OR _
  732. LCase(strFilterOperation) = LCase(L_OperatorGt_Text) OR _
  733. LCase(strFilterOperation) = LCase(L_OperatorLt_Text) Then
  734. strTemp = ReplaceOperators(strFilterOperation)
  735. strFilterOperation = strTemp
  736. Else
  737. component.vbPrintf L_InvalidFilterOperation_ErrorMessage, Array(strFilterOperation, strFilter)
  738. WScript.Quit(EXIT_INVALID_INPUT)
  739. End If
  740. ElseIf LCase(strFilterProperty) = LCase(L_UserFilterType_Text) OR _
  741. LCase(strFilterProperty) = LCase(L_UserFilterUser_Text) OR _
  742. LCase(strFilterProperty) = LCase(L_UserFilterComputer_Text) OR _
  743. LCase(strFilterProperty) = LCase(L_UserFilterSource_Text) OR _
  744. LCase(strFilterProperty) = LCase(L_UserFilterDateCategory_Text) Then
  745. ' for others, only these two operators are valid
  746. If LCase(strFilterOperation) = LCase(L_OperatorEq_Text) OR _
  747. LCase(strFilterOperation) = LCase(L_OperatorNe_Text) Then
  748. strTemp = ReplaceOperators(strFilterOperation)
  749. strFilterOperation = strTemp
  750. Else
  751. component.vbPrintf L_InvalidFilterOperation_ErrorMessage, _
  752. Array(strFilterOperation, strFilter)
  753. WScript.Quit(EXIT_INVALID_INPUT)
  754. End If
  755. Else
  756. component.vbPrintf L_InvalidFilterOperation_ErrorMessage, _
  757. Array(strFilterProperty, strFilter)
  758. WScript.Quit(EXIT_INVALID_INPUT)
  759. End If
  760. ' validate the filter asked for
  761. Select Case LCase(strFilterProperty)
  762. Case L_UserFilterDateTime_Text
  763. 'Checking " OR " is only supported property EQ "TYPE OR ID" only
  764. If blnOR = True then
  765. WScript.Echo InvalidORSyntaxInFilterErrorMessage
  766. WScript.Quit(EXIT_INVALID_INPUT)
  767. End If
  768. ' Here To find Time Zone of system from CLASS_TIMEZONE_FILE
  769. strTempQuery = "SELECT * FROM Win32_OperatingSystem "
  770. Set objEnumerator = objService.ExecQuery(strTempQuery,,0)
  771. ' getting the Time Zone
  772. For each objInstance in objEnumerator
  773. strTimeZone = objInstance.CurrentTimeZone
  774. Next
  775. 'here to format timeZome value as '+/-' UUU
  776. If Isnull(strTimeZone) or IsEmpty(strTimeZone)then
  777. strTimeZone =0
  778. End If
  779. 'default sign
  780. strSign ="+"
  781. IF strTimeZone < 0 THEN
  782. strSign ="-"
  783. End If
  784. If Len(strTimeZone) < 4 then
  785. If Len(strTimeZone) = 3 then
  786. If strTimeZone < 0 then
  787. strTimeZone = Replace(strTimeZone,"-","0")
  788. End If
  789. ElseIf Len(strTimeZone) = 2 then
  790. If strTimeZone < 0 then
  791. strTimeZone = Replace(strTimeZone,"-","00")
  792. Else
  793. strTimeZone = "0" & strTimeZone
  794. End If
  795. ElseIf Len(strTimeZone) = 1 then
  796. IF strTimeZone >= 0 Then
  797. strTimeZone = "00" & strTimeZone
  798. End if
  799. End If
  800. 'return to a format as "+|-" & UUU
  801. strTimeZone= strSign & strTimeZone
  802. End If
  803. ' check for the valid format - mm/dd/yy,hh:mm:ssPM
  804. ' PATTERN_DATETIME
  805. If CInt(component.matchPattern(PATTERN_DATETIME, strFilterValue)) > 0 Then
  806. If component.validateDateTime(strFilterValue) Then
  807. ' a valid datetime filter. Prepare for query
  808. strFilterProperty = FLD_FILTER_DATETIME
  809. strTemp = component.changeToWMIDateTime(strFilterValue,strTimeZone)
  810. ' Format the input
  811. ' TimeGenerated > "07/25/2000 10:12:00 PM"
  812. strFilterValue = Chr(34) & strTemp & Chr(34)
  813. End If
  814. Else
  815. ' match for range of dates in the format
  816. ' mm/dd/yy,hh:mm:ssPM - mm/dd/yy,hh:mm:ssAM
  817. ' PATTERN_DATETIME_RANGE
  818. If CInt(component.matchPattern(PATTERN_DATETIME_RANGE, strFilterValue)) > 0 Then
  819. strFilterProperty = FLD_FILTER_DATETIME
  820. ' Only = operation supported in this format
  821. If strFilterOperation <> "=" Then
  822. WScript.Echo (L_InvalidOperator_ErrorMessage)
  823. WScript.Quit(EXIT_INVALID_INPUT)
  824. End If
  825. arrTemp = split(strFilterValue,"-",2,VBBinaryCompare)
  826. If component.validateDateTime(arrTemp(0)) Then
  827. ' a valid datetime filter. Prepare for query
  828. strTemp = component.changeToWMIDateTime(arrTemp(0),strTimeZone)
  829. ' Format the input
  830. ' TimeGenerated > "07/25/2000 10:12:00 PM"
  831. strFilterOperation = ">="
  832. strFilterValue = Chr(34) & strTemp & Chr(34)
  833. If component.validateDateTime(arrTemp(1)) Then
  834. ' a valid datetime filter. Prepare for query
  835. strTemp = component.changeToWMIDateTime(arrTemp(1),strTimeZone)
  836. ' Format the input
  837. ' TimeGenerated > "07/25/2000 10:12:00 PM"
  838. strFilterValue = strFilterValue & _
  839. " AND " & strFilterProperty & "<="& Chr(34)_
  840. & strTemp & Chr(34)
  841. End If
  842. End If
  843. Else
  844. component.vbPrintf L_InvalidDateTimeFormat_ErrorMessage, Array(strFilter)
  845. WScript.Quit(EXIT_INVALID_INPUT)
  846. End If
  847. End If
  848. Case L_UserFilterType_Text
  849. ' the following values are only valid for the "Type" filter
  850. ' Valid: ERROR|INFORMATION|WARNING|SUCCESSAUDIT|FAILUREAUDIT
  851. ' PATTERNTYPE
  852. If CInt(component.matchPattern(PATTERNTYPE, strFilterValue)) = _
  853. CONST_NO_MATCHES_FOUND Then
  854. component.vbPrintf L_InvalidType_ErrorMessage, Array(strFilterValue, strFilter)
  855. WScript.Quit(EXIT_INVALID_INPUT)
  856. Else
  857. ' here i need to check WINXP or not
  858. If ( IsWinXP ( ObjService) = TRUE ) Then
  859. ' a valid type filter. Prepare for query
  860. If LCase(strFilterValue) =LCase(PATTERNTYPE_ERROR) Then
  861. strFilterValue = EVENTTYPE_ERROR
  862. ElseIf LCase(strFilterValue) =LCase(PATTERNTYPE_WARNING) Then
  863. strFilterValue = EVENTTYPE_WARNING
  864. ElseIf LCase(strFilterValue) =LCase(PATTERNTYPE_INFORMATION) Then
  865. strFilterValue = EVENTTYPE_INFORMATION
  866. ElseIf LCase(strFilterValue) =LCase(PATTERNTYPE_SUCCESSAUDIT) Then
  867. strFilterValue = EVENTTYPE_SUCCESSAUDIT
  868. ElseIf LCase(strFilterValue) =LCase(PATTERNTYPE_FAILUREAUDIT) Then
  869. strFilterValue = EVENTTYPE_FAILUREAUDIT
  870. End If
  871. ' a valid type filter. Prepare for query
  872. strFilterProperty = FLD_FILTER_EVENTTYPE
  873. Else
  874. ' a valid type filter. Prepare for query
  875. If LCase(strFilterValue) =LCase(PATTERNTYPE_SUCCESSAUDIT) Then
  876. strFilterValue = FLDFILTERTYPE_SUCCESSAUDIT
  877. ElseIf LCase(strFilterValue) =LCase(PATTERNTYPE_FAILUREAUDIT) Then
  878. strFilterValue = FLDFILTERTYPE_FAILUREAUDIT
  879. End If
  880. ' a valid type filter. Prepare for query
  881. strFilterProperty = FLD_FILTER_TYPE
  882. End If
  883. End If
  884. Case L_UserFilterUser_Text
  885. 'Checking " OR " is only supported property EQ "TYPE OR ID" only
  886. If blnOR = True then
  887. WScript.Echo InvalidORSyntaxInFilterErrorMessage
  888. WScript.Quit(EXIT_INVALID_INPUT)
  889. End If
  890. ' these are invalid characters for a user name
  891. ' PATTERN_INVALID_USER
  892. If CInt(component.matchPattern(PATTERN_INVALID_USER, strFilterValue)) > 0 Then
  893. component.vbPrintf L_InvalidUser_ErrorMessage , Array(strFilterValue, strFilter)
  894. WScript.Quit(EXIT_INVALID_INPUT)
  895. Else
  896. ' a valid user filter. Prepare for query
  897. If InStr(1, strFilterValue, "\", VBBinaryCompare) Then
  898. strFilterValue = Replace(strFilterValue, "\","\\")
  899. End If
  900. If LCase(strFilterValue) =LCase(L_TextNa_Text) Then
  901. strFilterValue = Null
  902. End If
  903. End If
  904. strFilterProperty = FLD_FILTER_USER
  905. Case L_UserFilterComputer_Text
  906. ' a valid computer filter. Prepare for query
  907. strFilterProperty = FLD_FILTER_COMPUTER
  908. 'Checking " OR " is only supported property EQ "TYPE OR ID" only
  909. If blnOR = True then
  910. WScript.Echo InvalidORSyntaxInFilterErrorMessage
  911. WScript.Quit(EXIT_INVALID_INPUT)
  912. End If
  913. Case L_UserFilterSource_Text
  914. ' a valid Source filter. Prepare for query
  915. strFilterProperty = FLD_FILTER_SOURCE
  916. 'Checking " OR " is only supported property EQ "TYPE OR ID" only
  917. If blnOR = True then
  918. WScript.Echo InvalidORSyntaxInFilterErrorMessage
  919. WScript.Quit(EXIT_INVALID_INPUT)
  920. End If
  921. Case L_UserFilterDateCategory_Text
  922. 'Checking " OR " is only supported property EQ "TYPE OR ID" only
  923. If blnOR = True then
  924. WScript.Echo InvalidORSyntaxInFilterErrorMessage
  925. WScript.Quit(EXIT_INVALID_INPUT)
  926. End If
  927. ' a valid Category filter. Prepare for query
  928. If LCase(strFilterValue) =LCase(L_TextNone_Text) Then
  929. strFilterValue = Null
  930. End If
  931. strFilterProperty = FLD_FILTER_CATEGORY
  932. Case L_UserFilterId_Text
  933. ' check if the given id is a number
  934. ' PATTERN_ID '"^(\d+)$"
  935. If CInt(component.matchPattern(PATTERN_ID, strFilterValue)) = CONST_NO_MATCHES_FOUND Then
  936. component.vbPrintf L_InvalidId_ErrorMessage, Array(strFilterValue, strFilter)
  937. WScript.Quit(EXIT_INVALID_INPUT)
  938. Else
  939. ' Invalid ID Number validation
  940. If ( Clng(strFilterValue) > CONST_ID_NUMBER )Then
  941. component.vbPrintf L_InvalidId_ErrorMessage, Array(strFilterValue, strFilter)
  942. WScript.Quit(EXIT_INVALID_INPUT)
  943. End If
  944. ' a valid id filter. Prepare for query
  945. strFilterProperty = FLD_FILTER_ID
  946. End If
  947. Case Else
  948. ' invalid filter specified
  949. component.vbPrintf L_InvalidFilter_ErrorMessage, Array(strFilterProperty, strFilter)
  950. WScript.Quit(EXIT_INVALID_INPUT)
  951. End Select
  952. If LCase(strFilterProperty) = LCase(FLD_FILTER_DATETIME) OR IsNull(strFilterValue) Then
  953. ' This is to handle NULL Property values i.e for category ,type
  954. If IsNull(strFilterValue) Then
  955. strFilter = strFilterProperty & strFilterOperation & strFilterValue & "Null"
  956. Else
  957. strFilter = strFilterProperty & strFilterOperation & strFilterValue
  958. End If
  959. Else
  960. strFilter = strFilterProperty & _
  961. strFilterOperation & Chr(34) & strFilterValue & Chr(34)
  962. End If
  963. 'Binding the string with "OR" to Prepare for query if blnOR is true
  964. If blnOR =TRUE Then
  965. If k = 0 then
  966. strTempFilter = strFilter
  967. Else
  968. strTempFilter = strTempFilter & " OR " & strFilter
  969. End If
  970. End If
  971. Next
  972. 'Set again making single filter string element if blnOR is TRUE
  973. If blnOR =TRUE Then
  974. 'this "()" Add the order of precedence of operation is SQL
  975. strFilter = "( " & strTempFilter & ")"
  976. End If
  977. 'Here setting filter to main array
  978. arrFilters(j) = strFilter
  979. Next
  980. ValidateFilters = TRUE
  981. End Function
  982. '********************************************************************
  983. '* Function: ReplaceOperators
  984. '*
  985. '* Purpose: Replaces the operator in string form with its symbol
  986. '*
  987. '* Input:
  988. '* [in] strFilterOperation the operation
  989. '*
  990. '* Output: Returns the symbolic operator
  991. '* If invalid operator, displays error message and quits
  992. '*
  993. '********************************************************************
  994. Private Function ReplaceOperators(ByVal strFilterOperation)
  995. ON ERROR RESUME NEXT
  996. Err.Clear
  997. Select Case LCase(strFilterOperation)
  998. Case L_OperatorEq_Text
  999. ReplaceOperators = SYMBOL_OPERATOR_EQ
  1000. Case L_OperatorNe_Text
  1001. ReplaceOperators = SYMBOL_OPERATOR_NE
  1002. Case L_OperatorGe_Text
  1003. ReplaceOperators = SYMBOL_OPERATOR_GE
  1004. Case L_OperatorLe_Text
  1005. ReplaceOperators = SYMBOL_OPERATOR_LE
  1006. Case L_OperatorGt_Text
  1007. ReplaceOperators = SYMBOL_OPERATOR_GT
  1008. Case L_OperatorLt_Text
  1009. ReplaceOperators = SYMBOL_OPERATOR_LT
  1010. Case Else
  1011. ' not a valid operator
  1012. component.vbPrintf L_Invalid_ErrorMessage, Array(strFilterOperation)
  1013. WScript.Quit(EXIT_INVALID_PARAM)
  1014. End Select
  1015. End Function
  1016. '********************************************************************
  1017. '* Sub : VerifyLogAndGetMaxRecords
  1018. '*
  1019. '* Purpose: populates the output array with count of records in given input array
  1020. '*
  1021. '* Input: [in] objService the service object
  1022. '* [out] objLogs the object containing the logs & max count of records corresponding log
  1023. '*
  1024. '* Output: array's are populates with logfile names and its count of max records
  1025. '*
  1026. '********************************************************************
  1027. Private Sub VerifyLogAndGetMaxRecords(ByVal objService, _
  1028. ByRef objLogs)
  1029. ON ERROR RESUME NEXT
  1030. Err.Clear
  1031. Dim strTempQuery ' string to make query
  1032. Dim objEnumerator ' to get the collection object after query
  1033. Dim objInstance ' to refer to each instance of the results got
  1034. Dim i ' for initialing loop
  1035. Dim strLogFile ' used to store log file inside loop
  1036. Dim arrKeyName ' used to store key value of Dictionary object for processing loop
  1037. arrKeyName = objLogs.Keys
  1038. For i = 0 to objLogs.Count -1
  1039. strLogFile = arrKeyName(i)
  1040. If Not strLogFile = "*" Then
  1041. ' Check if log file exists, by querying
  1042. strTempQuery = "SELECT NumberOfRecords FROM Win32_NTEventlogFile " &_
  1043. "WHERE LogfileName=" & Chr(34) & strLogFile & Chr(34)
  1044. Set objEnumerator = objService.ExecQuery(strTempQuery,,0)
  1045. If Err.Number Then
  1046. component.vbPrintf L_ExecuteQuery_ErrorMessage, Array(strLogFile)
  1047. WScript.Quit(EXIT_METHOD_FAIL)
  1048. End If
  1049. ' check if given log is present
  1050. If ObjEnumerator.Count <> 1 Then
  1051. component.vbPrintf L_LogDoesNotExist_ErrorMessage, Array(strLogFile)
  1052. 'If Count of Logs = 1 Quit Here
  1053. If objLogs.Count= 1 Then
  1054. WScript.Quit(EXIT_INVALID_INPUT)
  1055. End If
  1056. 'If more proceed ..
  1057. objLogs.Remove(strLogFile)
  1058. Else
  1059. ' get maximum number of records in that log(used if range specified)
  1060. For each objInstance in objEnumerator
  1061. If objInstance.NumberOfRecords <> "" Then
  1062. objLogs.Item(strLogFile) = objInstance.NumberOfRecords
  1063. Else
  1064. objLogs.Item(strLogFile) = 0
  1065. End If
  1066. Next
  1067. End If
  1068. Set ObjEnumerator = Nothing
  1069. End If
  1070. Next
  1071. If objLogs.Exists("*") Then
  1072. ' if the * is specified, populate array with elements
  1073. objLogs.Remove("*")
  1074. ' get the instances of the logs present in the system
  1075. Set objEnumerator = objService.InstancesOf(CLASS_EVENTLOG_FILE)
  1076. If Err.number Then
  1077. Wscript.Echo (L_InstancesFailed_ErrorMessage)
  1078. WScript.Quit(EXIT_METHOD_FAIL)
  1079. End If
  1080. ' if no logs present
  1081. If objEnumerator.Count <= 0 Then
  1082. WScript.Echo (L_InfoNoLogsPresent_Message)
  1083. WScript.Quit(EXIT_UNEXPECTED)
  1084. Else
  1085. For Each objInstance In objEnumerator
  1086. If Not IsEmpty(objInstance.LogfileName) Then
  1087. If NOT objLogs.Exists(LCase(objInstance.LogfileName)) Then
  1088. If objInstance.NumberOfRecords Then
  1089. objLogs.Add LCase(objInstance.LogfileName), objInstance.NumberOfRecords
  1090. Else
  1091. objLogs.Add LCase(objInstance.LogfileName), 0
  1092. End If
  1093. End If
  1094. End If
  1095. Next
  1096. End If
  1097. End If
  1098. End Sub
  1099. '********************************************************************
  1100. '* Function: BuildFiltersForQuery
  1101. '*
  1102. '* Purpose: Builds the query with the filter arguments
  1103. '*
  1104. '* Input: [in] arrFilters the array containing the filter conditions
  1105. '*
  1106. '* Output: Returns the string to be concatenated to the main query
  1107. '*
  1108. '********************************************************************
  1109. Function BuildFiltersForQuery(ByVal arrFilters)
  1110. ON ERROR RESUME NEXT
  1111. Err.Clear
  1112. Dim strTempFilter ' to store the return string
  1113. Dim i ' used in loop
  1114. strTempFilter = ""
  1115. For i = 0 to UBound(arrFilters)
  1116. strTempFilter = strTempFilter & " AND "
  1117. strTempFilter = strTempFilter & arrFilters(i)
  1118. Next
  1119. BuildFiltersForQuery = strTempFilter
  1120. End Function
  1121. '********************************************************************
  1122. '* Function : BuildRangeForQuery
  1123. '*
  1124. '* Purpose: Builds the range boundaries to display the records.
  1125. '*
  1126. '* Input: [in] strRange ' the range specified by the user
  1127. '* Will be in the format N, -N or N-N
  1128. '* [in] intFiltersSpecified ' array containing the filters number
  1129. '* [in] objService ' the service object
  1130. '* [out] intRecordRangeFrom ' where do we start the display of records?
  1131. '* [out] intRecordRangeTo ' where do we stop displaying records
  1132. '* [out] strFilterLog ' log file to build query
  1133. '* [out] strQuery ' to build query according to given Range Type
  1134. '* Output: Sets the value for the start and end of display boundaries.
  1135. '*
  1136. '********************************************************************
  1137. Private Function BuildRangeForQuery(ByVal strRange, _
  1138. ByRef intRecordRangeFrom, _
  1139. ByRef intRecordRangeTo,_
  1140. ByVal intFiltersSpecified,_
  1141. ByRef strQuery,_
  1142. ByVal ObjService,_
  1143. ByVal strFilterLog )
  1144. ON ERROR RESUME NEXT
  1145. Err.Clear
  1146. Dim intMaxEventRecordsPresent ' to store the max recods in the log
  1147. Dim arrRangeValues ' to store the split values if range is of the type N-N
  1148. Dim objInstance ' to refer to the instances of the objEnumerator
  1149. Dim objEnumerator ' to store the results of the query is executed
  1150. Dim FilterRecordCount ' to store the count of records if filter with +N specified
  1151. FilterRecordCount = 0
  1152. BuildRangeForQuery = strquery 'intialize
  1153. Dim currentMaxRecordnumber 'curentMaxrecord number
  1154. Dim currentMinRecordnumber 'curentMinrecord number
  1155. currentMaxRecordnumber = 0
  1156. currentMinRecordnumber = 0
  1157. ' save the max. no. of records available in the current log
  1158. intMaxEventRecordsPresent = intRecordRangeTo
  1159. ' find the count of events / logfile if Filter is specified .
  1160. If intFiltersSpecified >= 0 Then
  1161. Set objEnumerator = objService.ExecQuery(strQuery,"WQL",0,null)
  1162. If Err.number Then
  1163. component.vbPrintf L_ExecuteQuery_ErrorMessage, Array(strFilterLog)
  1164. Exit Function
  1165. End if
  1166. FilterRecordCount= objEnumerator.count
  1167. Set objEnumerator= Nothing 'releases the memory
  1168. End If
  1169. ' check the type of range specified ( first N / last N / N1 - N2 )
  1170. If ( IsNumeric(strRange) ) Then
  1171. ' range is first N or last N
  1172. ' now check whether it is first N or last N
  1173. If strRange < 0 Then
  1174. If intFiltersSpecified >= 0 Then
  1175. ' first N records
  1176. ' initial the counter so that all the out is displayed
  1177. If FilterRecordCount > CLng(Abs(strRange)) then
  1178. intRecordRangeFrom = FilterRecordCount - CLng(Abs(strRange)) + 1
  1179. intRecordRangeTo = FilterRecordCount
  1180. Else
  1181. intRecordRangeFrom = 0
  1182. intRecordRangeTo = FilterRecordCount
  1183. End If
  1184. Else
  1185. Set objEnumerator = objService.ExecQuery(strQuery,"WQL",48,null)
  1186. For Each objInstance In objEnumerator
  1187. currentMaxRecordnumber= objInstance.RecordNumber
  1188. Exit for
  1189. Next
  1190. If currentMaxRecordnumber > intMaxEventRecordsPresent then
  1191. currentMinRecordnumber = currentMaxRecordnumber - intMaxEventRecordsPresent
  1192. intMaxEventRecordsPresent = currentMaxRecordnumber
  1193. End If
  1194. Set objEnumerator= Nothing 'releases the memory
  1195. ' N means record number <= N
  1196. ' initial the counter s+o that all the out is displayed
  1197. ' build the query
  1198. BuildRangeForQuery = strQuery & " AND RecordNumber <= "& CLng(Abs(strRange)) + currentMinRecordnumber
  1199. End If
  1200. Else
  1201. ' *** range is last N (i.e -N)
  1202. If intFiltersSpecified >= 0 Then
  1203. If FilterRecordCount > CLng(Abs(strRange)) then
  1204. intRecordRangeFrom =0
  1205. intRecordRangeTo = CLng(Abs(strRange))
  1206. Else
  1207. intRecordRangeFrom =0
  1208. intRecordRangeTo = FilterRecordCount
  1209. End If
  1210. Else
  1211. Set objEnumerator = objService.ExecQuery(strQuery,"WQL",48,null)
  1212. 'getting current max recordnumber
  1213. For Each objInstance In objEnumerator
  1214. currentMaxRecordnumber= objInstance.RecordNumber
  1215. Exit for
  1216. Next
  1217. If currentMaxRecordnumber > intMaxEventRecordsPresent then
  1218. currentMinRecordnumber = currentMaxRecordnumber - intMaxEventRecordsPresent
  1219. intMaxEventRecordsPresent = currentMaxRecordnumber
  1220. End If
  1221. Set objEnumerator= Nothing 'releases the memory
  1222. ' -N means record number > (maxNumber - N )
  1223. ' initial the counter so that all the out is displayed
  1224. ' build the query
  1225. If CLng(Abs(strRange)) > intMaxEventRecordsPresent Then
  1226. 'Show all records
  1227. BuildRangeForQuery =strQuery & " AND RecordNumber > 0 "
  1228. Else
  1229. BuildRangeForQuery =strQuery & " AND RecordNumber > " & intMaxEventRecordsPresent - CLng(Abs(strRange))
  1230. End If
  1231. End If
  1232. End If
  1233. Else
  1234. ' range of records asked for N-N case
  1235. arrRangeValues = split(strRange,"-", 2, VBBinaryCompare)
  1236. If intFiltersSpecified >= 0 Then
  1237. If CLng(arrRangeValues(0)) < FilterRecordCount then
  1238. ' initial the counter so that all the out is displayed
  1239. intRecordRangeFrom = CLng(arrRangeValues(0))
  1240. intRecordRangeTo = CLng(arrRangeValues(1))
  1241. Else
  1242. 'forcebly putting the invaid query
  1243. 'when N1 > FilterRecordCount to avoid unnessaray looping between intRecordRangeFrom TO intRecordRangeTo
  1244. BuildRangeForQuery =strQuery & " AND RecordNumber = 0 "
  1245. End If
  1246. Else
  1247. Set objEnumerator = objService.ExecQuery(strQuery,"WQL",48,null)
  1248. For Each objInstance In objEnumerator
  1249. currentMaxRecordnumber= objInstance.RecordNumber
  1250. Exit for
  1251. Next
  1252. If currentMaxRecordnumber > intMaxEventRecordsPresent then
  1253. currentMinRecordnumber = currentMaxRecordnumber - intMaxEventRecordsPresent
  1254. intMaxEventRecordsPresent = currentMaxRecordnumber
  1255. End If
  1256. Set objEnumerator= Nothing 'releases the memory
  1257. ' build the query
  1258. BuildRangeForQuery =strQuery & " AND RecordNumber >= "& CLng(arrRangeValues(0))+ currentMinRecordnumber & " AND RecordNumber <= " & CLng(arrRangeValues(1)) + currentMinRecordnumber
  1259. End If
  1260. End If
  1261. End Function
  1262. '********************************************************************
  1263. '* Sub: ShowEvents
  1264. '*
  1265. '* Purpose: Displays the EventLog details
  1266. '*
  1267. '* Input:
  1268. '* [in] strMachine machine to query events from
  1269. '* [in] strUserName user name to connect to the machine
  1270. '* [in] strPassword password for the user
  1271. '* [in] arrFilters the array containing the filters
  1272. '* [in] strFormat the display format
  1273. '* [in] strRange the range of records required
  1274. '* [in] blnVerboseDisplay flag to verify if verbose display is needed
  1275. '* [in] blnNoHeader flag to verify if noheader display is needed
  1276. '* [in] objLogs to store all the given logfles
  1277. '* Output: Displays error message and quits if connection fails
  1278. '* Calls component.showResults() to display the event records
  1279. '*
  1280. '********************************************************************
  1281. Private Sub ShowEvents(ByVal strMachine, _
  1282. ByVal strUserName, _
  1283. ByVal strPassword, _
  1284. ByRef arrFilters, _
  1285. ByVal strFormat, _
  1286. ByVal strRange, _
  1287. ByVal blnVerboseDisplay, _
  1288. ByVal blnNoHeader,_
  1289. ByRef objLogs)
  1290. ON ERROR RESUME NEXT
  1291. Err.Clear
  1292. Dim objService ' the service object
  1293. Dim objEnumerator ' to store the results of the query is executed
  1294. Dim objInstance ' to refer to the instances of the objEnumerator
  1295. Dim strFilterLog ' to refer to each log specified by the user
  1296. Dim strTemp ' to store the temporary variables
  1297. Dim strQuery ' to store the query obtained for given conditions
  1298. Dim arrResults ' to store the columns of each filter
  1299. Dim arrHeader ' to store the array header values
  1300. Dim arrMaxLength ' to store the maximum length for each column
  1301. Dim arrFinalResults ' used to send the arrResults to component.showResults()
  1302. Dim arrTemp ' to store temporary array values
  1303. Dim intLoopCount ' used in the loop
  1304. Dim intElementCount ' used as array subscript
  1305. Dim strFilterQuery ' to store the query for the given filters
  1306. Dim intResultCount ' used to count no of records that are fetched in the query
  1307. Dim blnPrintHeader ' used to check header is printed or not in resulted Query
  1308. ' the following are used for implementing the range option
  1309. Dim intRecordRangeFrom ' to store the display record beginning number
  1310. Dim intRecordRangeTo ' to store the display record ending number
  1311. Dim arrKeyName ' to store then key value of dictionary object
  1312. Dim strTempQuery ' to store a string for -N range values
  1313. Dim arrblnDisplay ' array to show the status of display of verbose mode for showresults function
  1314. Dim intDataCount ' used in looping to get value of Insertion string for the field "Description column"
  1315. Dim i 'used for looping to enable All special privileges
  1316. ' flag to set condition specific locale & default value setting
  1317. Dim bLocaleChanged
  1318. bLocaleChanged =FALSE
  1319. 'Validating the arguments which is passed from commandline
  1320. If NOT (ValidateArguments(strMachine, strUserName, strPassword, _
  1321. arrFilters, strFormat, strRange , blnNoHeader)) Then
  1322. WScript.Quit(EXIT_UNEXPECTED)
  1323. End If
  1324. ' checking for UNC format for the system name
  1325. If Left(strMachine,2) = UNC_Format_Servername Then
  1326. If Len(strMachine) = 2 Then
  1327. component.vbPrintf InvalidInputErrorMessage ,Array(Wscript.ScriptName)
  1328. WScript.Quit(EXIT_UNEXPECTED)
  1329. End if
  1330. strMachine = Mid(strMachine,3,Len(strMachine))
  1331. End If
  1332. 'getting the password ....
  1333. If ((strUserName <> VBEmpty) AND (strPassword = VBEmpty)) Then
  1334. strPassword = component.getPassword()
  1335. End If
  1336. ' To set GetSupportedUserLocale for Some Diff locales
  1337. bLocaleChanged =GetSupportedUserLocale()
  1338. 'Establish a connection with the server.
  1339. If NOT component.wmiConnect(CONST_NAMESPACE_CIMV2 , _
  1340. strUserName , _
  1341. strPassword , _
  1342. strMachine , _
  1343. blnLocalConnection , _
  1344. objService ) Then
  1345. Wscript.Echo(L_HintCheckConnection_Message)
  1346. WScript.Quit(EXIT_METHOD_FAIL)
  1347. End If
  1348. ' set the previlige's To query all event's in eventlog's .
  1349. objService.Security_.Privileges.AddAsString("SeSecurityPrivilege")
  1350. 'Enable all privileges as some DC's were requiring special privileges
  1351. For i = 1 to 26
  1352. objService.Security_.Privileges.Add(i)
  1353. Next
  1354. ' get the HostName from the function
  1355. strMachine = component.getHostName( objService)
  1356. ' Validating the Filters which is passed from commandline
  1357. If UBound(arrFilters) >= 0 Then
  1358. ' filters are specified. Validate them
  1359. If Not ValidateFilters(arrFilters,objService ) Then
  1360. WScript.Quit(EXIT_INVALID_INPUT)
  1361. End If
  1362. End If
  1363. blnPrintHeader = TRUE
  1364. If blnNoHeader Then
  1365. blnPrintHeader = FALSE
  1366. End If
  1367. ' Initialize - header to display, the maximum length of each column and
  1368. ' number of columns present
  1369. arrHeader = Array(L_ColHeaderType_Text,L_ColHeaderEventcode_Text, L_ColHeaderDateTime_Text,_
  1370. L_ColHeaderSource_Text,L_ColHeaderComputerName_Text)
  1371. ' first initialize the array with N/A
  1372. arrResults = Array(L_TextNa_Text,L_TextNa_Text,L_TextNa_Text,L_TextNa_Text,L_TextNa_Text,L_TextNa_Text,_
  1373. L_TextNa_Text,L_TextNa_Text)
  1374. arrMaxLength = Array(13,6, 24, 17, 14, 15, 20,750)
  1375. arrblnDisplay = Array(0, 0, 0, 0, 0, 1, 1, 1)
  1376. If blnVerboseDisplay Then
  1377. arrblnDisplay = Array(0, 0, 0, 0, 0, 0, 0,0)
  1378. arrHeader = Array( L_ColHeaderType_Text,L_ColHeaderEventcode_Text, L_ColHeaderDateTime_Text, _
  1379. L_ColHeaderSource_Text,L_ColHeaderComputerName_Text,L_ColHeaderCategory_Text,_
  1380. L_ColHeaderUser_Text, L_ColHeaderDesription_Text)
  1381. End IF
  1382. If UBound(arrFilters) >=0 Then
  1383. strFilterQuery = BuildFiltersForQuery(arrFilters)
  1384. End If
  1385. ' call function to verify given log and also get records count in log
  1386. Call VerifyLogAndGetMaxRecords(objService, objLogs)
  1387. arrKeyName = objLogs.Keys
  1388. intResultCount = 0
  1389. intLoopCount = 0
  1390. 'blank line before first data is displayed on console
  1391. WScript.Echo EmptyLine_Text
  1392. Do While (intLoopCount < objLogs.Count)
  1393. 'setting Header to print every Log file explicilty
  1394. If blnNoHeader Then
  1395. blnPrintHeader = FALSE
  1396. Else
  1397. blnPrintHeader = TRUE
  1398. End If
  1399. If CInt(objLogs.Item(arrKeyName(intLoopCount))) > 0 Then
  1400. strFilterLog = arrKeyName(intLoopCount)
  1401. intRecordRangeFrom = 0
  1402. intRecordRangeTo = CInt(objLogs.Item(arrKeyName(intLoopCount)))
  1403. ' build the query
  1404. strQuery = "Select * FROM Win32_NTLogEvent WHERE Logfile=" &_
  1405. Chr(34) & strFilterLog & Chr(34)
  1406. If UBound(arrFilters) >=0 Then
  1407. strQuery = strQuery & strFilterQuery
  1408. End If
  1409. If Len(Trim(CStr(strRange))) > 0 Then
  1410. ' building again query for -N condition in range switch
  1411. strQuery = BuildRangeForQuery(strRange,intRecordRangeFrom, _
  1412. intRecordRangeTo, UBound(arrFilters),strQuery,objService,strFilterLog)
  1413. End If
  1414. ' process the results, else go for next log
  1415. Set objEnumerator = objService.ExecQuery(strQuery,"WQL",48,null)
  1416. If Err.Number Then
  1417. component.vbPrintf L_ExecuteQuery_ErrorMessage, Array(strFilterLog)
  1418. ' if error occurred in the query, go for next log
  1419. intLoopCount = intLoopCount + 1
  1420. Err.clear ' for next loop if more logs present
  1421. Else
  1422. intElementCount = 0
  1423. ReDim arrFinalResults(CONST_ARRAYBOUND_NUMBER)
  1424. For each objInstance in objEnumerator
  1425. ' inside error trapping for most unexpected case...
  1426. If Err.number then Exit For
  1427. intResultCount = intResultCount + 1
  1428. ' print the header for each log file along with Host Name
  1429. 'imp:: if and only if have Data
  1430. If intResultCount = 1 Then
  1431. WScript.Echo(String(78,"-"))
  1432. component.vbPrintf L_InfoDisplayLog_Message ,Array(strFilterLog,strMachine)
  1433. WScript.Echo(String(78,"-"))
  1434. End If
  1435. ' check whether the current record is fitting in
  1436. ' the required range
  1437. If ( intResultCount >= intRecordRangeFrom ) And _
  1438. ( intResultCount <= intRecordRangeTo ) Then
  1439. ' record fitting the range ... this has to be displayed
  1440. If objInstance.Type <> "" Then
  1441. arrResults(0) = objInstance.Type
  1442. Else
  1443. arrResults(0) = L_TextNa_Text
  1444. End If
  1445. If objInstance.EventCode <> "" Then
  1446. arrResults(1) = objInstance.EventCode
  1447. Else
  1448. arrResults(1) = L_TextNa_Text
  1449. End If
  1450. If (NOT IsEmpty(objInstance.TimeGenerated)) Then
  1451. strTemp = objInstance.TimeGenerated
  1452. 'is LOCALE CHANGED
  1453. If bLocaleChanged <> TRUE Then
  1454. 'format DatTime as DATE & "Space" & TIME
  1455. arrResults(2)= Formatdatetime( Mid(strTemp,5,2) & "/" & Mid(strTemp,7,2) & "/" &_
  1456. Mid(strTemp,1,4)) & " " & formatdatetime( Mid(strTemp,9,2) & ":" &_
  1457. Mid(strTemp,11,2) & ":" & Mid(strTemp,13,2))
  1458. Else
  1459. arrResults(2) = Mid(strTemp,5,2) & "/" & Mid(strTemp,7,2) & "/" &_
  1460. Mid(strTemp,1,4) & " " & Mid(strTemp,9,2) & ":" &_
  1461. Mid(strTemp,11,2) & ":" & Mid(strTemp,13,2)
  1462. End If
  1463. Else
  1464. arrResults(2) = L_TextNa_Text
  1465. End If
  1466. If objInstance.SourceName <> "" Then
  1467. arrResults(3) = objInstance.SourceName
  1468. Else
  1469. arrResults(3) = L_TextNa_Text
  1470. End If
  1471. If objInstance.ComputerName <> "" Then
  1472. arrResults(4) =objInstance.ComputerName
  1473. Else
  1474. arrResults(4) = L_TextNa_Text
  1475. End If
  1476. If blnVerboseDisplay Then
  1477. If objInstance.CategoryString <> "" Then
  1478. arrResults(5) = Replace(objInstance.CategoryString, VbCrLf, "")
  1479. Else
  1480. arrResults(5) = L_TextNone_Text ' None display
  1481. End If
  1482. If (NOT IsNull(objInstance.User)) Then
  1483. arrResults(6) = objInstance.User
  1484. Else
  1485. arrResults(6) = L_TextNa_Text
  1486. End If
  1487. If objInstance.Message <> "" Then
  1488. arrResults(7) = Trim(Replace(objInstance.Message, VbCrLf, " "))
  1489. Else
  1490. 'Check here eighter value in presenet "InsertionStrings" column .
  1491. If (NOT IsNull(objInstance.InsertionStrings)) Then
  1492. arrTemp = objInstance.InsertionStrings
  1493. 'removing default value "N/A"
  1494. arrResults(7)= ""
  1495. For intDataCount = 0 to UBound(arrTemp)
  1496. arrResults(7) = arrResults(7) & " " & arrTemp(intDataCount)
  1497. Next
  1498. arrResults(7) = Trim(arrResults(7))
  1499. Else
  1500. arrResults(7) = L_TextNa_Text
  1501. End If
  1502. End If
  1503. End If
  1504. ' add the record to the queue of records that has to be displayed
  1505. arrFinalResults( intElementCount ) = arrResults
  1506. intElementCount = intElementCount + 1 ' increment the buffer
  1507. ' check whether the output buffer is filled and ready for display
  1508. ' onto the screen or not
  1509. If intElementCount = CONST_ARRAYBOUND_NUMBER +1 Then
  1510. ' Call the display function with required parameters
  1511. Call component.showResults(arrHeader, arrFinalResults, arrMaxLength, _
  1512. strFormat, blnPrintHeader, arrblnDisplay)
  1513. blnPrintHeader = FALSE
  1514. Redim arrFinalResults(CONST_ARRAYBOUND_NUMBER) ' clear the existing buffer contents
  1515. intElementCount = 0 ' reset the buffer start
  1516. End If
  1517. End If
  1518. ' check whether the last record number that has to be displayed is
  1519. ' crossed or not ... if crossed exit the loop without proceeding further
  1520. If ( intResultCount >= intRecordRangeTo ) Then
  1521. ' max. TO range is crossed/reached ... no need of further looping
  1522. Exit For
  1523. End If
  1524. Next
  1525. ' check whether there any pending in the output buffer that has to be
  1526. ' displayed
  1527. If intElementCount > 0 Then
  1528. ' resize the array so that the buffer is shrinked to its content size
  1529. ReDim Preserve arrFinalResults( intElementCount - 1 )
  1530. ' Call the display function with required parameters
  1531. Call component.showResults(arrHeader, arrFinalResults, arrMaxLength, _
  1532. strFormat, blnPrintHeader, arrblnDisplay)
  1533. Else ' array bounds checking
  1534. If intResultCount = 0 Then
  1535. 'ie no records found
  1536. If UBound(arrFilters) >= 0 OR Len(Trim(CStr(strRange))) > 0 Then
  1537. ' message no records present if filter specified
  1538. component.vbPrintf L_InfoNoRecordsInFilter_Message, Array(strFilterLog)
  1539. Else
  1540. 'message no records present if filter not specified
  1541. component.vbPrintf L_InfoNoRecords_Message, Array(strFilterLog)
  1542. End If
  1543. End If ' intResultCount = 0
  1544. End If ' array bounds checking
  1545. End If
  1546. Else
  1547. 'message no records present
  1548. component.vbPrintf L_InfoNoRecords_Message, Array(arrKeyName(intLoopCount))
  1549. End If
  1550. ' re-initialize all the needed variables
  1551. intResultCount = 0
  1552. Set objEnumerator = Nothing
  1553. intLoopCount = intLoopCount + 1
  1554. 'blank line before end of the Next Each Log file details
  1555. WScript.Echo EmptyLine_Text
  1556. Loop ' do-while
  1557. End Sub
  1558. '********************************************************************
  1559. '* Function: GetSupportedUserLocale
  1560. '*
  1561. '* Purpose:This function checks if the current locale is supported or not.
  1562. '*
  1563. '* Output: Returns TRUE or FALSE
  1564. '*
  1565. '********************************************************************
  1566. Private Function GetSupportedUserLocale()
  1567. ON ERROR RESUME NEXT
  1568. Err.Clear
  1569. GetSupportedUserLocale =FALSE
  1570. CONST LANG_ARABIC = &H01
  1571. CONST LANG_HEBREW = &H0d
  1572. CONST LANG_HINDI = &H39
  1573. CONST LANG_TAMIL = &H49
  1574. CONST LANG_THAI = &H1e
  1575. CONST LANG_VIETNAMESE = &H2a
  1576. Dim Lcid 'to store LocaleId
  1577. ' get the current locale
  1578. Lcid=GetLocale()
  1579. CONST PRIMARYLANGID = 1023 '0x3ff
  1580. Dim LANGID 'to store LangID
  1581. ' Convert LCID >>>>>>>>>>>>> LANGID
  1582. ' BIT Wise And Operation
  1583. 'formating to compare HEX Value's
  1584. LANGID = Hex ( Lcid AND PRIMARYLANGID )
  1585. ' check whether the current locale is supported by our tool or not
  1586. ' if not change the locale to the English which is our default locale
  1587. Select Case LANGID
  1588. 'here to chaeck the values
  1589. Case Hex(LANG_ARABIC),Hex(LANG_HEBREW),Hex(LANG_THAI) ,Hex(LANG_HINDI ),Hex(LANG_TAMIL) ,Hex(LANG_VIETNAMESE)
  1590. GetSupportedUserLocale =TRUE
  1591. Exit Function
  1592. End Select
  1593. End Function
  1594. ' ****************************************************************************************
  1595. '* Function : IsWinXP
  1596. '*
  1597. '* Purpose:This function checks if the OS is XP or Above.
  1598. '*
  1599. '* Input: [in] Objservice the service object
  1600. '* Output: Returns TRUE or FALSE
  1601. '*
  1602. ' ****************************************************************************************
  1603. Private Function IsWinXP ( ByVal objService)
  1604. ON ERROR RESUME NEXT
  1605. Err.Clear
  1606. CONST WIN2K_MAJOR_VERSION = 5000
  1607. CONST WINXP_MAJOR_VERSION = 5001
  1608. Dim strQuery ' to store the query to be executed
  1609. Dim objEnum ' collection object
  1610. Dim objInstance ' instance object
  1611. Dim strVersion ' to store the OS version
  1612. Dim arrVersionElements ' to store the OS version elements
  1613. Dim CurrentMajorVersion ' the major version number
  1614. ISWinXP= FALSE
  1615. strQuery = "Select * From Win32_operatingsystem"
  1616. Set objEnum = objService.ExecQuery(strQuery,"WQL",0,NULL)
  1617. For each objInstance in objEnum
  1618. strVersion= objInstance.Version
  1619. Next
  1620. ' OS Version : 5.1.xxxx(Whistler), 5.0.xxxx(Win2K)
  1621. arrVersionElements = split(strVersion,".")
  1622. ' converting to major version
  1623. CurrentMajorVersion = arrVersionElements(0) * 1000 + arrVersionElements(1)
  1624. ' Determine the OS Type
  1625. ' WinXP > Win2K
  1626. If CInt(CurrentMajorVersion) >= CInt(WINXP_MAJOR_VERSION) Then
  1627. IsWinXP= TRUE
  1628. End If
  1629. End Function
  1630. '********************************************************************
  1631. '* Sub: ShowUsage
  1632. '*
  1633. '* Purpose: Shows the correct usage to the user.
  1634. '*
  1635. '* Output: Help messages are displayed on screen.
  1636. '*
  1637. '********************************************************************
  1638. Private Sub ShowUsage ()
  1639. WScript.Echo EmptyLine_Text
  1640. WScript.Echo L_ShowUsageLine01_Text
  1641. WScript.Echo L_ShowUsageLine02_Text
  1642. WScript.Echo EmptyLine_Text
  1643. WScript.Echo L_ShowUsageLine03_Text
  1644. WScript.Echo L_ShowUsageLine04_Text
  1645. WScript.Echo L_ShowUsageLine05_Text
  1646. WScript.Echo EmptyLine_Text
  1647. WScript.Echo L_ShowUsageLine06_Text
  1648. WScript.Echo L_ShowUsageLine07_Text
  1649. WScript.Echo EmptyLine_Text
  1650. WScript.Echo L_ShowUsageLine08_Text
  1651. WScript.Echo L_ShowUsageLine09_Text
  1652. WScript.Echo EmptyLine_Text
  1653. WScript.Echo L_ShowUsageLine10_Text
  1654. WScript.Echo L_ShowUsageLine11_Text
  1655. WScript.Echo EmptyLine_Text
  1656. WScript.Echo L_ShowUsageLine12_Text
  1657. WScript.Echo L_ShowUsageLine13_Text
  1658. WScript.Echo EmptyLine_Text
  1659. WScript.Echo L_ShowUsageLine14_Text
  1660. WScript.Echo L_ShowUsageLine15_Text
  1661. WScript.Echo EmptyLine_Text
  1662. WScript.Echo L_ShowUsageLine16_Text
  1663. WScript.Echo L_ShowUsageLine17_Text
  1664. WScript.Echo L_ShowUsageLine18_Text
  1665. WScript.Echo EmptyLine_Text
  1666. WScript.Echo L_ShowUsageLine19_Text
  1667. WScript.Echo L_ShowUsageLine20_Text
  1668. WScript.Echo L_ShowUsageLine21_Text
  1669. WScript.Echo L_ShowUsageLine22_Text
  1670. WScript.Echo L_ShowUsageLine23_Text
  1671. WScript.Echo EmptyLine_Text
  1672. WScript.Echo L_ShowUsageLine24_Text
  1673. WScript.Echo L_ShowUsageLine25_Text
  1674. WScript.Echo L_ShowUsageLine26_Text
  1675. WScript.Echo EmptyLine_Text
  1676. WScript.Echo L_ShowUsageLine27_Text
  1677. WScript.Echo EmptyLine_Text
  1678. WScript.Echo L_ShowUsageLine28_Text
  1679. WScript.Echo EmptyLine_Text
  1680. WScript.Echo L_ShowUsageLine29_Text
  1681. WScript.Echo L_ShowUsageLine30_Text
  1682. WScript.Echo L_ShowUsageLine31_Text
  1683. WScript.Echo L_ShowUsageLine32_Text
  1684. WScript.Echo L_ShowUsageLine33_Text
  1685. WScript.Echo L_ShowUsageLine34_Text
  1686. WScript.Echo L_ShowUsageLine35_Text
  1687. WScript.Echo L_ShowUsageLine36_Text
  1688. WScript.Echo L_ShowUsageLine37_Text
  1689. WScript.Echo L_ShowUsageLine38_Text
  1690. WScript.Echo EmptyLine_Text
  1691. WScript.Echo L_ShowUsageLine39_Text
  1692. WScript.Echo L_ShowUsageLine40_Text
  1693. WScript.Echo EmptyLine_Text
  1694. WScript.Echo L_ShowUsageLine41_Text
  1695. WScript.Echo L_ShowUsageLine42_Text
  1696. WScript.Echo L_ShowUsageLine43_Text
  1697. WScript.Echo L_ShowUsageLine44_Text
  1698. WScript.Echo L_ShowUsageLine45_Text
  1699. WScript.Echo L_ShowUsageLine46_Text
  1700. WScript.Echo L_ShowUsageLine47_Text
  1701. WScript.Echo L_ShowUsageLine48_Text
  1702. WScript.Echo L_ShowUsageLine49_Text
  1703. WScript.Echo L_ShowUsageLine50_Text
  1704. WScript.Echo L_ShowUsageLine51_Text
  1705. WScript.Echo L_ShowUsageLine52_Text
  1706. WScript.Echo L_ShowUsageLine53_Text
  1707. End Sub
  1708. '-----------------------------------------------------------------------------
  1709. ' End of the Script
  1710. '-----------------------------------------------------------------------------