极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 3433|回复: 0

系统自带公共功能

[复制链接]
发表于 2015-4-17 18:57:55 | 显示全部楼层 |阅读模式
本帖最后由 editors 于 2015-4-17 18:59 编辑
  1. <?xml version="1.0" ?>
  2. <package>
  3.         <?component error="true" debug="true"?>
  4.         <component>
  5.                 <registration description="VBS Library" progid="Microsoft.CmdLib" version="1" classid="{6D335ADF-8270-4805-A044-2B6A09476396}">
  6.                 </registration>
  7.                 <public>
  8.                         <comment>
  9. ******************************************************************************
  10.          版权所有 (C) Microsoft Corporation。保留所有权利。
  11.        
  12.          模块名称:       CmdLib.wsc
  13.        
  14.          摘要:           此模块包含公共功能。
  15.        
  16. *******************************************************************************
  17. </comment>
  18.                         <method name="checkScript"/>
  19.                         <method name="vbPrintf"/>
  20.                         <method name="getHostName"/>
  21.                         <method name="getUserName"/>
  22.                         <method name="getDomainName"/>
  23.                         <method name="LengthinBytes"/>
  24.                         <method name="getPassword"/>
  25.                         <method name="trapError"/>
  26.                         <method name="getArguments"/>
  27.                         <method name="wmiConnect"/>
  28.                         <method name="packString"/>
  29.                         <method name="getMaxStringLen"/>
  30.                         <method name="showResults"/>
  31.                         <method name="validateDateTime"/>
  32.                         <method name="changeToWMIDateTime"/>
  33.                         <method name="matchPattern"/>
  34.                         <property name="ScriptingHost" internalName="WScript"/>
  35.                 </public>
  36.                 <resource id="PATTERN_VBPRINTF">%\d</resource>
  37.                 <resource id="L_INVALID_ERRORMESSAGE_TYPE_AS_INPUT">错误: 对函数传递了无效的类型作为输入。</resource>
  38.                 <resource id="L_INVALID_ERRORMESSAGE_ARG_NUMBER_AS_INPUT_ERRORMESSAGE">错误: 对 Print 函数传递了无效的参数数目。</resource>
  39.                 <resource id="TEXT_NA">暂缺</resource>
  40.                 <resource id="OBJ_SYSTEMINFO_CLASS">Win32_ComputerSystem</resource>
  41.                 <resource id="L_INVALID_ERRORMESSAGE">错误: 无效的 '%1'。</resource>
  42.                 <resource id="L_INVALID_SYNTAX_ERRORMESSAGE">错误: 无效语法。需要 '%v' 的值。</resource>
  43.                 <resource id="L_HELP_SYNTAX_MESSAGE">键入 "%1 /?" 了解用法信息。</resource>
  44.                 <resource id="HINT_CHECK_INPUT">请检查输入,然后再试一次。</resource>
  45.                 <resource id="L_ERROR_CHECK_VBSCRIPT_VERSION_ERRORMESSAGE">意外错误: 请检查当前 VBScript 的版本。</resource>
  46.                 <resource id="PATTERN_NEGATIVE_NUMBER">^\-\d|\d+$</resource>
  47.                 <resource id="CONST_NO_MATCHES_FOUND">0</resource>
  48.                 <resource id="OBJ_SCRIPTING_LOCATOR">WbemScripting.SWbemLocator</resource>
  49.                 <resource id="L_DISPLAY_FMT_TABLE_TEXT">TABLE</resource>
  50.                 <resource id="L_DISPLAY_FMT_CSV_TEXT">CSV</resource>
  51.                 <resource id="L_DISPLAY_FMT_LIST_TEXT">LIST</resource>
  52.                 <resource id="EXIT_SUCCESS">0</resource>
  53.                 <resource id="EXIT_INVALID_PARAM">999</resource>
  54.                 <resource id="EXIT_UNEXPECTED">255</resource>
  55.                 <resource id="EXIT_INVALID_INPUT">254</resource>
  56.                 <resource id="EXIT_METHOD_FAIL">250</resource>
  57.                 <resource id="L_INVALID_ERRORMESSAGE_TIME_ERRORMESSAGE">错误: 在筛选器 '%2' 中指定了无效的时间 '%h:%m'。</resource>
  58.                 <resource id="L_INVALID_ERRORMESSAGE_DATE_ERRORMESSAGE">错误: 在筛选器 '%2' 中指定了无效的日期 '%1'。</resource>
  59.                 <resource id="L_ENTER_"OBJxxxx"PASSWORD_TEXT">输入密码:OBJ</resource>
  60.                 <resource id="L_PROCESSING_TEXT">正在处理...</resource>
  61.                 <resource id="OBJ_SCRIPT_PASSWORD">ScriptPW.Password.1</resource>
  62.                 <resource id="L_HINT_CHECK_PASSWORD_DLL_MESSAGE">提示: 请检查是否在系统中AutoRegistration了 ScriptPW.dll。</resource>
  63.                 <resource id="CONST_ERROR">0</resource>
  64.                 <resource id="CONST_CSCRIPT">2</resource>
  65.                 <resource id="L_WARRING_LOCAL_CREDENTIALS_SUPPLIED_MESSAGE">警告: 为本地连接忽略用户凭据。</resource>
  66.                 <resource id="CONST_LOCAL_CREDENTIALS_SUPPLIED">-2147217308</resource>
  67.                 <script language="VBScript">
  68.                         <![CDATA[

  69.         ' All the functions which are used in common across all the vbs scripts are defined below

  70.         ' Function used to find whether CScript is used or not
  71.         '********************************************************************
  72.         '* Function: checkScript
  73.         '*
  74.         '* Purpose:  Determines which program is used to run this script.
  75.         '*
  76.         '* Input:    None
  77.         '*
  78.         '* Output:   intChkProgram is set to one of CONST_ERROR or CONST_CSCRIPT.
  79.         '*
  80.         '********************************************************************
  81.         Function checkScript()
  82.             ON ERROR RESUME NEXT
  83.             Err.Clear

  84.             Dim strFullName        'program with its full path - used to execute the script
  85.             Dim strCommand        'name of program without extension (like exe, Eg:CScript)
  86.             Dim intExe_Index        'to calculate the position of .exe in strFullName
  87.             Dim intSlash_Index        'to calculate the position of \ (slash) in strFullName

  88.             'strFullName should be something like C:\WINDOWS\COMMAND\CSCRIPT.EXE
  89.             strFullName = WScript.FullName

  90.             If Err.Number then
  91.                 Wscript.Echo "Error 0x" & CStr(Hex(Err.Number))
  92.                 If Err.Description <> "" Then
  93.                     Wscript.Echo "Error description: " & Err.Description & "."
  94.                 End If
  95.                 Err.Clear
  96.                 checkScript =   getResource("CONST_ERROR")
  97.                 Exit Function
  98.             End If

  99.             intExe_Index = InStr(1, strFullName, ".exe", 1)

  100.             If intExe_Index = 0 Then
  101.                 checkScript = getResource("CONST_ERROR")
  102.                 Exit Function
  103.             Else
  104.                 intSlash_Index = InStrRev(strFullName, "", intExe_Index, 1)

  105.                 If intSlash_Index = 0 Then
  106.                     checkScript =  getResource("CONST_ERROR")
  107.                     Exit Function
  108.                 Else
  109.                     strCommand = Mid(strFullName, intSlash_Index+1, _
  110.                                     intExe_Index-intSlash_Index-1)

  111.                     If LCase(strCommand) = LCase("cscript") Then
  112.                         checkScript =  getResource("CONST_CSCRIPT")
  113.                     Else
  114.                         checkScript = getResource("CONST_ERROR")
  115.                     End If

  116.                 End If  'If intSlash_Index = 0 Then

  117.             End If      'If intExe_Index = 0 Then

  118.         End Function

  119.         ' Subroutine which implements normal printf functionality
  120.         '********************************************************************
  121.         '* Sub:     vbPrintf
  122.         '*
  123.         '* Purpose: Simulates the Printf function.
  124.         '*
  125.         '* Input:  [in]  strPhrase      the string with '%1 %2 &3 ' in it
  126.         '*         [in]  args           the values to replace '%1 %2 ..etc' with
  127.         '*
  128.         '* Output:  Displays the string on the screen
  129.         '*          (All the '%x' variables in strPhrase is replaced by the
  130.         '*           corresponding elements in the array)
  131.         '*
  132.         '********************************************************************
  133.         Sub vbPrintf(ByVal strPhrase, ByVal args )

  134.             ON ERROR RESUME NEXT
  135.             Err.Clear

  136.             'Changed for localization  

  137.             Dim strMatchPattern         ' the pattern to match - '%[number]'
  138.             Dim intValuesCount          ' to get the count of matching results
  139.             Dim i                       ' used in the loop
  140.             Dim strTemp                 ' to store temporally  the given input string  for formatting

  141.             strTemp   = strPhrase

  142.             ' look out for '%[number]' in the given string
  143.             strMatchPattern = getResource("PATTERN_VBPRINTF") '"\%[number]"

  144.             intValuesCount = matchPattern (strMatchPattern, strTemp)

  145.             If intValuesCount <> 0 Then
  146.                 ' if present then replace '%1 %2 %3' in the string by
  147.                 ' corresponding element in the given array

  148.                 If Not IsArray(args) Then
  149.                     WScript.Echo getResource("L_INVALID_ERRORMESSAGE_TYPE_AS_INPUT")
  150.                     WScript.Quit getResource("EXIT_INVALID_PARAM")
  151.                 End If
  152.                
  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.             
  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: strPackString
  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.             packString = getResource("TEXT_NA") & Space(intWidth-3)
  480.             Exit Function
  481.         End If
  482.         
  483.         If intWidth >= LengthinBytes(strString) Then
  484.             packString = strString & Space(intWidth-LengthinBytes(strString))
  485.         Else
  486.             ' truncate the string
  487.             packString = Left(strString, intWidth)
  488.         End If

  489.     End Function

  490.     ' Function used to get length of the maximum length string in an array of strings
  491.     '**************************************************************************
  492.     '* Function: getMaxStringLength
  493.     '*
  494.     '* Purpose:  To get the length of longest string in the given array
  495.     '*
  496.     '* Input:    [in] arrStrings    an array of strings
  497.     '*
  498.     '* Output:   Returns length of longest string in the array
  499.     '*           If error in input, displays message and quits
  500.     '*
  501.     '**************************************************************************

  502.     Function getMaxStringLen(ByVal arrStrings)
  503.         ON ERROR RESUME NEXT
  504.         Err.Clear

  505.         Dim intMaxLength   ' to store the maximum length of the string
  506.         Dim intArrCount    ' used in the loop

  507.         intMaxLength = 0
  508.         ' quit if input is not an array
  509.         If NOT IsArray(arrStrings) Then
  510.             WScript.Echo getResource("L_INVALID_ERRORMESSAGE_TYPE_AS_INPUT")
  511.             WScript.Quit(getResource("EXIT_INVALID_PARAM"))
  512.         End If

  513.         ' check for length of each element in the array
  514.         For intArrCount = 0 To UBound(arrStrings)
  515.             If LengthinBytes(arrStrings(intArrCount)) > intMaxLength Then
  516.                 intMaxLength = LengthinBytes(arrStrings(intArrCount))
  517.             End If
  518.         Next
  519.         getMaxStringLen = intMaxLength
  520.     End Function

  521.     ' Function used to get length of actual bytes required by the string.
  522.     '**************************************************************************
  523.     '* Function: LengthinBytes
  524.     '*
  525.     '* Purpose:  To get the length of a string in Bytes.
  526.     '*
  527.     '* Input:    [in] strString    a String
  528.     '*
  529.     '* Output:   Returns length of a string in Bytes.
  530.     '*
  531.     '**************************************************************************

  532.     Function LengthinBytes(ByVal strString)
  533.         Dim i, strChar
  534.         LengthinBytes = 0
  535.         For i =1 To Len(strString)
  536.             strChar = Mid(strString, i, 1)
  537.             If Asc(strChar) > 255 OR Asc(strChar) < 0 Then
  538.                 LengthinBytes = LengthinBytes + 2
  539.                 Else
  540.                     LengthinBytes = LengthinBytes + 1
  541.                 End If
  542.         Next
  543.     End Function


  544.   ' Function used to show results in the desired format
  545.     '**************************************************************************
  546.     '* Function: showResults
  547.     '*
  548.     '* Purpose:  To show results in the desired format
  549.     '*
  550.     '* Input:   
  551.     '*        [in] arrHeader        an array of strings containing all the headers
  552.     '*        [in] arrResultsArray  array containing all the records
  553.     '*        [in] strFormat        CSV or LIST or TABLE
  554.     '*        [in] blnPrintHeader   Boolean value indicating whether header
  555.     '*                              should be printed or not
  556.     '*        [in] arrBlnHide       an array containing boolean values. Each value
  557.     '*                              indicates whether a particular value in a record
  558.     '*                              is to be displayed or not
  559.     '*
  560.     '* Output:   Displays all the records in the required format
  561.     '*
  562.     '**************************************************************************
  563.     Sub showResults( ByVal arrHeader,       _
  564.                      ByVal arrResultsArray, _
  565.                      ByVal arrMaxLength,    _
  566.                      ByVal strFormat,       _
  567.                      ByVal blnPrintHeader,  _
  568.                      ByVal arrBlnHide       )

  569.         ON ERROR RESUME NEXT
  570.         Err.Clear

  571.         Dim i, j                   ' used as loop variables
  572.         Dim intTestResult          ' to store temporary results
  573.         Dim intMaxHeaderLength     ' to store length of longest column header
  574.         Dim strPackedString        ' to store the padded/truncated string
  575.         Dim arrResults             ' to store the row to display(which is an array)
  576.         Dim intColumnCount         ' to store the count for no.of columns

  577.         ' get the maximum length of all the header names given
  578.         intMaxHeaderLength = getMaxStringLen(arrHeader)

  579.         ' initialize the values
  580.         intColumnCount = UBound(arrHeader)
  581.         intTestResult  = 0

  582.         Select Case LCase(strFormat)

  583.             Case LCase(getResource("L_DISPLAY_FMT_LIST_TEXT"))
  584.                ' If LIST format is specified
  585.                For i = 0 to UBound(arrResultsArray)
  586.                     arrResults = arrResultsArray(i)
  587.                     For j =  0 to UBound(arrResults)
  588.                         If arrBlnHide(j) = 0 Then
  589.                             intTestResult = arrHeader(j) & ":"
  590.                             strPackedString = packString(intTestResult, intMaxHeaderLength+1)
  591.                             WScript.Echo strPackedString & " " & arrResults(j)
  592.                         End If
  593.                     Next
  594.                     ' print an empty line
  595.                     WScript.Echo ""
  596.              Next

  597.             Case LCase(getResource("L_DISPLAY_FMT_CSV_TEXT"))
  598.                 ' If CSV format is specified
  599.                 If blnPrintHeader Then
  600.                         strPackedString = ""
  601.                         ' first print the header , if not already printed
  602.                         For i = 0 to UBound(arrHeader)
  603.                             If arrBlnHide(i) = 0 Then
  604.                                 intTestResult = InStr(1,arrHeader(i), ",", VBBinaryCompare)
  605.                                 If intTestResult > 0 Then
  606.                                     arrHeader(i) = chr(34) & arrHeader(i) & chr(34)
  607.                                 Else
  608.                                   arrHeader(i) = chr(34) & arrHeader(i) & chr(34)
  609.                                 End If

  610.                                 strPackedString = strPackedString & arrHeader(i)

  611.                                 If (i+1) <= intColumnCount Then
  612.                                     strPackedString = strPackedString & ","
  613.                                 End If
  614.                             End If
  615.                         Next
  616.                         WScript.Echo strPackedString
  617.                 End If

  618.                 ' print all the comma separated values
  619.                 For i = 0 to UBound(arrResultsArray)
  620.                     arrResults = arrResultsArray(i)
  621.                     strPackedString = ""
  622.                     For j =  0 to UBound(arrResults)
  623.                        If arrBlnHide(j) = 0 Then
  624.                         intTestResult = InStr(1,arrResults(j), ",", VBBinaryCompare)

  625.                         If intTestResult > 0 Then
  626.                             strPackedString = strPackedString & chr(34) & arrResults(j) & chr(34)
  627.                         Else
  628.                             strPackedString = strPackedString & chr(34) & arrResults(j) & chr(34)
  629.                         End If
  630.                         
  631.                         If (j+1) <= intColumnCount Then
  632.                             strPackedString = strPackedString & ","
  633.                           '  strPackedString = strPackedString & chr(34) & "," & chr(34)
  634.                         End If
  635.                     End If
  636.                     Next
  637.                     WScript.Echo strPackedString
  638.                     strPackedString = ""
  639.                 Next

  640.             Case LCase(getResource("L_DISPLAY_FMT_TABLE_TEXT"))
  641.                 ' If table format is asked for
  642.                 If blnPrintHeader Then
  643.                     strPackedString = ""
  644.                     ' print the header, if not already printed
  645.                     For i = 0 to UBound(arrHeader)
  646.                     If arrBlnHide(i) = 0 Then
  647.                         strPackedString = strPackedString & " " & _
  648.                                             packString(arrHeader(i), _
  649.                                             arrMaxLength(i))
  650.                     End If
  651.                     Next

  652.                     WScript.Echo strPackedString
  653.                     strPackedString = ""
  654.                     ' print the Underline to the column header
  655.                     For i =  0 to UBound(arrHeader)
  656.                         If arrBlnHide(i) = 0 Then
  657.                             strPackedString = strPackedString & " " & _
  658.                                 packString(String(arrMaxLength(i),"-"), arrMaxLength(i))
  659.                         End If
  660.                     Next   

  661.                     WScript.Echo strPackedString
  662.                 End If

  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.                         strPackedString = strPackedString & " " & _
  669.                                        packString(arrResults(j), _
  670.                                        arrMaxLength(j))
  671.                      End If
  672.                     Next
  673.                     WScript.Echo strPackedString
  674.                 Next
  675.         End Select

  676.     End Sub

  677.    
  678.     '********************************************************************
  679.     '* Function: strDateTime
  680.     '*
  681.     '* Purpose:  To validate the date-time format specified
  682.     '*
  683.     '* Input:
  684.     '*           [in] strDateTime     the date-time string
  685.     '*
  686.     '* Output:   Returns true if valid format
  687.     '*           Else displays error message and quits
  688.     '*
  689.     '********************************************************************
  690.      Function validateDateTime(ByVal strDateTime)
  691.         ON ERROR RESUME NEXT
  692.         Err.Clear

  693.         validateDateTime = False

  694.         Dim arrDateTimeCheck    ' to store the date and time values
  695.         Dim intMonth            ' to store the month(instead of array(subscript))
  696.         Dim intDay              ' to store the day(instead of array(subscript))
  697.         Dim intYear             ' to store the year(instead of array(subscript))
  698.         Dim strTemp             ' to store temporary values
  699.         Dim arrTemp             ' to store temporary values when split is used
  700.         Dim intHour             ' to store the Hour(instead of array(subscript))
  701.         Dim intMinute           ' to store the Minutes(instead of array(subscript))
  702.         Dim intSecond           ' to store the Seconds(instead of array(subscript))

  703.         ' strDateTime is of the format "mm/dd/yy|yyyy,hh:mm:ssPM"
  704.         ' first split at the comma and separate date and time
  705.         arrDateTimeCheck = split(strDateTime, ",",2,VBBinaryCompare)

  706.         ' split the date and check if the month and day are in bounds
  707.         arrTemp = split(arrDateTimeCheck(0), "/",3,VBBinaryCompare)

  708.         intMonth = arrTemp(0)
  709.         intDay   = arrTemp(1)
  710.         intYear  = arrTemp(2)

  711.         If ((CInt(intMonth) < 1) OR (CInt(intMonth) > 12) OR (CInt(intDay) < 1) OR (CInt(intDay) > 31)) Then
  712.             vbPrintf getResource("L_INVALID_ERRORMESSAGE_DATE_ERRORMESSAGE"), Array(arrDateTimeCheck(0), strDateTime)
  713.             WScript.quit(getResource("EXIT_INVALID_INPUT"))
  714.             Exit Function
  715.         End If

  716.         If CInt(year(arrDateTimeCheck(0))) => 9999 OR CInt(year(arrDateTimeCheck(0))) < 1601 then
  717.                 vbPrintf getResource("L_INVALID_ERRORMESSAGE_DATE_ERRORMESSAGE"), Array(arrDateTimeCheck(0), strDateTime)
  718.                 WScript.quit(getResource("EXIT_INVALID_INPUT"))
  719.                 Exit Function
  720.         End If

  721.         ' split the time to hour, minute and second. Check for bounds
  722.         arrTemp = split(arrDateTimeCheck(1), ":",3,VBBinaryCompare)

  723.         intHour   = arrTemp(0)
  724.         intMinute = arrTemp(1)
  725.         intSecond = Left(arrTemp(2), (Len(arrTemp(2))-2)) ' remove the am or pm

  726.         If ((CInt(intHour) < 1) OR (CInt(intHour) > 12)     OR _
  727.             (CInt(intMinute) < 0) OR (CInt(intMinute) > 59) OR _
  728.             (CInt(intSecond) < 0) OR (CInt(intSecond) > 59)) Then
  729.                 vbPrintf getResource("L_INVALID_ERRORMESSAGE_TIME_ERRORMESSAGE"), Array(arrDateTimeCheck(1),strDateTime)
  730.                 WScript.Quit(getResource("EXIT_INVALID_INPUT"))
  731.                 Exit Function
  732.        End If

  733.        ' check if the given date an time are valid
  734.         If IsDate(arrDateTimeCheck(0)) Then
  735.             strTemp = TimeValue(arrDateTimeCheck(1))
  736.             If Err.Number Then
  737.                 Err.Clear
  738.                 vbPrintf getResource("L_INVALID_ERRORMESSAGE_TIME_ERRORMESSAGE"), Array(arrDateTimeCheck(1),strDateTime)
  739.                 WScript.Quit(getResource("EXIT_INVALID_INPUT"))
  740.                 Exit Function
  741.             Else
  742.                 validateDateTime = TRUE
  743.             End If
  744.         Else
  745.             vbPrintf getResource("L_INVALID_ERRORMESSAGE_DATE_ERRORMESSAGE"), Array(arrDateTimeCheck(0), strDateTime)
  746.             WScript.Quit(getResource("EXIT_INVALID_INPUT"))
  747.             Exit Function
  748.         End If
  749.     End Function

  750.     '********************************************************************
  751.     '* Function: changeToWMIDateTime
  752.     '*
  753.     '* Purpose:  To format the given date-time
  754.     '*
  755.     '* Input:   
  756.     '*        [in] strDateTime     the date-time string
  757.     '*        [in] strTimeZone    the TimeZone  of the Queried system
  758.     '*
  759.     '* Output:   Returns the formatted date-time string
  760.     '*
  761.     '********************************************************************
  762.      Function changeToWMIDateTime(ByVal strDateTime,strTimeZone)
  763.         ON ERROR RESUME NEXT
  764.         Err.Clear

  765.         Dim arrDateTimeCheck  ' to store the date-time values
  766.         Dim strDate           ' to store temporary date value
  767.         Dim arrDate           ' array to store date values(MMDDYYYY)
  768.         Dim strMonth          ' to store Month value
  769.         Dim strYear           ' to store Year value
  770.         Dim strDay            ' to store Day  value
  771.         Dim strTime           ' to store temporary date value
  772.         Dim arrTime           ' array to store date values(MMDDYYYY)
  773.         Dim i                 ' for looping

  774.         ' input strDateTime is like "mm/dd/yy|yyyy,hh:mm:ssAM|PM"
  775.         ' input Timezone is like "'+|-' UUU"

  776.         arrDateTimeCheck = split(strDateTime,",")
  777.         ' Finally format the  input like "YYYYMMDDHHMMSS.000000+TIMEZONE"

  778.         ' first format the month and day. Append the four digit year
  779.         strDate = Left(arrDateTimeCheck(0),InStrRev(arrDateTimeCheck(0), "/")) & Year(arrDateTimeCheck(0))

  780.         'now date is mm/dd/yyyy
  781.         arrDateTimeCheck(0) = strDate

  782.         'Spliting the array for month,day,year
  783.         arrDate = split(arrDateTimeCheck(0) , "/" )

  784.         ' The date, month  must be of 2 digits
  785.         ' If they are of single digit length < 2, append a "0"
  786.         For i=0 to ubound(arrDate) - 1
  787.             If Len(arrDate(i)) < 2 then
  788.                 arrDate(i) = "0" & arrdate(i)
  789.             End If
  790.         Next

  791.         strMonth = arrDate(0)
  792.         strDay   = arrDate(1)
  793.         strYear  = arrDate(2)

  794.         'for 'YYYYMMDDW' Pattern
  795.         strDate = strYear & strMonth & strDay & strWeek        

  796.         ' Take the Time for formating
  797.         strTime  =  arrDateTimeCheck(1)

  798.         'NOW arrDateTimeCheck(1)="HH:MM:SSAM|PM".
  799.         'here formating Time 24Hours independent of Locale separator  

  800.         'Spliting the array for HH MM SS
  801.         arrTime = split(strTime , ":" )           

  802.         'Looking for [A|P]M string
  803.        If  Instr(1,Lcase(arrTime(2)),Lcase("AM"),VBBinaryCompare) > 0 Then
  804.                    'AM Conversion  for 24H
  805.                If  arrTime(0) >=  12 Then
  806.                     arrTime(0) = arrTime(0) - 12
  807.                End If        

  808.         Else
  809.                     'PM Conversion for 24H
  810.                If  arrTime(0)  < 12 Then
  811.                        arrTime(0) =arrTime(0) + 12
  812.                End If

  813.        End If

  814.         'Adding leading zero  if third element  is  S[A|P]M
  815.         If Len( arrTime(2)) = 3 then   arrTime(2)  = "0" & arrTime(2)  

  816.         'Removing  AM|PM from  third  element in the  array
  817.         arrTime(2) =Mid(arrTime(2),1,2)

  818.         ' The hours, mins and secs must be of 2 digits
  819.         ' If they are of single digit i.e Len < 2 , append a "0"
  820.         For i=0 to ubound(arrTime)
  821.                 If Len(arrTime(i)) < 2 then
  822.                        arrTime(i) = "0" & arrTime(i)
  823.                 End If
  824.         Next

  825.         strTime = Join( arrTime ,"") ' formatting as HHMMSS

  826.         ' Return the total format as "YYYYMMDDHHMMSS.1MMMMM+TIMEZONE"
  827.          ChangeToWMIDateTimeMS = strDate & strTime & ".1MMMMM" & strTimeZone

  828. End Function

  829.     ]]>
  830.                 </script>
  831.         </component>
  832. </package>
复制代码
这个code更新了......
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-18 22:39 , Processed in 0.036453 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表