home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 May / W2KPRK.iso / compmgmt.cab / GroupDescription.vbs < prev    next >
Text File  |  1999-11-04  |  17KB  |  476 lines

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