Advertisement
relay12

cmdlib.wsc (XP SP2 version)

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