home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 May / W2KPRK.iso / ras.cab / listproperties.vbs < prev    next >
Text File  |  1999-11-04  |  18KB  |  519 lines

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