home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 May / W2KPRK.iso / compmgmt.cab / Usergroup.vbs < prev   
Text File  |  1999-11-04  |  18KB  |  492 lines

  1.  
  2. '********************************************************************
  3. '* File:          USERGROUP.VBS
  4. '* Created:       May 1998
  5. '* Version:       1.0
  6. '*
  7. '* Main Function: Adds or deletes one or multiple users to or from a group.
  8. '* Usage: USERGROUP.VBS grouppath </A | /D | /L> </A:userpath | /I:inputfile>
  9. '*        [/U:username] [/W:password] [/Q]
  10. '*
  11. '* Copyright (C) 1998 Microsoft Corporation
  12. '*
  13. '********************************************************************
  14.  
  15. OPTION EXPLICIT
  16. ON ERROR RESUME NEXT
  17.  
  18. 'Define constants
  19. CONST CONST_ERROR                   = 0
  20. CONST CONST_WSCRIPT                 = 1
  21. CONST CONST_CSCRIPT                 = 2
  22. CONST CONST_SHOW_USAGE              = 3
  23. CONST CONST_PROCEED                 = 4
  24. CONST CONST_ADD                        = "add"
  25. CONST CONST_DELETE                    = "delete"
  26. CONST CONST_LIST                    = "list"
  27.  
  28. 'Declare variables
  29. Dim strInputFile, intOpMode, blnQuiet, strAction, i
  30. Dim strGroupPath, strUserName, strPassword
  31. ReDim strUserPaths(0), strArgumentArray(0)
  32.  
  33. 'Initialize variables
  34. strGroupPath = ""
  35. strUserName = ""
  36. strPassword = ""
  37. strInputFile = ""
  38. blnQuiet = False
  39. strAction = CONST_ADD        'Default to add users to group.
  40. strArgumentArray(0) = ""
  41. strUserPaths(0) = ""
  42.  
  43. 'Get the command line arguments
  44. For i = 0 to Wscript.arguments.count - 1
  45.     ReDim Preserve strArgumentArray(i)
  46.     strArgumentArray(i) = Wscript.arguments.Item(i)
  47. Next
  48.  
  49. 'Check whether the script is run using CScript
  50. Select Case intChkProgram()
  51.     Case CONST_CSCRIPT
  52.         'Do Nothing
  53.     Case CONST_WSCRIPT
  54.         WScript.Echo "Please run this script using CScript." & vbCRLF & _
  55.             "This can be achieved by" & vbCRLF & _
  56.             "1. Using ""CScript UserGroup.vbs arguments"" for Windows 95/98 or" & vbCRLF & _
  57.             "2. Changing the default Windows Scripting Host setting to CScript" & vbCRLF & _
  58.             "    using ""CScript //H:CScript //S"" and running the script using" & vbCRLF & _
  59.             "    ""UserGroup.vbs arguments"" for Windows NT."
  60.         WScript.Quit
  61.     Case Else
  62.         WScript.Quit
  63. End Select
  64.  
  65. 'Parse the command line
  66. intOpMode = intParseCmdLine(strArgumentArray, strGroupPath, strUserPaths, _
  67.             strInputFile, strAction, strUserName, strPassword, blnQuiet)
  68. If Err.Number then
  69.     Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred in parsing the command line."
  70.     If Err.Description <> "" Then
  71.         Print "Error description: " & Err.Description & "."
  72.     End If
  73.     WScript.Quit
  74. End If
  75.  
  76. Select Case intOpMode
  77.     Case CONST_SHOW_USAGE
  78.         Call ShowUsage()
  79.     Case CONST_PROCEED
  80.         Call UserGroup(strGroupPath, strUserPaths, strInputFile, strAction, _
  81.              strUserName, strPassword)
  82.     Case CONST_ERROR
  83.         'Do nothing.
  84.     Case Else                    'Default -- should never happen
  85.         Print "Error occurred in passing parameters."
  86. End Select
  87.  
  88. '********************************************************************
  89. '*
  90. '* Function intChkProgram()
  91. '* Purpose: Determines which program is used to run this script.
  92. '* Input:   None
  93. '* Output:  intChkProgram is set to one of CONST_ERROR, CONST_WSCRIPT,
  94. '*          and CONST_CSCRIPT.
  95. '*
  96. '********************************************************************
  97.  
  98. Private Function intChkProgram()
  99.  
  100.     ON ERROR RESUME NEXT
  101.  
  102.     Dim strFullName, strCommand, i, j
  103.  
  104.     'strFullName should be something like C:\WINDOWS\COMMAND\CSCRIPT.EXE
  105.     strFullName = WScript.FullName
  106.     If Err.Number then
  107.         Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred."
  108.         If Err.Description <> "" Then
  109.             Print "Error description: " & Err.Description & "."
  110.         End If
  111.         intChkProgram =  CONST_ERROR
  112.         Exit Function
  113.     End If
  114.  
  115.     i = InStr(1, strFullName, ".exe", 1)
  116.     If i = 0 Then
  117.         intChkProgram =  CONST_ERROR
  118.         Exit Function
  119.     Else
  120.         j = InStrRev(strFullName, "\", i, 1)
  121.         If j = 0 Then
  122.             intChkProgram =  CONST_ERROR
  123.             Exit Function
  124.         Else
  125.             strCommand = Mid(strFullName, j+1, i-j-1)
  126.             Select Case LCase(strCommand)
  127.                 Case "cscript"
  128.                     intChkProgram = CONST_CSCRIPT
  129.                 Case "wscript"
  130.                     intChkProgram = CONST_WSCRIPT
  131.                 Case Else       'should never happen
  132.                     Print "An unexpected program is used to run this script."
  133.                     Print "Only CScript.Exe or WScript.Exe can be used to run this script."
  134.                     intChkProgram = CONST_ERROR
  135.             End Select
  136.         End If
  137.     End If
  138.  
  139. End Function
  140.  
  141. '********************************************************************
  142. '*
  143. '* Function intParseCmdLine()
  144. '* Purpose: Parses the command line.
  145. '* Input:   strArgumentArray    an array containing input from the command line
  146. '* Output:  strGroupPath        ADsPath of a group object
  147. '*          strUserPaths        ADsPath of a user object
  148. '*          strInputFile        an input file name
  149. '*          strAction           the action to take
  150. '*          strUserName         name of the current user
  151. '*          strPassword         password of the current user
  152. '*          blnQuiet            specifies whether to suppress messages
  153. '*          intParseCmdLine     is set to CONST_SHOW_USAGE if there is an error
  154. '*                              in input and CONST_PROCEED otherwise.
  155. '*
  156. '********************************************************************
  157.  
  158. Private Function intParseCmdLine(strArgumentArray, strGroupPath, strUserPaths, _
  159.     strInputFile, strAction, strUserName, strPassword, blnQuiet)
  160.  
  161.     ON ERROR RESUME NEXT
  162.  
  163.     Dim strFlag, i, intCount
  164.  
  165.     strFlag = strArgumentArray(0)
  166.  
  167.     If strFlag = "" then                'No arguments have been received
  168.         Print "Arguments are required."
  169.         intParseCmdLine = CONST_ERROR
  170.         Exit Function
  171.     End If
  172.  
  173.     If (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
  174.         OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "?") OR (strFlag="h") Then
  175.         intParseCmdLine = CONST_SHOW_USAGE
  176.         Exit Function
  177.     End If
  178.  
  179.     strGroupPath = strFlag          'The first parameter must be the group's ADsPath.
  180.     intCount = -1
  181.     For i = 1 to UBound(strArgumentArray)
  182.         strFlag = LCase(Left(strArgumentArray(i), InStr(1, strArgumentArray(i), ":")-1))
  183.         If Err.Number Then            'An error occurs if there is no : in the string
  184.             Err.Clear
  185.             Select Case LCase(strArgumentArray(i))
  186.                 Case "/q"
  187.                     blnQuiet = True
  188.                 Case "/a"
  189.                     strAction = CONST_ADD
  190.                 Case "/d"
  191.                     strAction = CONST_DELETE
  192.                 Case "/l"
  193.                     strAction = CONST_LIST
  194.                 Case Else
  195.                     Print strArgumentArray(i) & " is not a valid input."
  196.                     Print "Please check the input and try again."
  197.                     intParseCmdLine = CONST_ERROR
  198.                     Exit Function
  199.             End Select
  200.         Else
  201.             Select Case strFlag
  202.                 Case "/a"
  203.                     intCount = intCount + 1
  204.                     ReDim Preserve strUserPaths(intCount)
  205.                     strUserPaths(intCount) = Right(strArgumentArray(i), _
  206.                         Len(strArgumentArray(i))-3)
  207.                 Case "/i"
  208.                     strInputFile = Right(strArgumentArray(i), Len(strArgumentArray(i))-3)
  209.                 Case "/u"
  210.                     strUserName = Right(strArgumentArray(i), Len(strArgumentArray(i))-3)
  211.                 Case "/w"
  212.                     strPassword = Right(strArgumentArray(i), Len(strArgumentArray(i))-3)
  213.                 Case Else
  214.                     Print "Invalid flag " & """" & strFlag & ":""" & "."
  215.                     Print "Please check the input and try again."
  216.                     intParseCmdLine = CONST_ERROR
  217.                     Exit Function
  218.                 End Select
  219.         End If
  220.     Next
  221.  
  222.     intParseCmdLine = CONST_PROCEED
  223.  
  224.     If strUserPaths(0) = "" And strInputFile = "" And strAction <> CONST_LIST Then
  225.         Print "The user ADsPath is missing."
  226.         Print "Please check the input and try again."
  227.         intParseCmdLine = CONST_ERROR
  228.     End If
  229.  
  230. End Function
  231.  
  232. '********************************************************************
  233. '*
  234. '* Sub ShowUsage()
  235. '* Purpose: Shows the correct usage to the user.
  236. '* Input:   None
  237. '* Output:  Help messages are displayed on screen.
  238. '*
  239. '********************************************************************
  240.  
  241. Private Sub ShowUsage()
  242.  
  243.     Wscript.echo ""
  244.     Wscript.echo "Adds or deletes one or multiple users to or from a group." & vbCRLF
  245.     Wscript.echo "USERGROUP.VBS grouppath </A | /D | /L> </A:userpath | /I:inputfile>"
  246.     Wscript.echo "[/U:username] [/W:password] [/Q]"
  247.     Wscript.echo "   /A, /I, /U, /W"
  248.     Wscript.echo "                 Parameter specifiers."
  249.     Wscript.Echo "   grouppath     The ADsPath of a group object."
  250.     Wscript.Echo "   /A, /D, /L    Adds users to a group, deletes users from a groups,"
  251.     Wscript.Echo "                   or list members of a group."
  252.     Wscript.Echo "   userpath      The ADsPath of a user object."
  253.     Wscript.Echo "   inputfile     Name of the input file."
  254.     Wscript.Echo "   username      Username of the current user."
  255.     Wscript.Echo "   password      Password of the current user."
  256.     Wscript.Echo "   /Q            Suppresses all output messages." & vbCRLF
  257.     Wscript.Echo "EXAMPLES:"
  258.     Wscript.echo "1. USERGROUP.VBS WinNT:\\FooFoo\administrators /A"
  259.     Wscript.echo "   \a:WinNT:\\FooFoo1\user1 \a:WinNT:\\FooFoo2\user2"
  260.     Wscript.echo "   adds user1 of domain FooFoo1 and user2 of domain FooFoo2 to"
  261.     Wscript.echo "   the administrators group of domain FooFoo."
  262.     Wscript.echo "2. USERGROUP.VBS WinNT:\\FooFoo\administrators /D" 
  263.     Wscript.echo "   \a:WinNT:\\FooFoo2\user2"
  264.     Wscript.echo "   deletes user2 of domain FooFoo2 from the administrators group of"
  265.     Wscript.echo "   domain FooFoo."
  266.  
  267. End Sub
  268.  
  269. '********************************************************************
  270. '*
  271. '* Sub UserGroup()
  272. '* Purpose: Adds or deletes one or multiple users to or from a group.
  273. '* Input:   strGroupPath        ADsPath of a group object
  274. '*          strUserPaths        ADsPath of a user object
  275. '*          strInputFile        an input file name
  276. '*          strAction           deletes user(s) from a group
  277. '*          strUserName         name of the current user
  278. '*          strPassword         password of the current user
  279. '* Output:  Results are either printed on screen or saved in strInputFile.
  280. '*
  281. '********************************************************************
  282.  
  283. Private Sub UserGroup(strGroupPath, strUserPaths, strInputFile, strAction, _
  284.     strUserName, strPassword)
  285.  
  286.     ON ERROR RESUME NEXT
  287.  
  288.     Dim objGroup, objUser, objProvider, strProvider, strUserPath
  289.     Dim objFileSystem, objInputFile, strMessage, i, intFlag
  290.  
  291.     Print "Getting object " & strGroupPath & "..."
  292.     If strUserName = ""    then        'The current user is assumed
  293.         set objGroup = GetObject(strGroupPath)
  294.     Else                        'Credentials are passed
  295.         strProvider = Left(strGroupPath, InStr(1, strGroupPath, ":"))
  296.         set objProvider = GetObject(strProvider)
  297.         'Use user authentication
  298.         set objGroup = objProvider.OpenDsObject(strGroupPath,strUserName,strPassword,1)        
  299.     End If
  300.     If Err.Number then
  301.         If CStr(Hex(Err.Number)) = "80070035" Then
  302.             Print "Object " & strGroupPath & " is not found."
  303.         Else
  304.             Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred in getting object " _
  305.                 & strGroupPath & "."
  306.             If Err.Description <> "" Then
  307.                 Print "Error description: " & Err.Description & "."
  308.             End If
  309.         End If
  310.         Err.Clear
  311.         Exit Sub
  312.     End If
  313.  
  314.     If strAction = CONST_LIST Then
  315.         Print "Members of " & objGroup.ADsPath & ":"
  316.         i = 0
  317.         For Each objUser In objGroup.Members
  318.             Print "    " & objUser.ADsPath
  319.             i = i + 1
  320.         Next
  321.         If i = 0 Then
  322.             Print objGroup.ADsPath & " has no members!"
  323.  
  324.         End If
  325.         Exit Sub
  326.     End If
  327.  
  328.     intFlag = 0
  329.     If strUserPaths(0) <> "" Then
  330.         For i = 0 To UBound(strUserPaths)
  331.             Select Case strAction 
  332.                 Case CONST_ADD
  333.                     If objGroup.IsMember(strUserPaths(i)) Then
  334.                         Print strUserPaths(i) & " is already a member of " _
  335.                             & objGroup.ADsPath & "."
  336.                         intFlag = 0
  337.                     Else
  338.                         objGroup.Add(strUserPaths(i))
  339.                         intFlag = 1
  340.                     End If
  341.                 Case CONST_DELETE
  342.                     If objGroup.IsMember(strUserPaths(i)) Then
  343.                         objGroup.Remove(strUserPaths(i))
  344.                         intFlag = 1
  345.                     Else
  346.                         Print strUserPaths(i) & " is not a member of " _
  347.                             & objGroup.ADsPath & "."
  348.                         intFlag = 0
  349.                     End If
  350.                 Case Else
  351.                     Print "Action " & strAction & " is unknown!"
  352.                     Exit Sub
  353.             End Select
  354.             If Err.Number Then
  355.                 If strAction = CONST_DELETE Then
  356.                     strMessage = " occurred in deleting user " & strUserPaths(i) _
  357.                         & " from group " & strGroupPath & "."
  358.                 ElseIf strAction = CONST_ADD Then
  359.                     strMessage = " occurred in adding user " & strUserPaths(i) _
  360.                         & " to group " & strGroupPath & "."
  361.                 End If
  362.                 Print "Error 0x" & CStr(Hex(Err.Number)) & strMessage
  363.                 If Err.Description <> "" Then
  364.                     Print "Error description: " & Err.Description & "."
  365.                 End If
  366.                 Print "Clear error and continue."
  367.                 Err.Clear
  368.             ElseIf intFlag = 1 Then
  369.                 If strAction = CONST_DELETE Then
  370.                     strMessage = "Succeeded in deleting user " & strUserPaths(i) _
  371.                     & " from group " & strGroupPath & "."
  372.                 ElseIf strAction = CONST_ADD Then
  373.                     strMessage = "Succeeded in adding user " & strUserPaths(i) _
  374.                     & " to group " & strGroupPath & "."
  375.                 End If
  376.                 Print strMessage
  377.             End If
  378.         Next
  379.     End If
  380.  
  381.     If strInputFile <> "" Then
  382.         'Create a filesystem object.
  383.         Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  384.         If Err.Number then
  385.             Print "Error 0x" & CStr(Hex(Err.Number)) & _
  386.                 " occurred in opening a filesystem object."
  387.             If Err.Description <> "" Then
  388.                 Print "Error description: " & Err.Description & "."
  389.             End If
  390.             Exit Sub
  391.         End If
  392.         'Open the file For output
  393.         Set objInputFile = objFileSystem.OpenTextFile(strInputFile)
  394.         If Err.Number then
  395.             Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred in opening file " _
  396.                 & strInputFile
  397.             If Err.Description <> "" Then
  398.                 Print "Error description: " & Err.Description & "."
  399.             End If
  400.             Exit Sub
  401.         End If
  402.  
  403.         While Not objInputFile.AtEndOfStream
  404.             'Read a line and get rid of leading and trailing spaces.
  405.             strUserPath = Trim(objInputFile.ReadLine)
  406.             If strUserPath <> "" Then
  407.                 Select Case strAction 
  408.                     Case CONST_ADD
  409.                         If objGroup.IsMember(strUserPath) Then
  410.                             Print strUserPath & " is already a member of " _
  411.                                 & objGroup.ADsPath & "."
  412.                             intFlag = 0
  413.                         Else
  414.                             objGroup.Add(strUserPath)
  415.                             intFlag = 1
  416.                         End If
  417.                     Case CONST_DELETE
  418.                         If objGroup.IsMember(strUserPath) Then
  419.                             objGroup.Remove(strUserPath)
  420.                             intFlag = 1
  421.                         Else
  422.                             Print strUserPath & " is not a member of " _
  423.                                 & objGroup.ADsPath & "."
  424.                             intFlag = 0
  425.                         End If
  426.                     Case Else
  427.                         Print "Action " & strAction & " is unknown!"
  428.                         Exit Sub
  429.                 End Select
  430.                 If Err.Number Then
  431.                     If strAction = CONST_DELETE Then
  432.                         strMessage = " occurred in deleting user " & strUserPath _
  433.                             & " from group " & strGroupPath & "."
  434.                     ElseIf strAction = CONST_ADD Then
  435.                         strMessage = " occurred in adding user " & strUserPath _
  436.                             & " to group " & strGroupPath & "."
  437.                     End If
  438.                     Print "Error 0x" & CStr(Hex(Err.Number)) & strMessage
  439.                     If Err.Description <> "" Then
  440.                         Print "Error description: " & Err.Description & "."
  441.                     End If
  442.                     Print "Clear error and continue."
  443.                     Err.Clear
  444.                 ElseIf intFlag = 1 Then
  445.                     If strAction = CONST_DELETE Then
  446.                         strMessage = "Succeeded in deleting user " & strUserPath _
  447.                         & " from group " & strGroupPath & "."
  448.                     ElseIf strAction = CONST_ADD Then
  449.                         strMessage = "Succeeded in adding user " & strUserPath _
  450.                         & " to group " & strGroupPath & "."
  451.                     End If
  452.                     Print strMessage
  453.                 End If
  454.             End If
  455.         Wend
  456.     End If
  457.  
  458. End Sub
  459.  
  460. '********************************************************************
  461. '*
  462. '* Sub Print()
  463. '* Purpose: Prints a message on screen if blnQuiet = False.
  464. '* Input:   strMessage      the string to print
  465. '* Output:  strMessage is printed on screen if blnQuiet = False.
  466. '*
  467. '********************************************************************
  468.  
  469. Sub Print(ByRef strMessage)
  470.     If Not blnQuiet then
  471.         Wscript.Echo  strMessage
  472.     End If
  473. End Sub
  474.  
  475. '********************************************************************
  476. '*                                                                  *
  477. '*                           End of File                            *
  478. '*                                                                  *
  479. '********************************************************************
  480.  
  481. '********************************************************************
  482. '*
  483. '* Procedures calling sequence: USERGROUP.VBS
  484. '*
  485. '*  intChkProgram
  486. '*  intParseCmdLine
  487. '*  ShowUsage
  488. '*  UserGroup
  489. '*
  490. '********************************************************************
  491.  
  492.