Leaked source code of windows server 2003
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.

1051 lines
42 KiB

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