home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 May / W2KPRK.iso / ras.cab / share.vbs < prev    next >
Text File  |  1999-11-04  |  38KB  |  1,043 lines

  1. '********************************************************************
  2. '*
  3. '* File:           Share.vbs
  4. '* Created:        March 1999
  5. '* Version:        1.0
  6. '*
  7. '*  Main Function:  Lists, creates, or deletes shares from a machine.
  8. '*
  9. '*    1.  Share.vbs /L [/S <server>] [/U <username>]
  10. '*                 [/W <password>]
  11. '*                 [/O <outputfile>]
  12. '*    2.  Share.vbs /C /N <name> /P <path> [/T <type>] [/V <description>]
  13. '*                 [/S <server>] [/U <username>] [/W <password>]
  14. '*                 [/O <outputfile>]
  15. '*    3.  Share.vbs /D /N <name>
  16. '*                 [/S <server>] [/U <username>] [/W <password>]
  17. '*                 [/O <outputfile>]
  18. '*
  19. '* Copyright (C) 1999 Microsoft Corporation
  20. '*
  21. '********************************************************************
  22.  
  23. OPTION EXPLICIT
  24.  
  25.     'Define constants
  26.     CONST CONST_ERROR                   = 0
  27.     CONST CONST_WSCRIPT                 = 1
  28.     CONST CONST_CSCRIPT                 = 2
  29.     CONST CONST_SHOW_USAGE              = 3
  30.     CONST CONST_PROCEED                 = 4
  31.     CONST CONST_DELETE                  = "DELETE"
  32.     CONST CONST_LIST                    = "LIST"
  33.     CONST CONST_CREATE                  = "CREATE"
  34.     
  35.     'Declare variables
  36.     Dim intOpMode, i
  37.     Dim strServer, strUserName, strPassword, strOutputFile
  38.     Dim strTaskCommand, strShareName, strSharePath
  39.     Dim strShareComment, strShareType
  40.  
  41.     'Make sure the host is csript, if not then abort
  42.     VerifyHostIsCscript()
  43.  
  44.     'Parse the command line
  45.     intOpMode = intParseCmdLine(strServer       , _
  46.                                 strUserName     , _
  47.                                 strPassword     , _
  48.                                 strOutputFile   , _
  49.                                 strTaskCommand  , _
  50.                                 strShareName    , _
  51.                                 strSharePath    , _
  52.                                 strShareType    , _
  53.                                 strShareComment   )
  54.  
  55.  
  56.     Select Case intOpMode
  57.  
  58.         Case CONST_SHOW_USAGE
  59.             Call ShowUsage()
  60.  
  61.         Case CONST_PROCEED        
  62.             Call Share(strServer       , _
  63.                        strUserName     , _
  64.                        strPassword     , _
  65.                        strOutputFile   , _
  66.                        strTaskCommand  , _
  67.                        strShareName    , _
  68.                        strSharePath    , _
  69.                        strShareType    , _
  70.                        strShareComment   )
  71.  
  72.         Case CONST_ERROR
  73.             Call Wscript.Echo("Error occurred in passing parameters.")
  74.  
  75.         Case Else                    'Default -- should never happen
  76.             Call Wscript.Echo("Error occurred in passing parameters.")
  77.  
  78.     End Select
  79.  
  80. '********************************************************************
  81. '* End of Script
  82. '********************************************************************
  83.  
  84. '********************************************************************
  85. '*
  86. '* Sub Share()
  87. '* Purpose:    Lists, creates, or deletes shares from a machine.
  88. '* Input:    strServer       name of the machine to be checked
  89. '*          strTaskCommand one of list, create, and delete
  90. '*          strShareName    name of the share to be created or deleted
  91. '*          strSharePath    path of the share to be created
  92. '*          strShareType    type of the share to be created
  93. '*          strShareComment a comment for the share to be created
  94. '*            strUserName        the current user's name
  95. '*            strPassword        the current user's password
  96. '*            strOutputFile    an output file name
  97. '* Output:    Results are either printed on screen or saved in strOutputFile.
  98. '*
  99. '********************************************************************
  100.  
  101. Private Sub Share(strServer, strUserName, strPassword, strOutputFile, _
  102.     strTaskCommand,strShareName,strSharePath,strShareType,strShareComment )
  103.  
  104.     ON ERROR RESUME NEXT
  105.  
  106.     Dim objFileSystem, objOutputFile, objService, strQuery
  107.  
  108.  
  109.    'Open a text file for output if the file is requested
  110.     If Not IsEmpty(strOutputFile) Then
  111.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  112.             Call Wscript.Echo ("Could not open an output file.")
  113.             Exit Sub
  114.         End If
  115.     End If
  116.  
  117.     'Establish a connection with the server.
  118.     If blnConnect("root\cimv2" , _
  119.                    strUserName , _
  120.                    strPassword , _
  121.                    strServer   , _
  122.                    objService  ) Then
  123.         Call Wscript.Echo("")
  124.         Call Wscript.Echo("Please check the server name, " _
  125.                         & "credentials and WBEM Core.")
  126.         Exit Sub
  127.     End If
  128.  
  129.     'Now execute the method.
  130.     Call ExecuteMethod(objService, objOutputFile,strTaskCommand, _
  131.          strShareName,strSharePath,strShareType,strShareComment)
  132.  
  133.     If IsObject(objOutputFile) Then
  134.         objOutputFile.Close
  135.         Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
  136.     End If
  137.  
  138. End Sub
  139.  
  140. '********************************************************************
  141. '*
  142. '* Sub ExecMethod()
  143. '* Purpose:    Executes a method: creation, deletion, or listing.
  144. '* Input:    objService        a service object
  145. '*            objOutputFile    an output file object
  146. '*          strTaskCommand one of list, create, and delete
  147. '*          strShareName    name of the share to be created or deleted
  148. '*          strSharePath    path of the share to be created
  149. '*          strShareType    type of the share to be created
  150. '*          strShareComment a comment for the share to be created
  151. '* Output:    Results are either printed on screen or saved in objOutputFile.
  152. '*
  153. '********************************************************************
  154.  
  155. Private Sub ExecuteMethod(objService, objOutputFile, strTaskCommand, _
  156.             strShareName, strSharePath,strShareType,strShareComment)
  157.  
  158.     ON ERROR RESUME NEXT
  159.  
  160.     Dim intType, intShareType, i, intStatus, strMessage
  161.     Dim objEnumerator, objInstance
  162.     ReDim strName(0), strDescription(0), strPath(0),strType(0), intOrder(0)
  163.  
  164.     intShareType = 0
  165.     strMessage = ""
  166.     strName(0) = ""
  167.     strPath(0) = ""
  168.     strDescription(0) = ""
  169.     strType(0) = ""
  170.     intOrder(0) = 0
  171.  
  172.     Select Case strTaskCommand
  173.         Case CONST_CREATE
  174.             Set objInstance = objService.Get("Win32_Share")
  175.             If Err.Number Then
  176.                 Print "Error 0x" & CStr(Hex(Err.Number)) & _
  177.                   " occurred in getting " & " a share object."
  178.                 If Err.Description <> "" Then
  179.                      Print "Error description: " & Err.Description & "."
  180.                 End If
  181.                 Err.Clear
  182.                 Exit Sub
  183.             End If
  184.  
  185.             If objInstance is nothing Then
  186.                 Exit Sub
  187.             Else
  188.                 Select Case strShareType
  189.                     Case "Disk"
  190.                         intShareType = 0
  191.                     Case "PrinterQ"
  192.                         intShareType = 1
  193.                     Case "Device"
  194.                         intShareType = 2
  195.                     Case "IPC"
  196.                         intShareType = 3
  197.                     Case "Disk$"
  198.                         intShareType = -2147483648
  199.                     Case "PrinterQ$"
  200.                         intShareType = -2147483647
  201.                     Case "Device$"
  202.                         intShareType = -2147483646
  203.                     Case "IPC$"
  204.                         intShareType = -2147483645
  205.                 End Select
  206.  
  207.                 intStatus = objInstance.Create(strSharePath, strShareName, _
  208.                     intShareType, null, strShareComment, null, null)
  209.                 If intStatus = 0 Then
  210.                     strMessage = "Succeeded in creating share " & _
  211.                       strShareName & "."
  212.                 Else
  213.                     strMessage = "Failed to create share " & strShareName & "."
  214.                     strMessage = strMessage & vbCRLF & "Status = " & _
  215.                       intStatus & "."
  216.                 End If
  217.  
  218.                 WriteLine strMessage, objOutputFile
  219.                 i = i + 1
  220.             End If
  221.         Case CONST_DELETE
  222.             Set objInstance = objService.Get("Win32_Share='" & strShareName _
  223.               & "'")
  224.             If Err.Number Then
  225.                 Print "Error 0x" & CStr(Hex(Err.Number)) & _
  226.                   " occurred in getting share " _
  227.                     & strShareName & "."
  228.                 If Err.Description <> "" Then
  229.                     Print "Error description: " & Err.Description & "."
  230.                 End If
  231.                 Err.Clear
  232.                 Exit Sub
  233.             End If
  234.  
  235.             If objInstance is nothing Then
  236.                 Exit Sub
  237.             Else
  238.                 intStatus = objInstance.Delete()
  239.                 If intStatus = 0 Then
  240.                     strMessage = "Succeeded in deleting share " & _
  241.                     strShareName & "."
  242.                 Else
  243.                     strMessage = "Failed to delete share " & strShareName & "."
  244.                     strMessage = strMessage & vbCRLF & "Status = " & _
  245.                     intStatus & "."
  246.                 End If
  247.                 WriteLine strMessage, objOutputFile
  248.                 i = i + 1
  249.             End If
  250.         Case CONST_LIST
  251.             Set objEnumerator = objService.ExecQuery (_
  252.                 "Select Name,Description,Path,Type From Win32_Share",,0)
  253.             If Err.Number Then
  254.                 Print "Error 0x" & CStr(Hex(Err.Number)) & _
  255.                   " occurred during the query."
  256.                 If Err.Description <> "" Then
  257.                     Print "Error description: " & Err.Description & "."
  258.                 End If
  259.                 Err.Clear
  260.                 Exit Sub
  261.             End If
  262.             Call WriteLine("There are " & objEnumerator.Count & _
  263.               " shares.", objOutputFile)
  264.             Call WriteLine("",objOutputFile)
  265.             For Each objInstance in objEnumerator
  266.                 I = I + 1
  267.                 If objInstance is nothing Then
  268.                     Exit Sub
  269.                 End If
  270.                 Call WriteLine("Name        : " & objInstance.Name, _
  271.                      objOutputFile)
  272.                 Select Case objInstance.Type
  273.                     Case 0
  274.                         Call WriteLine("Type        : " & "Disk", _
  275.                              objOutputFile)
  276.                     Case 1
  277.                         Call WriteLine("Type        : " & "PrinterQ", _
  278.                              objOutputFile)
  279.                     Case 2
  280.                         Call WriteLine("Type        : " & "Device", _
  281.                              objOutputFile)
  282.                     Case 3
  283.                         Call WriteLine("Type        : " & "IPC", _
  284.                              objOutputFile)
  285.                     Case -2147483648
  286.                         Call WriteLine("Type        : " & "Disk$", _
  287.                              objOutputFile)
  288.                     Case -2147483647
  289.                         Call WriteLine("Type        : " & "PrinterQ$", _
  290.                              objOutputFile)
  291.                     Case -2147483646
  292.                         Call WriteLine("Type        : " & "Device$", _
  293.                              objOutputFile)
  294.                     Case -2147483645
  295.                         Call WriteLine("Type        : " & "IPC$", _
  296.                              objOutputFile)
  297.                     Case else
  298.                         Call WriteLine("Type        : " & "Unknown", _
  299.                              objOutputFile)
  300.                         strType (i) = "Unknown"
  301.                 End Select
  302.                 Call WriteLine("Description : " & _
  303.                      objInstance.Description, objOutputFile)
  304.                 Call WriteLine("Path        : " & _
  305.                      objInstance.Path, objOutputFile)
  306.                 Call WriteLine("", objOutputFile)
  307.             Next
  308.  
  309.             If i > 0 Then
  310.  
  311.             Else
  312.                 strMessage = "No share is found."
  313.                 WriteLine strMessage, objOutputFile
  314.            End If
  315.     End Select
  316.  
  317. End Sub
  318.  
  319. '********************************************************************
  320. '*
  321. '* Function intParseCmdLine()
  322. '*
  323. '* Purpose: Parses the command line.
  324. '* Input:   
  325. '*
  326. '* Output:  strServer         a remote server ("" = local server")
  327. '*          strUserName       the current user's name
  328. '*          strPassword       the current user's password
  329. '*          strOutputFile     an output file name
  330. '*
  331. '********************************************************************
  332. Private Function intParseCmdLine(strServer       , _
  333.                                  strUserName     , _
  334.                                  strPassword     , _
  335.                                  strOutputFile   , _
  336.                                  strTaskCommand  , _
  337.                                  strShareName    , _
  338.                                  strSharePath    , _
  339.                                  strShareType    , _
  340.                                  strShareComment   )
  341.  
  342.     ON ERROR RESUME NEXT
  343.  
  344.     Dim strFlag
  345.     Dim intState, intArgIter
  346.     Dim objFileSystem
  347.  
  348.     If Wscript.Arguments.Count > 0 Then
  349.         strFlag = Wscript.arguments.Item(0)
  350.     End If
  351.  
  352.     If IsEmpty(strFlag) Then                'No arguments have been received
  353.         intParseCmdLine = CONST_PROCEED
  354.         strTaskCommand = CONST_LIST
  355.         Exit Function
  356.     End If
  357.  
  358.     'Check if the user is asking for help or is just confused
  359.     If (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
  360.         OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "?") _ 
  361.         OR (strFlag="h") Then
  362.         intParseCmdLine = CONST_SHOW_USAGE
  363.         Exit Function
  364.     End If
  365.  
  366.     'Retrieve the command line and set appropriate variables
  367.      intArgIter = 0
  368.     Do While intArgIter <= Wscript.arguments.Count - 1
  369.         Select Case Left(LCase(Wscript.arguments.Item(intArgIter)),2)
  370.   
  371.             Case "/s"
  372.                 If Not blnGetArg("Server", strServer, intArgIter) Then
  373.                     intParseCmdLine = CONST_ERROR
  374.                     Exit Function
  375.                 End If
  376.                 intArgIter = intArgIter + 1
  377.  
  378.             Case "/o"
  379.                 If Not blnGetArg("Output File", strOutputFile, intArgIter) Then
  380.                     intParseCmdLine = CONST_ERROR
  381.                     Exit Function
  382.                 End If
  383.                 intArgIter = intArgIter + 1
  384.  
  385.             Case "/u"
  386.                 If Not blnGetArg("User Name", strUserName, intArgIter) Then
  387.                     intParseCmdLine = CONST_ERROR
  388.                     Exit Function
  389.                 End If
  390.                 intArgIter = intArgIter + 1
  391.  
  392.             Case "/w"
  393.                 If Not blnGetArg("User Password", strPassword, intArgIter) Then
  394.                     intParseCmdLine = CONST_ERROR
  395.                     Exit Function
  396.                 End If
  397.                 intArgIter = intArgIter + 1
  398.  
  399.             Case "/l"
  400.                 intParseCmdLine = CONST_PROCEED
  401.                 strTaskCommand = CONST_LIST
  402.                 intArgITer = intArgITer + 1
  403.                
  404.             Case "/c"
  405.                 intParseCmdLine = CONST_PROCEED
  406.                 strTaskCommand = CONST_CREATE
  407.                 intArgITer = intArgITer + 1
  408.  
  409.             Case "/d"
  410.                 intParseCmdLine = CONST_PROCEED
  411.                 strTaskCommand = CONST_DELETE
  412.                 intArgITer = intArgITer + 1
  413.  
  414.             Case "/n"
  415.                 If Not blnGetArg("Share Name", strShareName, intArgIter) Then
  416.                     intParseCmdLine = CONST_ERROR
  417.                     Exit Function
  418.                 End If
  419.                 intArgITer = intArgITer + 1
  420.  
  421.             Case "/p"
  422.                 If Not blnGetArg("Share Path", strSharePath, intArgIter) Then
  423.                     intParseCmdLine = CONST_ERROR
  424.                     Exit Function
  425.                 End If
  426.                 intArgITer = intArgITer + 1
  427.  
  428.             Case "/t"
  429.                 If Not blnGetArg("Share Type", strShareType, intArgIter) Then
  430.                     intParseCmdLine = CONST_ERROR
  431.                     Exit Function
  432.                 End If
  433.                 intArgITer = intArgITer + 1
  434.  
  435.             Case "/v"
  436.                 If Not blnGetArg("Share Description", strShareComment, _
  437.                     intArgIter) Then
  438.                     intParseCmdLine = CONST_ERROR
  439.                     Exit Function
  440.                 End If
  441.                 intArgITer = intArgITer + 1
  442.  
  443.             Case Else 'We shouldn't get here
  444.                 Call Wscript.Echo("Invalid or misplaced parameter: " _
  445.                    & Wscript.arguments.Item(intArgIter) & vbCRLF _
  446.                    & "Please check the input and try again," & vbCRLF _
  447.                    & "or invoke with '/?' for help with the syntax.")
  448.                 Wscript.Quit
  449.  
  450.         End Select
  451.  
  452.     Loop '** intArgIter <= Wscript.arguments.Count - 1
  453.  
  454.     If IsEmpty(intParseCmdLine) Then
  455.         intParseCmdLine = CONST_PROCEED
  456.         strTaskCommand = CONST_LIST
  457.     End If
  458.     Select Case strTaskCommand
  459.         Case CONST_CREATE
  460.             If IsEmpty(strShareName) then
  461.                 intParseCmdLine = CONST_ERROR
  462.             End IF
  463.             If IsEmpty(strSharePath) then
  464.                 intParseCmdLine = CONST_ERROR
  465.             End If
  466.         Case CONST_DELETE
  467.             If IsEmpty(strShareName) then
  468.                 intParseCmdLine = CONST_ERROR
  469.             End IF
  470.     End Select
  471.  
  472. End Function
  473.  
  474. '********************************************************************
  475. '*
  476. '* Sub ShowUsage()
  477. '*
  478. '* Purpose: Shows the correct usage to the user.
  479. '*
  480. '* Input:   None
  481. '*
  482. '* Output:  Help messages are displayed on screen.
  483. '*
  484. '********************************************************************
  485. Private Sub ShowUsage()
  486.  
  487.     Wscript.Echo ""
  488.     Wscript.Echo "Lists, creates, or deletes shares from a machine."
  489.     Wscript.Echo ""
  490.     Wscript.Echo "SYNTAX:"
  491.     Wscript.Echo "1.  Share.vbs /L [/S <server>] [/U <username>]" _
  492.                 &" [/W <password>]"
  493.     Wscript.Echo "                 [/O <outputfile>]"
  494.     Wscript.Echo "2.  Share.vbs /C /N <name> /P <path> [/T <type>]" _
  495.                & " [/V <description>]"
  496.     Wscript.Echo "             [/S <server>] [/U <username>] [/W <password>]"
  497.     Wscript.Echo "             [/O <outputfile>]"
  498.     Wscript.Echo "3.  Share.vbs /D /N <name>"
  499.     Wscript.Echo "             [/S <server>] [/U <username>] [/W <password>]"
  500.     Wscript.Echo "             [/O <outputfile>]"
  501.     Wscript.Echo ""
  502.     Wscript.Echo "PARAMETER SPECIFIERS:"
  503.     Wscript.Echo "   /L            Lists all shares on a machine."
  504.     Wscript.Echo "   /C            Creates a share on a machine."
  505.     Wscript.Echo "   /D            Deletes a share from a machine."
  506.     Wscript.echo "   name          Name of the share to be created or deleted."
  507.     Wscript.echo "   path          Path of the share to be created."
  508.     Wscript.Echo "   description   A description for the share."
  509.     Wscript.echo "   type          Type of the share to be created. Must be one"
  510.     Wscript.echo "                 one of Disk, Printer, IPC, Special."
  511.     Wscript.Echo "   server        A machine name."
  512.     Wscript.Echo "   username      The current user's name."
  513.     Wscript.Echo "   password      Password of the current user."
  514.     Wscript.Echo "   outputfile    The output file name."
  515.     Wscript.Echo ""
  516.     Wscript.Echo "EXAMPLE:"
  517.     Wscript.Echo "1. cscript Share.vbs /l /s MyMachine2"
  518.     Wscript.Echo "   List the shares on the machine MyMachine2."
  519.     Wscript.Echo "2. cscript Share.vbs /c /n scratch /p c:\scratch " _
  520.                & "/t Disk /v ""Scratch Directory"""
  521.     Wscript.Echo "   Creates a file share called ""scratch"" on the" _
  522.                & " local machine."
  523.     Wscript.Echo "3. cscript Share.vbs /d /n scratch /s MyMachine2."
  524.     Wscript.Echo "   Deletes the share named ""scratch"" on the machine" _
  525.                & " MyMachine2."
  526.  
  527. End Sub
  528.  
  529.  
  530. '********************************************************************
  531. '*
  532. '* Sub SortArray()
  533. '* Purpose: Sorts an array and arrange another array accordingly.
  534. '* Input:   strArray    the array to be sorted
  535. '*          blnOrder    True for ascending and False for descending
  536. '*          strArray2   an array that has exactly the same number of
  537. '*                      elements as strArray
  538. '*                      and will be reordered together with strArray
  539. '*          blnCase     indicates whether the order is case sensitive
  540. '* Output:  The sorted arrays are returned in the original arrays.
  541. '* Note:    Repeating elements are not deleted.
  542. '*
  543. '********************************************************************
  544.  
  545. Private Sub SortArray(strArray, blnOrder, strArray2, blnCase)
  546.  
  547.     ON ERROR RESUME NEXT
  548.  
  549.     Dim i, j, intUbound
  550.  
  551.     If IsArray(strArray) Then
  552.         intUbound = UBound(strArray)
  553.     Else
  554.         Print "Argument is not an array!"
  555.         Exit Sub
  556.     End If
  557.  
  558.     blnOrder = CBool(blnOrder)
  559.     blnCase = CBool(blnCase)
  560.     If Err.Number Then
  561.         Print "Argument is not a boolean!"
  562.         Exit Sub
  563.     End If
  564.  
  565.     i = 0
  566.     Do Until i > intUbound-1
  567.         j = i + 1
  568.         Do Until j > intUbound
  569.             If blnCase Then     'Case sensitive
  570.                 If (strArray(i) > strArray(j)) and blnOrder Then
  571.                     Swap strArray(i), strArray(j)   'swaps element i and j
  572.                     Swap strArray2(i), strArray2(j)
  573.                 ElseIf (strArray(i) < strArray(j)) and Not blnOrder Then
  574.                     Swap strArray(i), strArray(j)   'swaps element i and j
  575.                     Swap strArray2(i), strArray2(j)
  576.                 ElseIf strArray(i) = strArray(j) Then
  577.                     'Move element j to next to i
  578.                     If j > i + 1 Then
  579.                         Swap strArray(i+1), strArray(j)
  580.                         Swap strArray2(i+1), strArray2(j)
  581.                     End If
  582.                 End If
  583.             Else
  584.                 If (LCase(strArray(i)) > LCase(strArray(j))) and blnOrder Then
  585.                     Swap strArray(i), strArray(j)   'swaps element i and j
  586.                     Swap strArray2(i), strArray2(j)
  587.                 ElseIf (LCase(strArray(i)) < LCase(strArray(j))) _
  588.                     and Not blnOrder Then
  589.                     Swap strArray(i), strArray(j)   'swaps element i and j
  590.                     Swap strArray2(i), strArray2(j)
  591.                 ElseIf LCase(strArray(i)) = LCase(strArray(j)) Then
  592.                     'Move element j to next to i
  593.                     If j > i + 1 Then
  594.                         Swap strArray(i+1), strArray(j)
  595.                         Swap strArray2(i+1), strArray2(j)
  596.                     End If
  597.                 End If
  598.             End If
  599.             j = j + 1
  600.         Loop
  601.         i = i + 1
  602.     Loop
  603.  
  604. End Sub
  605.  
  606. '********************************************************************
  607. '*
  608. '* Sub Swap()
  609. '* Purpose: Exchanges values of two strings.
  610. '* Input:   strA    a string
  611. '*          strB    another string
  612. '* Output:  Values of strA and strB are exchanged.
  613. '*
  614. '********************************************************************
  615.  
  616. Private Sub Swap(ByRef strA, ByRef strB)
  617.  
  618.     Dim strTemp
  619.  
  620.     strTemp = strA
  621.     strA = strB
  622.     strB = strTemp
  623.  
  624. End Sub
  625.  
  626. '********************************************************************
  627. '*
  628. '* Sub ReArrangeArray()
  629. '* Purpose: Rearranges one array according to order specified in another array.
  630. '* Input:   strArray    the array to be rearranged
  631. '*          intOrder    an integer array that specifies the order
  632. '* Output:  strArray is returned as rearranged
  633. '*
  634. '********************************************************************
  635.  
  636. Private Sub ReArrangeArray(strArray, intOrder)
  637.  
  638.     ON ERROR RESUME NEXT
  639.  
  640.     Dim intUBound, i, strTempArray()
  641.  
  642.     If Not (IsArray(strArray) and IsArray(intOrder)) Then
  643.         Print "At least one of the arguments is not an array"
  644.         Exit Sub
  645.     End If
  646.  
  647.     intUBound = UBound(strArray)
  648.  
  649.     If intUBound <> UBound(intOrder) Then
  650.         Print "The upper bound of these two arrays do not match!"
  651.         Exit Sub
  652.     End If
  653.  
  654.     ReDim strTempArray(intUBound)
  655.  
  656.     For i = 0 To intUBound
  657.         strTempArray(i) = strArray(intOrder(i))
  658.         If Err.Number Then
  659.             Print "Error 0x" & CStr(Hex(Err.Number)) & _
  660.                 " occurred in rearranging an array."
  661.             If Err.Description <> "" Then
  662.                 Print "Error description: " & Err.Description & "."
  663.             End If
  664.             Err.Clear
  665.             Exit Sub
  666.         End If
  667.     Next
  668.  
  669.     For i = 0 To intUBound
  670.         strArray(i) = strTempArray(i)
  671.     Next
  672.  
  673. End Sub
  674.  
  675. '********************************************************************
  676. '* General Routines
  677. '********************************************************************
  678.  
  679. '********************************************************************
  680. '*
  681. '* Function strPackString()
  682. '*
  683. '* Purpose: Attaches spaces to a string to increase the length to intWidth.
  684. '*
  685. '* Input:   strString   a string
  686. '*          intWidth    the intended length of the string
  687. '*          blnAfter    Should spaces be added after the string?
  688. '*          blnTruncate specifies whether to truncate the string or not if
  689. '*                      the string length is longer than intWidth
  690. '*
  691. '* Output:  strPackString is returned as the packed string.
  692. '*
  693. '********************************************************************
  694. Private Function strPackString( ByVal strString, _
  695.                                 ByVal intWidth,  _
  696.                                 ByVal blnAfter,  _
  697.                                 ByVal blnTruncate)
  698.  
  699.     ON ERROR RESUME NEXT
  700.  
  701.     intWidth      = CInt(intWidth)
  702.     blnAfter      = CBool(blnAfter)
  703.     blnTruncate   = CBool(blnTruncate)
  704.  
  705.     If Err.Number Then
  706.         Call Wscript.Echo ("Argument type is incorrect!")
  707.         Err.Clear
  708.         Wscript.Quit
  709.     End If
  710.  
  711.     If IsNull(strString) Then
  712.         strPackString = "null" & Space(intWidth-4)
  713.         Exit Function
  714.     End If
  715.  
  716.     strString = CStr(strString)
  717.     If Err.Number Then
  718.         Call Wscript.Echo ("Argument type is incorrect!")
  719.         Err.Clear
  720.         Wscript.Quit
  721.     End If
  722.  
  723.     If intWidth > Len(strString) Then
  724.         If blnAfter Then
  725.             strPackString = strString & Space(intWidth-Len(strString))
  726.         Else
  727.             strPackString = Space(intWidth-Len(strString)) & strString & " "
  728.         End If
  729.     Else
  730.         If blnTruncate Then
  731.             strPackString = Left(strString, intWidth-1) & " "
  732.         Else
  733.             strPackString = strString & " "
  734.         End If
  735.     End If
  736.  
  737. End Function
  738.  
  739. '********************************************************************
  740. '* 
  741. '*  Function blnGetArg()
  742. '*
  743. '*  Purpose: Helper to intParseCmdLine()
  744. '* 
  745. '*  Usage:
  746. '*
  747. '*     Case "/s" 
  748. '*       blnGetArg ("server name", strServer, intArgIter)
  749. '*
  750. '********************************************************************
  751. Private Function blnGetArg ( ByVal StrVarName,   _
  752.                              ByRef strVar,       _
  753.                              ByRef intArgIter) 
  754.  
  755.     blnGetArg = False 'failure, changed to True upon successful completion
  756.  
  757.     If Len(Wscript.Arguments(intArgIter)) > 2 then
  758.         If Mid(Wscript.Arguments(intArgIter),3,1) = ":" then
  759.             If Len(Wscript.Arguments(intArgIter)) > 3 then
  760.                 strVar = Right(Wscript.Arguments(intArgIter), _
  761.                          Len(Wscript.Arguments(intArgIter)) - 3)
  762.                 blnGetArg = True
  763.                 Exit Function
  764.             Else
  765.                 intArgIter = intArgIter + 1
  766.                 If intArgIter > (Wscript.Arguments.Count - 1) Then
  767.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  768.                     Call Wscript.Echo( "Please check the input and try again.")
  769.                     Exit Function
  770.                 End If
  771.  
  772.                 strVar = Wscript.Arguments.Item(intArgIter)
  773.                 If Err.Number Then
  774.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  775.                     Call Wscript.Echo( "Please check the input and try again.")
  776.                     Exit Function
  777.                 End If
  778.  
  779.                 If InStr(strVar, "/") Then
  780.                     Call Wscript.Echo( "Invalid " & StrVarName)
  781.                     Call Wscript.Echo( "Please check the input and try again.")
  782.                     Exit Function
  783.                 End If
  784.  
  785.                 blnGetArg = True 'success
  786.             End If
  787.         Else
  788.             strVar = Right(Wscript.Arguments(intArgIter), _
  789.                      Len(Wscript.Arguments(intArgIter)) - 2)
  790.             blnGetArg = True 'success
  791.             Exit Function
  792.         End If
  793.     Else
  794.         intArgIter = intArgIter + 1
  795.         If intArgIter > (Wscript.Arguments.Count - 1) Then
  796.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  797.             Call Wscript.Echo( "Please check the input and try again.")
  798.             Exit Function
  799.         End If
  800.  
  801.         strVar = Wscript.Arguments.Item(intArgIter)
  802.         If Err.Number Then
  803.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  804.             Call Wscript.Echo( "Please check the input and try again.")
  805.             Exit Function
  806.         End If
  807.  
  808.         If InStr(strVar, "/") Then
  809.             Call Wscript.Echo( "Invalid " & StrVarName)
  810.             Call Wscript.Echo( "Please check the input and try again.")
  811.             Exit Function
  812.         End If
  813.         blnGetArg = True 'success
  814.     End If
  815. End Function
  816.  
  817. '********************************************************************
  818. '*
  819. '* Function blnConnect()
  820. '*
  821. '* Purpose: Connects to machine strServer.
  822. '*
  823. '* Input:   strServer       a machine name
  824. '*          strNameSpace    a namespace
  825. '*          strUserName     name of the current user
  826. '*          strPassword     password of the current user
  827. '*
  828. '* Output:  objService is returned  as a service object.
  829. '*          strServer is set to local host if left unspecified
  830. '*
  831. '********************************************************************
  832. Private Function blnConnect(ByVal strNameSpace, _
  833.                             ByVal strUserName,  _
  834.                             ByVal strPassword,  _
  835.                             ByRef strServer,    _
  836.                             ByRef objService)
  837.  
  838.     ON ERROR RESUME NEXT
  839.  
  840.     Dim objLocator, objWshNet
  841.  
  842.     blnConnect = False     'There is no error.
  843.  
  844.     'Create Locator object to connect to remote CIM object manager
  845.     Set objLocator = CreateObject("WbemScripting.SWbemLocator")
  846.     If Err.Number then
  847.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  848.                            " occurred in creating a locator object." )
  849.         If Err.Description <> "" Then
  850.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  851.         End If
  852.         Err.Clear
  853.         blnConnect = True     'An error occurred
  854.         Exit Function
  855.     End If
  856.  
  857.     'Connect to the namespace which is either local or remote
  858.     Set objService = objLocator.ConnectServer (strServer, strNameSpace, _
  859.        strUserName, strPassword)
  860.     ObjService.Security_.impersonationlevel = 3
  861.     If Err.Number then
  862.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  863.                            " occurred in connecting to server " _
  864.            & strServer & ".")
  865.         If Err.Description <> "" Then
  866.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  867.         End If
  868.         Err.Clear
  869.         blnConnect = True     'An error occurred
  870.     End If
  871.  
  872.     'Get the current server's name if left unspecified
  873.     If IsEmpty(strServer) Then
  874.         Set objWshNet = CreateObject("Wscript.Network")
  875.     strServer     = objWshNet.ComputerName
  876.     End If
  877.  
  878. End Function
  879.  
  880. '********************************************************************
  881. '*
  882. '* Sub      VerifyHostIsCscript()
  883. '*
  884. '* Purpose: Determines which program is used to run this script.
  885. '*
  886. '* Input:   None
  887. '*
  888. '* Output:  If host is not cscript, then an error message is printed 
  889. '*          and the script is aborted.
  890. '*
  891. '********************************************************************
  892. Sub VerifyHostIsCscript()
  893.  
  894.     ON ERROR RESUME NEXT
  895.  
  896.     Dim strFullName, strCommand, i, j, intStatus
  897.  
  898.     strFullName = WScript.FullName
  899.  
  900.     If Err.Number then
  901.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & " occurred." )
  902.         If Err.Description <> "" Then
  903.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  904.         End If
  905.         intStatus =  CONST_ERROR
  906.     End If
  907.  
  908.     i = InStr(1, strFullName, ".exe", 1)
  909.     If i = 0 Then
  910.         intStatus =  CONST_ERROR
  911.     Else
  912.         j = InStrRev(strFullName, "\", i, 1)
  913.         If j = 0 Then
  914.             intStatus =  CONST_ERROR
  915.         Else
  916.             strCommand = Mid(strFullName, j+1, i-j-1)
  917.             Select Case LCase(strCommand)
  918.                 Case "cscript"
  919.                     intStatus = CONST_CSCRIPT
  920.                 Case "wscript"
  921.                     intStatus = CONST_WSCRIPT
  922.                 Case Else       'should never happen
  923.                     Call Wscript.Echo( "An unexpected program was used to " _
  924.                                        & "run this script." )
  925.                     Call Wscript.Echo( "Only CScript.Exe or WScript.Exe can " _
  926.                                        & "be used to run this script." )
  927.                     intStatus = CONST_ERROR
  928.                 End Select
  929.         End If
  930.     End If
  931.  
  932.     If intStatus <> CONST_CSCRIPT Then
  933.         Call WScript.Echo( "Please run this script using CScript." & vbCRLF & _
  934.              "This can be achieved by" & vbCRLF & _
  935.              "1. Using ""CScript Share.vbs arguments"" for Windows 95/98 or" _
  936.              & vbCRLF & "2. Changing the default Windows Scripting Host " _
  937.              & "setting to CScript" & vbCRLF & "    using ""CScript " _
  938.              & "//H:CScript //S"" and running the script using" & vbCRLF & _
  939.              "    ""Share.vbs arguments"" for Windows NT/2000." )
  940.         WScript.Quit
  941.     End If
  942.  
  943. End Sub
  944.  
  945. '********************************************************************
  946. '*
  947. '* Sub WriteLine()
  948. '* Purpose: Writes a text line either to a file or on screen.
  949. '* Input:   strMessage  the string to print
  950. '*          objFile     an output file object
  951. '* Output:  strMessage is either displayed on screen or written to a file.
  952. '*
  953. '********************************************************************
  954. Sub WriteLine(ByVal strMessage, ByVal objFile)
  955.  
  956.     On Error Resume Next
  957.     If IsObject(objFile) then        'objFile should be a file object
  958.         objFile.WriteLine strMessage
  959.     Else
  960.         Call Wscript.Echo( strMessage )
  961.     End If
  962.  
  963. End Sub
  964.  
  965. '********************************************************************
  966. '* 
  967. '* Function blnErrorOccurred()
  968. '*
  969. '* Purpose: Reports error with a string saying what the error occurred in.
  970. '*
  971. '* Input:   strIn        string saying what the error occurred in.
  972. '*
  973. '* Output:  displayed on screen 
  974. '* 
  975. '********************************************************************
  976. Private Function blnErrorOccurred (ByVal strIn)
  977.  
  978.     If Err.Number Then
  979.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & ": " & strIn)
  980.         If Err.Description <> "" Then
  981.             Call Wscript.Echo( "Error description: " & Err.Description)
  982.         End If
  983.         Err.Clear
  984.         blnErrorOccurred = True
  985.     Else
  986.         blnErrorOccurred = False
  987.     End If
  988.  
  989. End Function
  990.  
  991. '********************************************************************
  992. '* 
  993. '* Function blnOpenFile
  994. '*
  995. '* Purpose: Opens a file.
  996. '*
  997. '* Input:   strFileName        A string with the name of the file.
  998. '*
  999. '* Output:  Sets objOpenFile to a FileSystemObject and setis it to 
  1000. '*            Nothing upon Failure.
  1001. '* 
  1002. '********************************************************************
  1003. Private Function blnOpenFile(ByVal strFileName, ByRef objOpenFile)
  1004.  
  1005.     ON ERROR RESUME NEXT
  1006.  
  1007.     Dim objFileSystem
  1008.  
  1009.     Set objFileSystem = Nothing
  1010.  
  1011.     If IsEmpty(strFileName) OR strFileName = "" Then
  1012.         blnOpenFile = False
  1013.         Set objOpenFile = Nothing
  1014.         Exit Function
  1015.     End If
  1016.  
  1017.     'Create a file object
  1018.     Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  1019.     If blnErrorOccurred("Could not create filesystem object.") Then
  1020.         blnOpenFile = False
  1021.         Set objOpenFile = Nothing
  1022.         Exit Function
  1023.     End If
  1024.  
  1025.     'Open the file for output
  1026.     Set objOpenFile = objFileSystem.OpenTextFile(strFileName, 8, True)
  1027.     If blnErrorOccurred("Could not open") Then
  1028.         blnOpenFile = False
  1029.         Set objOpenFile = Nothing
  1030.         Exit Function
  1031.     End If
  1032.     blnOpenFile = True
  1033.  
  1034. End Function
  1035.  
  1036. '********************************************************************
  1037. '*                                                                  *
  1038. '*                           End of File                            *
  1039. '*                                                                  *
  1040. '********************************************************************
  1041.  
  1042.  
  1043.