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.

977 lines
39 KiB

  1. <?xml version="1.0"?>
  2. <package>
  3. <?component error="true" debug="true"?>
  4. <component>
  5. <registration
  6. description="VBS Library"
  7. progid="Microsoft.CmdLib"
  8. version="1"
  9. classid="{6D335ADF-8270-4805-A044-2B6A09476396}">
  10. </registration>
  11. <public>
  12. <comment>
  13. ******************************************************************************
  14. Copyright (c) Microsoft Corporation. All rights reserved.
  15. Module Name: CmdLib.wsc
  16. Abstract: This module contains the common functionality.
  17. *******************************************************************************
  18. </comment>
  19. <method name="checkScript"/>
  20. <method name="vbPrintf"/>
  21. <method name="getHostName"/>
  22. <method name="getUserName"/>
  23. <method name="getDomainName"/>
  24. <method name="LengthinBytes"/>
  25. <method name="getPassword"/>
  26. <method name="trapError"/>
  27. <method name="getArguments"/>
  28. <method name="wmiConnect"/>
  29. <method name="packString"/>
  30. <method name="getMaxStringLen"/>
  31. <method name="showResults"/>
  32. <method name="validateDateTime"/>
  33. <method name="changeToWMIDateTime"/>
  34. <method name="matchPattern"/>
  35. <property name="ScriptingHost" internalName="WScript"/>
  36. </public>
  37. <resource id="PATTERN_VBPRINTF">%\d</resource>
  38. <resource id="L_INVALID_ERRORMESSAGE_TYPE_AS_INPUT">ERROR: Invalid type passed as input to the function.</resource>
  39. <resource id="L_INVALID_ERRORMESSAGE_ARG_NUMBER_AS_INPUT_ERRORMESSAGE">ERROR: Invalid number of arguments passed to the Print function.</resource>
  40. <resource id="TEXT_NA">N/A</resource>
  41. <resource id="OBJ_SYSTEMINFO_CLASS">Win32_ComputerSystem</resource>
  42. <resource id="L_INVALID_ERRORMESSAGE">ERROR: Invalid '%1'.</resource>
  43. <resource id="L_INVALID_SYNTAX_ERRORMESSAGE">ERROR: Invalid Syntax. Value expected for '%1'.</resource>
  44. <resource id="L_HELP_SYNTAX_MESSAGE">Type "%1 /?" for usage.</resource>
  45. <resource id="HINT_CHECK_INPUT">Please check the input and try again.</resource>
  46. <resource id="L_ERROR_CHECK_VBSCRIPT_VERSION_ERRORMESSAGE">Unexpected Error: Please check the current version of VBScript.</resource>
  47. <resource id="PATTERN_NEGATIVE_NUMBER">^\-\d|\d+$</resource>
  48. <resource id="CONST_NO_MATCHES_FOUND">0</resource>
  49. <resource id="OBJ_SCRIPTING_LOCATOR">WbemScripting.SWbemLocator</resource>
  50. <resource id="L_DISPLAY_FMT_TABLE_TEXT">TABLE</resource>
  51. <resource id="L_DISPLAY_FMT_CSV_TEXT">CSV</resource>
  52. <resource id="L_DISPLAY_FMT_LIST_TEXT">LIST</resource>
  53. <resource id="EXIT_SUCCESS">0</resource>
  54. <resource id="EXIT_INVALID_PARAM">999</resource>
  55. <resource id="EXIT_UNEXPECTED">255</resource>
  56. <resource id="EXIT_INVALID_INPUT">254</resource>
  57. <resource id="EXIT_METHOD_FAIL">250</resource>
  58. <resource id="L_INVALID_ERRORMESSAGE_TIME_ERRORMESSAGE">ERROR: Invalid time '%1' specified for the filter '%2'.</resource>
  59. <resource id="L_INVALID_ERRORMESSAGE_DATE_ERRORMESSAGE">ERROR: Invalid date '%1' specified for the filter '%2'.</resource>
  60. <resource id="L_ENTER_PASSWORD_TEXT">Enter the Password:</resource>
  61. <resource id="L_PROCESSING_TEXT">Processing...</resource>
  62. <resource id="OBJ_SCRIPT_PASSWORD">ScriptPW.Password.1</resource>
  63. <resource id="L_HINT_CHECK_PASSWORD_DLL_MESSAGE">HINT: Please check if ScriptPW.dll is registered in the system.</resource>
  64. <resource id="CONST_ERROR">0</resource>
  65. <resource id="CONST_CSCRIPT">2</resource>
  66. <resource id="L_WARRING_LOCAL_CREDENTIALS_SUPPLIED_MESSAGE">WARNING: Ignoring the user credentials for the local connection.</resource>
  67. <resource id="CONST_LOCAL_CREDENTIALS_SUPPLIED">-2147217308</resource>
  68. <script language="VBScript">
  69. <![CDATA[
  70. ' All the functions which are used in common across all the vbs scripts are defined below
  71. ' Function used to find whether CScript is used or not
  72. '********************************************************************
  73. '* Function: checkScript
  74. '*
  75. '* Purpose: Determines which program is used to run this script.
  76. '*
  77. '* Input: None
  78. '*
  79. '* Output: intChkProgram is set to one of CONST_ERROR or CONST_CSCRIPT.
  80. '*
  81. '********************************************************************
  82. Function checkScript()
  83. ON ERROR RESUME NEXT
  84. Err.Clear
  85. Dim strFullName 'program with its full path - used to execute the script
  86. Dim strCommand 'name of program without extension (like exe, Eg:CScript)
  87. Dim intExe_Index 'to calculate the position of .exe in strFullName
  88. Dim intSlash_Index 'to calculate the position of \ (slash) in strFullName
  89. 'strFullName should be something like C:\WINDOWS\COMMAND\CSCRIPT.EXE
  90. strFullName = WScript.FullName
  91. If Err.Number then
  92. Wscript.Echo "Error 0x" & CStr(Hex(Err.Number))
  93. If Err.Description <> "" Then
  94. Wscript.Echo "Error description: " & Err.Description & "."
  95. End If
  96. Err.Clear
  97. checkScript = getResource("CONST_ERROR")
  98. Exit Function
  99. End If
  100. intExe_Index = InStr(1, strFullName, ".exe", 1)
  101. If intExe_Index = 0 Then
  102. checkScript = getResource("CONST_ERROR")
  103. Exit Function
  104. Else
  105. intSlash_Index = InStrRev(strFullName, "\", intExe_Index, 1)
  106. If intSlash_Index = 0 Then
  107. checkScript = getResource("CONST_ERROR")
  108. Exit Function
  109. Else
  110. strCommand = Mid(strFullName, intSlash_Index+1, _
  111. intExe_Index-intSlash_Index-1)
  112. If LCase(strCommand) = LCase("cscript") Then
  113. checkScript = getResource("CONST_CSCRIPT")
  114. Else
  115. checkScript = getResource("CONST_ERROR")
  116. End If
  117. End If 'If intSlash_Index = 0 Then
  118. End If 'If intExe_Index = 0 Then
  119. End Function
  120. ' Subroutine which implements normal printf functionality
  121. '********************************************************************
  122. '* Sub: vbPrintf
  123. '*
  124. '* Purpose: Simulates the Printf function.
  125. '*
  126. '* Input: [in] strPhrase the string with '%1 %2 &3 ' in it
  127. '* [in] args the values to replace '%1 %2 ..etc' with
  128. '*
  129. '* Output: Displays the string on the screen
  130. '* (All the '%x' variables in strPhrase is replaced by the
  131. '* corresponding elements in the array)
  132. '*
  133. '********************************************************************
  134. Sub vbPrintf(ByVal strPhrase, ByVal args )
  135. ON ERROR RESUME NEXT
  136. Err.Clear
  137. 'Changed for localization
  138. Dim strMatchPattern ' the pattern to match - '%[number]'
  139. Dim intValuesCount ' to get the count of matching results
  140. Dim i ' used in the loop
  141. Dim strTemp ' to store temporally the given input string for formatting
  142. strTemp = strPhrase
  143. ' look out for '%[number]' in the given string
  144. strMatchPattern = getResource("PATTERN_VBPRINTF") '"\%[number]"
  145. intValuesCount = matchPattern (strMatchPattern, strTemp)
  146. If intValuesCount <> 0 Then
  147. ' if present then replace '%1 %2 %3' in the string by
  148. ' corresponding element in the given array
  149. If Not IsArray(args) Then
  150. WScript.Echo getResource("L_INVALID_ERRORMESSAGE_TYPE_AS_INPUT")
  151. WScript.Quit getResource("EXIT_INVALID_PARAM")
  152. End If
  153. If intValuesCount <> UBound(args)+1 Then
  154. WScript.Echo getResource("L_INVALID_ERRORMESSAGE_ARG_NUMBER_AS_INPUT_ERRORMESSAGE")
  155. WScript.Quit getResource("EXIT_INVALID_PARAM")
  156. End If
  157. For i = 1 to intValuesCount
  158. strPhrase = Replace(strPhrase, "%" & Cstr(i), (args(i-1) ), 1, 1, VBBinaryCompare)
  159. Next
  160. End If
  161. WScript.Echo(strPhrase)
  162. End Sub
  163. ' Function which checks whether a given value matches a particular pattern
  164. '********************************************************************
  165. '* Function: matchPattern
  166. '*
  167. '* Purpose: To check if the given pattern is existing in the string
  168. '*
  169. '* Input:
  170. '* [in] strMatchPattern the pattern to look out for
  171. '* [in] strPhrase string in which the pattern needs to be checked
  172. '*
  173. '* Output: Returns number of occurrences if pattern present,
  174. '* Else returns CONST_NO_MATCHES_FOUND
  175. '*
  176. '********************************************************************
  177. Function matchPattern(ByVal strMatchPattern, ByVal strPhrase)
  178. ON ERROR RESUME NEXT
  179. Err.Clear
  180. Dim objRegEx ' the regular expression object
  181. Dim Matches ' the results that match the given pattern
  182. Dim intResultsCount ' the count of Matches
  183. intResultsCount = 0 ' initialize the count to 0
  184. 'create instance of RegExp object
  185. Set objRegEx = New RegExp
  186. If (NOT IsObject(objRegEx)) Then
  187. WScript.Echo (getResource("L_ERROR_CHECK_VBSCRIPT_VERSION_ERRORMESSAGE"))
  188. End If
  189. 'find all matches
  190. objRegEx.Global = True
  191. 'set case insensitive
  192. objRegEx.IgnoreCase = True
  193. 'set the pattern
  194. objRegEx.Pattern = strMatchPattern
  195. Set Matches = objRegEx.Execute(strPhrase)
  196. intResultsCount = Matches.Count
  197. 'test for match
  198. If intResultsCount > 0 Then
  199. matchPattern = intResultsCount
  200. Else
  201. matchPattern = getResource("CONST_NO_MATCHES_FOUND")
  202. End If
  203. End Function
  204. ' Function used to get the current Host name
  205. '********************************************************************
  206. '* Function: getHostName
  207. '*
  208. '* Purpose: To get the Host Name
  209. '*
  210. '* Input: objService ' the service object
  211. '*
  212. '* Output: Returns the Host Name
  213. '*
  214. '********************************************************************
  215. Function getHostName ( ByVal ObjService)
  216. ON ERROR RESUME NEXT
  217. Err.Clear
  218. Dim objSystemSet ' to store the InstancesOf Class
  219. Dim System ' to refer to the instances objSystemSet
  220. Set objSystemSet = objService.InstancesOf(getResource("OBJ_SYSTEMINFO_CLASS"))
  221. If Err.Number Then
  222. getHostName = getResource("TEXT_NA")
  223. Err.clear
  224. Else
  225. For each System in objSystemSet
  226. If IsEmpty(System.Name) Then
  227. getHostName = getResource("TEXT_NA")
  228. Else
  229. getHostName = System.Name
  230. End If
  231. Exit for
  232. Next
  233. End If
  234. End Function
  235. ' Function used to get the current User Name
  236. '********************************************************************
  237. '* Function: getUserName
  238. '*
  239. '* Purpose: To get the User Name
  240. '*
  241. '* Input: objService ' the service object
  242. '*
  243. '* Output: Returns the User Name
  244. '*
  245. '********************************************************************
  246. Function getUserName ( ByVal ObjService)
  247. ON ERROR RESUME NEXT
  248. Err.Clear
  249. Dim objSystemSet ' to store the InstancesOf Class
  250. Dim System ' to refer to the instances objSystemSet
  251. Set objSystemSet = objService.InstancesOf(getResource("OBJ_SYSTEMINFO_CLASS"))
  252. If Err.Number Then
  253. getUserName = getResource("TEXT_NA")
  254. Err.clear
  255. Else
  256. For each System in objSystemSet
  257. If IsEmpty(System.UserName) Then
  258. getUserName = getResource("TEXT_NA")
  259. Else
  260. getUserName = System.UserName
  261. End If
  262. Exit for
  263. Next
  264. End If
  265. End Function
  266. ' Function used to get the current Domain name
  267. '********************************************************************
  268. '* Function: getDomainName
  269. '*
  270. '* Purpose: To get the Domain Name
  271. '*
  272. '* Input: objService ' the service object
  273. '*
  274. '* Output: Returns the Domain Name
  275. '*
  276. '********************************************************************
  277. Function getDomainName( ByVal ObjService)
  278. ON ERROR RESUME NEXT
  279. Err.Clear
  280. Dim objSystemSet ' to store the InstancesOf Class
  281. Dim System ' to refer to the instances objSystemSet
  282. Set objSystemSet = objService.InstancesOf(getResource("OBJ_SYSTEMINFO_CLASS"))
  283. If Err.Number Then
  284. getDomainName = getResource("TEXT_NA")
  285. Err.clear
  286. Else
  287. For each System in objSystemSet
  288. If IsEmpty(System.Domain) Then
  289. getDomainName = getResource("TEXT_NA")
  290. Else
  291. getDomainName = System.Domain
  292. End If
  293. Exit for
  294. Next
  295. End If
  296. End Function
  297. ' Function used to get the password from the user
  298. '**********************************************************************
  299. '* Function: getPassword
  300. '*
  301. '* Purpose: To get password from the user
  302. '*
  303. '* Input: None
  304. '*
  305. '* Output: Returns the Password specified by the user
  306. '*
  307. '**********************************************************************
  308. Function getPassword()
  309. ON ERROR RESUME NEXT
  310. Err.Clear
  311. Dim objPassword ' the object to store password.dll
  312. WScript.Echo getResource("L_ENTER_PASSWORD_TEXT")
  313. Set objPassword = CreateObject(getResource("OBJ_SCRIPT_PASSWORD"))
  314. If NOT IsObject(objPassword) Then
  315. ' error in getting the password
  316. WScript.Echo("") 'blank line
  317. WScript.Echo(getResource("L_HINT_CHECK_PASSWORD_DLL_MESSAGE"))
  318. WScript.Quit(getResource("EXIT_UNEXPECTED"))
  319. End If
  320. getPassword = objPassword.GetPassword
  321. WScript.Echo getResource("L_PROCESSING_TEXT")
  322. End Function
  323. ' Function used to trap error
  324. '**********************************************************************
  325. '* Function: trapError
  326. '*
  327. '* Purpose: Reports error with a string saying what the error occurred in.
  328. '*
  329. '* Input:
  330. '* [in] strIn string saying what the error occurred in.
  331. '*
  332. '* Output: displayed on screen
  333. '*
  334. '**********************************************************************
  335. Function trapError (ByVal strIn)
  336. ON ERROR RESUME NEXT
  337. If Err.Number Then
  338. Wscript.Echo( "Error (0x" & CStr(Hex(Err.Number)) & "): " & strIn)
  339. If Err.Description <> "" Then
  340. Wscript.Echo( "Error description: " & Err.Description)
  341. End If
  342. Err.Clear
  343. trapError = TRUE
  344. Else
  345. trapError = FALSE
  346. End If
  347. End Function
  348. ' Function used to get the arguments into appropriate variables
  349. '**********************************************************************
  350. '* Function: getArguments
  351. '*
  352. '* Purpose: Gets the arguments specified into appropriate variables
  353. '*
  354. '* Input:
  355. '* [in] StrVarName stores the parameter
  356. '* [in] strVar stores the parameter value
  357. '* [in] intArgIter counts the no.of arguments
  358. '* [in] blnAllowNegativeValues checks if negative parameter values are valid
  359. '*
  360. '* Output: Returns TRUE or FALSE
  361. '*
  362. '**********************************************************************
  363. ' Function used to get the arguments into appropriate variables
  364. Function getArguments ( ByVal StrVarName, _
  365. ByRef strVar, _
  366. ByRef intArgIter, _
  367. ByVal blnAllowNegativeValues )
  368. ON ERROR RESUME NEXT
  369. Err.Clear
  370. 'initialized to failure, changed to True upon successful completion
  371. getArguments = False
  372. intArgIter = intArgIter + 1
  373. If intArgIter > (Wscript.Arguments.Count - 1) Then
  374. vbPrintf getResource("L_INVALID_SYNTAX_ERRORMESSAGE"), Array(Wscript.Arguments.Item(intArgIter-1))
  375. Exit Function
  376. End If
  377. strVar = Wscript.Arguments.Item(intArgIter)
  378. If Err.Number Then
  379. vbPrintf getResource("L_INVALID_ERRORMESSAGE"), Array(StrVarName)
  380. Call Wscript.Echo ( getResource("HINT_CHECK_INPUT") )
  381. Err.Clear
  382. Exit Function
  383. End If
  384. ' check for the input of those accept negitive numeric values also.
  385. If blnAllowNegativeValues =True Then
  386. ' the input can be a negative number
  387. If matchPattern(getResource("PATTERN_NEGATIVE_NUMBER"), strVar) = getResource("CONST_NO_MATCHES_FOUND") Then
  388. vbPrintf getResource("L_INVALID_ERRORMESSAGE"), Array(StrVarName)
  389. Wscript.Echo ( getResource("HINT_CHECK_INPUT") )
  390. Exit Function
  391. End If
  392. End If
  393. getArguments = True 'success
  394. End Function
  395. ' Function used to connect to wmi provider with the given credentials
  396. '**************************************************************************
  397. '* Function: wmiConnect
  398. '*
  399. '* Purpose: Connects to machine strServer.
  400. '*
  401. '* Input:
  402. '* [in] strServer a machine name
  403. '* [in] strNameSpace a namespace
  404. '* [in] strUserName name of the current user
  405. '* [in] strPassword password of the current user
  406. '* [in/out] blnLocalConnection a flag for localConnection
  407. '* [out] objService a service object
  408. '*
  409. '* Output: objService is returned as a service object.
  410. '*
  411. '**************************************************************************
  412. Function wmiConnect( ByVal strNameSpace, _
  413. ByVal strUserName, _
  414. ByVal strPassword, _
  415. ByVal strServer, _
  416. ByRef blnLocalConnection, _
  417. ByRef objService )
  418. ON ERROR RESUME NEXT
  419. Err.Clear
  420. Dim objLocator ' the locator object
  421. wmiConnect = True ' There is no error.
  422. 'Create Locator object to connect to remote CIM object manager
  423. Set objLocator = CreateObject(getResource("OBJ_SCRIPTING_LOCATOR"))
  424. If Err.Number Then
  425. wmiConnect = False ' An error occurred
  426. Exit Function
  427. End If
  428. 'Connect to the namespace which is either local or remote
  429. Set objService = objLocator.ConnectServer (strServer, strNameSpace, _
  430. strUserName, strPassword)
  431. If Err.Number <> 0 Then
  432. If Err.Number = Clng(getResource("CONST_LOCAL_CREDENTIALS_SUPPLIED")) Then
  433. If Not blnLocalConnection =True then
  434. ' -2147217308 number to catch local credentails supplied by WMI
  435. Wscript.echo getResource("L_WARRING_LOCAL_CREDENTIALS_SUPPLIED_MESSAGE")
  436. 'setting the flag that target is local system to eleminate error message next time
  437. blnLocalConnection = True
  438. End If
  439. Err.Clear ' clear the error number for local connection
  440. ' Calling the Locator object to connect to local system
  441. Set objService = objLocator.ConnectServer(strServer, strNameSpace, "" , "" )
  442. If Err.Number <> 0 Then wmiConnect = False ' An error occurred
  443. Else
  444. wmiConnect = False ' An error occurred
  445. End If
  446. End If
  447. ObjService.Security_.impersonationlevel = 3
  448. End Function
  449. ' Function used to pack the string to the given width
  450. '**************************************************************************
  451. '* Function: strPackString
  452. '*
  453. '* Purpose: Attaches spaces to a string to increase the length to intWidth.
  454. '*
  455. '* Input:
  456. '* [in] strString a string
  457. '* [in] intWidth the intended length of the string
  458. '*
  459. '* Output: strPackString is returned as the packed (padded/truncated) string.
  460. '*
  461. '**************************************************************************
  462. Function packString( ByVal strString, ByVal intWidth)
  463. ON ERROR RESUME NEXT
  464. Err.Clear
  465. strString = CStr(strString)
  466. If Err.Number Then
  467. Call Wscript.Echo (getResource("L_INVALID_ERRORMESSAGE_TYPE_AS_INPUT"))
  468. Err.Clear
  469. Wscript.Quit(getResource("EXIT_INVALID_PARAM"))
  470. End If
  471. intWidth = CInt(intWidth)
  472. If Err.Number Then
  473. Call Wscript.Echo (getResource("L_INVALID_ERRORMESSAGE_TYPE_AS_INPUT"))
  474. Err.Clear
  475. Wscript.Quit(getResource("EXIT_INVALID_PARAM"))
  476. End If
  477. If IsNull(strString) OR IsEmpty(strString) OR Len(strString) = 0 Then
  478. packString = getResource("TEXT_NA") & Space(intWidth-3)
  479. Exit Function
  480. End If
  481. If intWidth >= LengthinBytes(strString) Then
  482. packString = strString & Space(intWidth-LengthinBytes(strString))
  483. Else
  484. ' truncate the string
  485. packString = Left(strString, intWidth)
  486. End If
  487. End Function
  488. ' Function used to get length of the maximum length string in an array of strings
  489. '**************************************************************************
  490. '* Function: getMaxStringLength
  491. '*
  492. '* Purpose: To get the length of longest string in the given array
  493. '*
  494. '* Input: [in] arrStrings an array of strings
  495. '*
  496. '* Output: Returns length of longest string in the array
  497. '* If error in input, displays message and quits
  498. '*
  499. '**************************************************************************
  500. Function getMaxStringLen(ByVal arrStrings)
  501. ON ERROR RESUME NEXT
  502. Err.Clear
  503. Dim intMaxLength ' to store the maximum length of the string
  504. Dim intArrCount ' used in the loop
  505. intMaxLength = 0
  506. ' quit if input is not an array
  507. If NOT IsArray(arrStrings) Then
  508. WScript.Echo getResource("L_INVALID_ERRORMESSAGE_TYPE_AS_INPUT")
  509. WScript.Quit(getResource("EXIT_INVALID_PARAM"))
  510. End If
  511. ' check for length of each element in the array
  512. For intArrCount = 0 To UBound(arrStrings)
  513. If LengthinBytes(arrStrings(intArrCount)) > intMaxLength Then
  514. intMaxLength = LengthinBytes(arrStrings(intArrCount))
  515. End If
  516. Next
  517. getMaxStringLen = intMaxLength
  518. End Function
  519. ' Function used to get length of actual bytes required by the string.
  520. '**************************************************************************
  521. '* Function: LengthinBytes
  522. '*
  523. '* Purpose: To get the length of a string in Bytes.
  524. '*
  525. '* Input: [in] strString a String
  526. '*
  527. '* Output: Returns length of a string in Bytes.
  528. '*
  529. '**************************************************************************
  530. Function LengthinBytes(ByVal strString)
  531. Dim i, strChar
  532. LengthinBytes = 0
  533. For i =1 To Len(strString)
  534. strChar = Mid(strString, i, 1)
  535. If Asc(strChar) > 255 OR Asc(strChar) < 0 Then
  536. LengthinBytes = LengthinBytes + 2
  537. Else
  538. LengthinBytes = LengthinBytes + 1
  539. End If
  540. Next
  541. End Function
  542. ' Function used to show results in the desired format
  543. '**************************************************************************
  544. '* Function: showResults
  545. '*
  546. '* Purpose: To show results in the desired format
  547. '*
  548. '* Input:
  549. '* [in] arrHeader an array of strings containing all the headers
  550. '* [in] arrResultsArray array containing all the records
  551. '* [in] strFormat CSV or LIST or TABLE
  552. '* [in] blnPrintHeader Boolean value indicating whether header
  553. '* should be printed or not
  554. '* [in] arrBlnHide an array containing boolean values. Each value
  555. '* indicates whether a particular value in a record
  556. '* is to be displayed or not
  557. '*
  558. '* Output: Displays all the records in the required format
  559. '*
  560. '**************************************************************************
  561. Sub showResults( ByVal arrHeader, _
  562. ByVal arrResultsArray, _
  563. ByVal arrMaxLength, _
  564. ByVal strFormat, _
  565. ByVal blnPrintHeader, _
  566. ByVal arrBlnHide )
  567. ON ERROR RESUME NEXT
  568. Err.Clear
  569. Dim i, j ' used as loop variables
  570. Dim intTestResult ' to store temporary results
  571. Dim intMaxHeaderLength ' to store length of longest column header
  572. Dim strPackedString ' to store the padded/truncated string
  573. Dim arrResults ' to store the row to display(which is an array)
  574. Dim intColumnCount ' to store the count for no.of columns
  575. ' get the maximum length of all the header names given
  576. intMaxHeaderLength = getMaxStringLen(arrHeader)
  577. ' initialize the values
  578. intColumnCount = UBound(arrHeader)
  579. intTestResult = 0
  580. Select Case LCase(strFormat)
  581. Case LCase(getResource("L_DISPLAY_FMT_LIST_TEXT"))
  582. ' If LIST format is specified
  583. For i = 0 to UBound(arrResultsArray)
  584. arrResults = arrResultsArray(i)
  585. For j = 0 to UBound(arrResults)
  586. If arrBlnHide(j) = 0 Then
  587. intTestResult = arrHeader(j) & ":"
  588. strPackedString = packString(intTestResult, intMaxHeaderLength+1)
  589. WScript.Echo strPackedString & " " & arrResults(j)
  590. End If
  591. Next
  592. ' print an empty line
  593. WScript.Echo ""
  594. Next
  595. Case LCase(getResource("L_DISPLAY_FMT_CSV_TEXT"))
  596. ' If CSV format is specified
  597. If blnPrintHeader Then
  598. strPackedString = ""
  599. ' first print the header , if not already printed
  600. For i = 0 to UBound(arrHeader)
  601. If arrBlnHide(i) = 0 Then
  602. intTestResult = InStr(1,arrHeader(i), ",", VBBinaryCompare)
  603. If intTestResult > 0 Then
  604. arrHeader(i) = chr(34) & arrHeader(i) & chr(34)
  605. Else
  606. arrHeader(i) = chr(34) & arrHeader(i) & chr(34)
  607. End If
  608. strPackedString = strPackedString & arrHeader(i)
  609. If (i+1) <= intColumnCount Then
  610. strPackedString = strPackedString & ","
  611. End If
  612. End If
  613. Next
  614. WScript.Echo strPackedString
  615. End If
  616. ' print all the comma separated values
  617. For i = 0 to UBound(arrResultsArray)
  618. arrResults = arrResultsArray(i)
  619. strPackedString = ""
  620. For j = 0 to UBound(arrResults)
  621. If arrBlnHide(j) = 0 Then
  622. intTestResult = InStr(1,arrResults(j), ",", VBBinaryCompare)
  623. If intTestResult > 0 Then
  624. strPackedString = strPackedString & chr(34) & arrResults(j) & chr(34)
  625. Else
  626. strPackedString = strPackedString & chr(34) & arrResults(j) & chr(34)
  627. End If
  628. If (j+1) <= intColumnCount Then
  629. strPackedString = strPackedString & ","
  630. ' strPackedString = strPackedString & chr(34) & "," & chr(34)
  631. End If
  632. End If
  633. Next
  634. WScript.Echo strPackedString
  635. strPackedString = ""
  636. Next
  637. Case LCase(getResource("L_DISPLAY_FMT_TABLE_TEXT"))
  638. ' If table format is asked for
  639. If blnPrintHeader Then
  640. strPackedString = ""
  641. ' print the header, if not already printed
  642. For i = 0 to UBound(arrHeader)
  643. If arrBlnHide(i) = 0 Then
  644. strPackedString = strPackedString & " " & _
  645. packString(arrHeader(i), _
  646. arrMaxLength(i))
  647. End If
  648. Next
  649. WScript.Echo strPackedString
  650. strPackedString = ""
  651. ' print the Underline to the column header
  652. For i = 0 to UBound(arrHeader)
  653. If arrBlnHide(i) = 0 Then
  654. strPackedString = strPackedString & " " & _
  655. packString(String(arrMaxLength(i),"-"), arrMaxLength(i))
  656. End If
  657. Next
  658. WScript.Echo strPackedString
  659. End If
  660. For i = 0 to UBound(arrResultsArray)
  661. arrResults = arrResultsArray(i)
  662. strPackedString = ""
  663. For j = 0 to UBound(arrResults)
  664. If arrBlnHide(j) = 0 Then
  665. strPackedString = strPackedString & " " & _
  666. packString(arrResults(j), _
  667. arrMaxLength(j))
  668. End If
  669. Next
  670. WScript.Echo strPackedString
  671. Next
  672. End Select
  673. End Sub
  674. '********************************************************************
  675. '* Function: strDateTime
  676. '*
  677. '* Purpose: To validate the date-time format specified
  678. '*
  679. '* Input:
  680. '* [in] strDateTime the date-time string
  681. '*
  682. '* Output: Returns true if valid format
  683. '* Else displays error message and quits
  684. '*
  685. '********************************************************************
  686. Function validateDateTime(ByVal strDateTime)
  687. ON ERROR RESUME NEXT
  688. Err.Clear
  689. validateDateTime = False
  690. Dim arrDateTimeCheck ' to store the date and time values
  691. Dim intMonth ' to store the month(instead of array(subscript))
  692. Dim intDay ' to store the day(instead of array(subscript))
  693. Dim intYear ' to store the year(instead of array(subscript))
  694. Dim strTemp ' to store temporary values
  695. Dim arrTemp ' to store temporary values when split is used
  696. Dim intHour ' to store the Hour(instead of array(subscript))
  697. Dim intMinute ' to store the Minutes(instead of array(subscript))
  698. Dim intSecond ' to store the Seconds(instead of array(subscript))
  699. ' strDateTime is of the format "mm/dd/yy|yyyy,hh:mm:ssPM"
  700. ' first split at the comma and separate date and time
  701. arrDateTimeCheck = split(strDateTime, ",",2,VBBinaryCompare)
  702. ' split the date and check if the month and day are in bounds
  703. arrTemp = split(arrDateTimeCheck(0), "/",3,VBBinaryCompare)
  704. intMonth = arrTemp(0)
  705. intDay = arrTemp(1)
  706. intYear = arrTemp(2)
  707. If ((CInt(intMonth) < 1) OR (CInt(intMonth) > 12) OR (CInt(intDay) < 1) OR (CInt(intDay) > 31)) Then
  708. vbPrintf getResource("L_INVALID_ERRORMESSAGE_DATE_ERRORMESSAGE"), Array(arrDateTimeCheck(0), strDateTime)
  709. WScript.quit(getResource("EXIT_INVALID_INPUT"))
  710. Exit Function
  711. End If
  712. If CInt(year(arrDateTimeCheck(0))) => 9999 OR CInt(year(arrDateTimeCheck(0))) < 1601 then
  713. vbPrintf getResource("L_INVALID_ERRORMESSAGE_DATE_ERRORMESSAGE"), Array(arrDateTimeCheck(0), strDateTime)
  714. WScript.quit(getResource("EXIT_INVALID_INPUT"))
  715. Exit Function
  716. End If
  717. ' split the time to hour, minute and second. Check for bounds
  718. arrTemp = split(arrDateTimeCheck(1), ":",3,VBBinaryCompare)
  719. intHour = arrTemp(0)
  720. intMinute = arrTemp(1)
  721. intSecond = Left(arrTemp(2), (Len(arrTemp(2))-2)) ' remove the am or pm
  722. If ((CInt(intHour) < 1) OR (CInt(intHour) > 12) OR _
  723. (CInt(intMinute) < 0) OR (CInt(intMinute) > 59) OR _
  724. (CInt(intSecond) < 0) OR (CInt(intSecond) > 59)) Then
  725. vbPrintf getResource("L_INVALID_ERRORMESSAGE_TIME_ERRORMESSAGE"), Array(arrDateTimeCheck(1),strDateTime)
  726. WScript.Quit(getResource("EXIT_INVALID_INPUT"))
  727. Exit Function
  728. End If
  729. ' check if the given date an time are valid
  730. If IsDate(arrDateTimeCheck(0)) Then
  731. strTemp = TimeValue(arrDateTimeCheck(1))
  732. If Err.Number Then
  733. Err.Clear
  734. vbPrintf getResource("L_INVALID_ERRORMESSAGE_TIME_ERRORMESSAGE"), Array(arrDateTimeCheck(1),strDateTime)
  735. WScript.Quit(getResource("EXIT_INVALID_INPUT"))
  736. Exit Function
  737. Else
  738. validateDateTime = TRUE
  739. End If
  740. Else
  741. vbPrintf getResource("L_INVALID_ERRORMESSAGE_DATE_ERRORMESSAGE"), Array(arrDateTimeCheck(0), strDateTime)
  742. WScript.Quit(getResource("EXIT_INVALID_INPUT"))
  743. Exit Function
  744. End If
  745. End Function
  746. '********************************************************************
  747. '* Function: changeToWMIDateTime
  748. '*
  749. '* Purpose: To format the given date-time
  750. '*
  751. '* Input:
  752. '* [in] strDateTime the date-time string
  753. '* [in] strTimeZone the TimeZone of the Queried system
  754. '*
  755. '* Output: Returns the formatted date-time string
  756. '*
  757. '********************************************************************
  758. Function changeToWMIDateTime(ByVal strDateTime,strTimeZone)
  759. ON ERROR RESUME NEXT
  760. Err.Clear
  761. Dim arrDateTimeCheck ' to store the date-time values
  762. Dim strDate ' to store temporary date value
  763. Dim arrDate ' array to store date values(MMDDYYYY)
  764. Dim strMonth ' to store Month value
  765. Dim strYear ' to store Year value
  766. Dim strDay ' to store Day value
  767. Dim strTime ' to store temporary date value
  768. Dim arrTime ' array to store date values(MMDDYYYY)
  769. Dim i ' for looping
  770. ' input strDateTime is like "mm/dd/yy|yyyy,hh:mm:ssAM|PM"
  771. ' input Timezone is like "'+|-' UUU"
  772. arrDateTimeCheck = split(strDateTime,",")
  773. ' Finally format the input like "YYYYMMDDHHMMSS.000000+TIMEZONE"
  774. ' first format the month and day. Append the four digit year
  775. strDate = Left(arrDateTimeCheck(0),InStrRev(arrDateTimeCheck(0), "/")) & Year(arrDateTimeCheck(0))
  776. 'now date is mm/dd/yyyy
  777. arrDateTimeCheck(0) = strDate
  778. 'Spliting the array for month,day,year
  779. arrDate = split(arrDateTimeCheck(0) , "/" )
  780. ' The date, month must be of 2 digits
  781. ' If they are of single digit length < 2, append a "0"
  782. For i=0 to ubound(arrDate) - 1
  783. If Len(arrDate(i)) < 2 then
  784. arrDate(i) = "0" & arrdate(i)
  785. End If
  786. Next
  787. strMonth = arrDate(0)
  788. strDay = arrDate(1)
  789. strYear = arrDate(2)
  790. 'for 'YYYYMMDD' Pattern
  791. strDate = strYear & strMonth & strDay
  792. ' Take the Time for formating
  793. strTime = arrDateTimeCheck(1)
  794. 'NOW arrDateTimeCheck(1)="HH:MM:SSAM|PM".
  795. 'here formating Time 24Hours independent of Locale separator
  796. 'Spliting the array for HH MM SS
  797. arrTime = split(strTime , ":" )
  798. 'Looking for [A|P]M string
  799. If Instr(1,Lcase(arrTime(2)),Lcase("AM"),VBBinaryCompare) > 0 Then
  800. 'AM Conversion for 24H
  801. If arrTime(0) >= 12 Then
  802. arrTime(0) = arrTime(0) - 12
  803. End If
  804. Else
  805. 'PM Conversion for 24H
  806. If arrTime(0) < 12 Then
  807. arrTime(0) =arrTime(0) + 12
  808. End If
  809. End If
  810. 'Adding leading zero if third element is S[A|P]M
  811. If Len( arrTime(2)) = 3 then arrTime(2) = "0" & arrTime(2)
  812. 'Removing AM|PM from third element in the array
  813. arrTime(2) =Mid(arrTime(2),1,2)
  814. ' The hours, mins and secs must be of 2 digits
  815. ' If they are of single digit i.e Len < 2 , append a "0"
  816. For i=0 to ubound(arrTime)
  817. If Len(arrTime(i)) < 2 then
  818. arrTime(i) = "0" & arrTime(i)
  819. End If
  820. Next
  821. strTime = Join( arrTime ,"") ' formatting as HHMMSS
  822. ' Return the total format as "YYYYMMDDHHMMSS.000000+TIMEZONE"
  823. ChangeToWMIDateTime = strDate & strTime & ".000000" & strTimeZone
  824. End Function
  825. ]]>
  826. </script>
  827. </component>
  828. </package>