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

  1.  
  2. '********************************************************************
  3. '*
  4. '* File:        LISTDOMAINS.VBS
  5. '* Created:     August 1998
  6. '* Version:     1.0
  7. '*
  8. '* Main Function: Lists all domains within a namespace.
  9. '* Usage: LISTDOMAINS.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 intOpMode, i
  27. Dim strADsPath, strUserName, strPassword, blnQuiet, strOutputFile
  28. ReDim strArgumentArray(0)
  29.  
  30. 'Initialize variables
  31. strArgumentArray(0) = ""
  32. strADsPath = ""
  33. strUserName = ""
  34. strPassword = ""
  35. strOutputFile = ""
  36. blnQuiet = False
  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 LISTDOMAINS.vbs arguments"" for Windows 95/98 or" & vbCRLF & _
  52.             "2. Changing the default Windows Scripting Host setting to CScript" & vbCRLF & _
  53.             "    using ""CScript //H:CScript //S"" and running the script using" & vbCRLF & _
  54.             "    ""LISTDOMAINS.vbs arguments"" for Windows NT."
  55.         WScript.Quit
  56.     Case Else
  57.         WScript.Quit
  58. End Select
  59.  
  60. 'Parse the command line
  61. intOpMode = intParseCmdLine(strArgumentArray, strADsPath, blnQuiet, _
  62.             strUserName, strPassword, strOutputFile)
  63. If Err.Number then
  64.     Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred in parsing the command line."
  65.     If Err.Description <> "" Then
  66.         Print "Error description: " & Err.Description & "."
  67.     End If
  68.     WScript.Quit
  69. End If
  70.  
  71. Select Case intOpMode
  72.     Case CONST_SHOW_USAGE
  73.         Call ShowUsage()
  74.     Case CONST_PROCEED
  75.         Call GetDomains(strADsPath, strUserName, strPassword, strOutputFile)
  76.     Case CONST_ERROR
  77.         'Do nothing.
  78.     Case Else                    'Default -- should never happen
  79.         Print "Error occurred in passing parameters."
  80. End Select
  81.  
  82. '********************************************************************
  83. '*
  84. '* Function intChkProgram()
  85. '* Purpose: Determines which program is used to run this script.
  86. '* Input:   None
  87. '* Output:  intChkProgram is set to one of CONST_ERROR, CONST_WSCRIPT,
  88. '*          and CONST_CSCRIPT.
  89. '*
  90. '********************************************************************
  91.  
  92. Private Function intChkProgram()
  93.  
  94.     ON ERROR RESUME NEXT
  95.  
  96.     Dim strFullName, strCommand, i, j
  97.  
  98.     'strFullName should be something like C:\WINDOWS\COMMAND\CSCRIPT.EXE
  99.     strFullName = WScript.FullName
  100.     If Err.Number then
  101.         Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred."
  102.         If Err.Description <> "" Then
  103.             Print "Error description: " & Err.Description & "."
  104.         End If
  105.         intChkProgram =  CONST_ERROR
  106.         Exit Function
  107.     End If
  108.  
  109.     i = InStr(1, strFullName, ".exe", 1)
  110.     If i = 0 Then
  111.         intChkProgram =  CONST_ERROR
  112.         Exit Function
  113.     Else
  114.         j = InStrRev(strFullName, "\", i, 1)
  115.         If j = 0 Then
  116.             intChkProgram =  CONST_ERROR
  117.             Exit Function
  118.         Else
  119.             strCommand = Mid(strFullName, j+1, i-j-1)
  120.             Select Case LCase(strCommand)
  121.                 Case "cscript"
  122.                     intChkProgram = CONST_CSCRIPT
  123.                 Case "wscript"
  124.                     intChkProgram = CONST_WSCRIPT
  125.                 Case Else       'should never happen
  126.                     Print "An unexpected program is used to run this script."
  127.                     Print "Only CScript.Exe or WScript.Exe can be used to run this script."
  128.                     intChkProgram = CONST_ERROR
  129.             End Select
  130.         End If
  131.     End If
  132.  
  133. End Function
  134.  
  135. '********************************************************************
  136. '*
  137. '* Function intParseCmdLine()
  138. '* Purpose: Parses the command line.
  139. '* Input:   strArgumentArray    an array containing input from the command line
  140. '* Output:  strADsPath          the ADsPath of the root
  141. '*          strUserName         name of the current user
  142. '*          strPassword         password of the current user
  143. '*          blnQuiet            specifies whether to suppress messages
  144. '*          strOutputFile       an output file name
  145. '*          intParseCmdLine     is set to one of CONST_ERROR, CONST_SHOW_USAGE, CONST_PROCEED.
  146. '*
  147. '********************************************************************
  148.  
  149. Private Function intParseCmdLine(strArgumentArray, strADsPath, blnQuiet, _
  150.     strUserName, strPassword, strOutputFile)
  151.  
  152.     ON ERROR RESUME NEXT
  153.  
  154.     Dim i, strFlag
  155.  
  156.     strFlag = strArgumentArray(0)
  157.  
  158.     If strFlag = "" Then                'No arguments have been received
  159.         Print "Arguments are required."
  160.         intParseCmdLine = CONST_ERROR
  161.         Exit Function
  162.     End If
  163.  
  164.     'Help is needed
  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 "/w"
  191.                     strPassword = Right(strArgumentArray(i), Len(strArgumentArray(i))-3)
  192.                 Case "/o"
  193.                     strOutputFile = 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.             End Select
  199.         End If
  200.     Next
  201.  
  202.     intParseCmdLine = CONST_PROCEED
  203.  
  204. End Function
  205.  
  206. '********************************************************************
  207. '*
  208. '* Sub ShowUsage()
  209. '* Purpose: Shows the correct usage to the user.
  210. '* Input:   None
  211. '* Output:  Help messages are displayed on screen.
  212. '*
  213. '********************************************************************
  214.  
  215. Private Sub ShowUsage()
  216.  
  217.     Wscript.Echo ""
  218.     Wscript.Echo "Lists all domains within a namespace." & vbCRLF
  219.     Wscript.Echo "LISTDOMAINS.VBS adspath [/O:outputfile]"
  220.     Wscript.Echo "[/U:username] [/W:password] [/Q]"
  221.     Wscript.Echo "   /O, /U, /W    Parameter specifiers."
  222.     Wscript.Echo "   adspath       The ADsPath of the namespace."
  223.     Wscript.Echo "   outputfile    The output file name."
  224.     Wscript.Echo "   username      Username of the current user."
  225.     Wscript.Echo "   password      Password of the current user."
  226.     Wscript.Echo "   /Q            Suppresses all output messages." & vbCRLF
  227.     Wscript.Echo "EXAMPLES:"
  228.     Wscript.Echo "1. LISTDOMAINS.VBS LDAP://dc=Foo,dc=com"
  229.     Wscript.Echo "   lists all domains within ""dc=Foo,dc=com""."
  230.     Wscript.Echo "2. LISTDOMAINS.VBS WinNT:"
  231.     Wscript.Echo "   lists all domains under WinNT:."
  232.  
  233. End Sub
  234.  
  235. '********************************************************************
  236. '*
  237. '* Sub GetDomains()
  238. '* Purpose: Lists all domains withing a namespace.
  239. '* Input:   strADsPath      the ADsPath of the root
  240. '*          strUserName     name of the current user
  241. '*          strPassword     password of the current user
  242. '*          blnQuiet        specifies whether to suppress messages
  243. '*          strOutputFile   an output file name
  244. '* Output:  The domain names are either printed on screen or saved in strOutputFile.
  245. '*
  246. '********************************************************************
  247.  
  248. Private Sub GetDomains(strADsPath, strUserName, strPassword, strOutputFile)
  249.  
  250.     ON ERROR RESUME NEXT
  251.  
  252.     Dim strProvider, objRoot, objDomain, objFile, strFileName, objOutputFile, i
  253.  
  254.     objOutputFile = ""
  255.  
  256.     If strOutputFile <> "" Then
  257.         'Create a filesystem object
  258.         set objFile = CreateObject("Scripting.FileSystemObject")
  259.         'Open the file for output
  260.         set objOutputFile = objFile.OpenTextFile(strOutputFile, 8, True)
  261.         If Err.Number then
  262.             Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred in opening file " _
  263.                 & strOutputFile
  264.             If Err.Description <> "" Then
  265.                 Print "Error description: " & Err.Description & "."
  266.             End If
  267.             Err.Clear
  268.             Exit Sub
  269.         End If
  270.     End If
  271.  
  272.     strProvider = Left(strADsPath, InStr(1, strADsPath, ":")-1)
  273.     If Err.Number then
  274.         Print "The provider """ & strADsPath & """ is not supported."
  275.         Print "Make sure add : at the end."
  276.         Err.Clear
  277.         Exit Sub
  278.     End If
  279.     Select Case strProvider
  280.         Case "WinNT"
  281.             Call GetDomainsWinNT(strADsPath, strUserName, strPassword, objOutputFile)
  282.         Case "LDAP"
  283.             Call GetDomainsLDAP(strADsPath, strUserName, strPassword, objOutputFile)
  284.         Case Else
  285.             Print "The provider """" & strProvider & """" is not supported."
  286.             Exit Sub
  287.     End Select
  288.  
  289.     If strOutputFile <> "" Then
  290.         objOutputFile.Close
  291.         Wscript.Echo "Results are saved in file " & strOutputFile & "."
  292.     End If
  293.  
  294. End Sub
  295.  
  296. '********************************************************************
  297. '*
  298. '* Sub GetDomainsLDAP()
  299. '* Purpose: Lists all domains under a root with LDAP provider.
  300. '* Input:   strADsPath      the ADsPath of the root
  301. '*          strUserName     name of the current user
  302. '*          strPassword     password of the current user
  303. '*          objOutputFile   an output file object
  304. '* Output:  The domain names are either printed on screen or saved in objOutputFile.
  305. '*
  306. '********************************************************************
  307.  
  308. Private Sub GetDomainsLDAP(strADsPath, strUserName, strPassword, objOutputFile)
  309.  
  310.     ON ERROR RESUME NEXT
  311.  
  312.     Dim objConnect, objCommand, objRecordSet, intCount
  313.     Dim strPathCopy, strCriteria, strProperties, strScope, k
  314.  
  315.     strPathCopy =  "<" & strADsPath & ">;"
  316.     strCriteria = "(ObjectClass=Domain);"
  317.     strProperties = "Name, ADsPath;"
  318.     strScope = "SubTree"
  319.  
  320.     Set objConnect = CreateObject("ADODB.Connection")
  321.     If Err.Number then
  322.         Print "Error 0x" & CStr(Hex(Err.Number)) & " ocurred in opening a connection."
  323.         If Err.Description <> "" Then
  324.             Print "Error description: " & Err.Description & "."
  325.         End If
  326.         Exit Sub
  327.     End If
  328.  
  329.     objConnect.Provider = "ADsDSOObject"
  330.  
  331.     If strUserName = "" then
  332.         objConnect.Open "Active Directory Provider"
  333.     Else
  334.         objConnect.Open "Active Directory Provider", strUserName, strPassword
  335.     End If
  336.  
  337.     If Err.Number then
  338.         Print "Error 0x" & CStr(Hex(Err.Number)) & " ocurred opening a provider."
  339.         If Err.Description <> "" Then
  340.             Print "Error description: " & Err.Description & "."
  341.         End If
  342.         Exit Sub
  343.     End If
  344.  
  345.     Set objCommand = CreateObject("ADODB.Command")
  346.     If Err.Number then
  347.         Print "Error 0x" & CStr(Hex(Err.Number)) & " ocurred in creating the command object."
  348.         If Err.Description <> "" Then
  349.             Print "Error description: " & Err.Description & "."
  350.         End If
  351.         Exit Sub
  352.     End If
  353.  
  354.     Set objCommand.ActiveConnection = objConnect
  355.  
  356.     'Set the query string
  357.     objCommand.CommandText  = strPathCopy & strCriteria & strProperties & strScope
  358.     objCommand.Properties("Page Size") = 100000                    'reset search properties
  359.     objCommand.Properties("Timeout") = 300000 'seconds
  360.     'objCommand.Properties("SearchScope") = 2
  361.  
  362.     'Let the user know what is going on
  363.     Print "Start query: " & objCommand.CommandText
  364.     'Execute the query
  365.     Set objRecordSet = objCommand.Execute
  366.     If Err.Number then
  367.         Print "Error 0x" & CStr(Hex(Err.Number)) & " ocurred during the query."
  368.         If Err.Description <> "" Then
  369.             Print "Error description: " & Err.Description & "."
  370.         End If
  371.         Print "Clear the error and continue."
  372.         Err.Clear
  373.     End If
  374.     Print "Finished the query."
  375.  
  376.     'Get the total number of objects found.
  377.     objRecordSet.MoveLast
  378.     intCount = objRecordSet.RecordCount
  379.  
  380.     If intCount Then                'If intCount is not zero
  381.         Print "Found " & intCount & " domains."
  382.         objRecordSet.MoveFirst
  383.         k = 0
  384.         While Not objRecordSet.EOF
  385.             k = k + 1
  386.             WriteLine objRecordSet.Fields(0).Value, objOutputFile
  387.             objRecordSet.MoveNext
  388.         Wend
  389.         Print "Results are saved in file """ & strFileName & """."
  390.     Else
  391.         Print "There is no domain within " & strADsPath & "."
  392.     End If
  393.  
  394. End Sub
  395.  
  396. '********************************************************************
  397. '*
  398. '* Sub GetDomainsWinNT()
  399. '* Purpose: Lists all domains under a root with WinNT provider.
  400. '* Input:   strADsPath      the ADsPath of the root
  401. '*          strUserName     name of the current user
  402. '*          strPassword     password of the current user
  403. '*          objOutputFile   an output file object
  404. '* Output:  The domain names are either printed on screen or saved in objOutputFile.
  405. '*
  406. '********************************************************************
  407.  
  408. Private Sub GetDomainsWinNT(strADsPath, strUserName, strPassword, objOutputFile)
  409.  
  410.     ON ERROR RESUME NEXT
  411.  
  412.     Dim objRoot, objDomain, strProvider, objProvider, i
  413.  
  414.     Print "Looking for domains in " & strADsPath & "..."
  415.     If strUserName = ""    then        'The current user is assumed
  416.         set objRoot = GetObject(strADsPath)
  417.     Else
  418.         'Credentials are passed
  419.         strProvider = Left(strADsPath, InStr(1, strADsPath, ":"))
  420.         set objProvider = GetObject(strProvider)
  421.         'Use user authentication
  422.         set objRoot = objProvider.OpenDsObject(strADsPath,strUserName,strPassword,1)
  423.     End If
  424.     If Err.Number then
  425.         If CStr(Hex(Err.Number)) = "80070035" Then
  426.             Print "Object " & strADsPath & " is not found."
  427.         Else
  428.             Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred in getting object " _
  429.                 & strADsPath & "."
  430.             If Err.Description <> "" Then
  431.                 Print "Error description: " & Err.Description & "."
  432.             End If
  433.         End If
  434.         Err.Clear
  435.         Exit Sub
  436.     End If
  437.  
  438.     objRoot.Filter = Array("domain")
  439.     i = 0
  440.     For each objDomain in objRoot
  441.         If Err.Number then
  442.             Err.Clear
  443.         Else
  444.             i = i + 1
  445.             WriteLine objDomain.Name, objOutputFile
  446.         End If
  447.     Next
  448.  
  449.     If i = 0 Then
  450.         Print "There is no domain under " & strADsPath & "."
  451.     Else
  452.         Print "There are " & i & " domains under " & strADsPath & "."
  453.     End If
  454.  
  455. End Sub
  456.  
  457. '********************************************************************
  458. '*
  459. '* Sub WriteLine()
  460. '* Purpose: Writes a text line either to a file or on screen.
  461. '* Input:   strMessage  the string to print
  462. '*          objFile     an output file object
  463. '* Output:  strMessage is either displayed on screen or written to a file.
  464. '*
  465. '********************************************************************
  466.  
  467. Sub WriteLine(ByRef strMessage, ByRef objFile)
  468.  
  469.     If IsObject(objFile) then        'objFile should be a file object
  470.         objFile.WriteLine strMessage
  471.     Else
  472.         Wscript.Echo  strMessage
  473.     End If
  474.  
  475. End Sub
  476.  
  477. '********************************************************************
  478. '*
  479. '* Sub Print()
  480. '* Purpose: Prints a message on screen if blnQuiet = False.
  481. '* Input:   strMessage      the string to print
  482. '* Output:  strMessage is printed on screen if blnQuiet = False.
  483. '*
  484. '********************************************************************
  485.  
  486. Sub Print(ByRef strMessage)
  487.     If Not blnQuiet then
  488.         Wscript.Echo  strMessage
  489.     End If
  490. End Sub
  491.  
  492. '********************************************************************
  493. '*                                                                  *
  494. '*                           End of File                            *
  495. '*                                                                  *
  496. '********************************************************************
  497.  
  498. '********************************************************************
  499. '*
  500. '* Procedures calling sequence: LISTDOMAINS.VBS
  501. '*
  502. '*  intChkProgram
  503. '*  intParseCmdLine
  504. '*  ShowUsage
  505. '*  GetDomains
  506. '*      GetDomainsWinNT
  507. '*          WriteLine
  508. '*      GetDomainsLDAP
  509. '*          WriteLine
  510. '*
  511. '********************************************************************
  512.