home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 Special / chip-cd_2001_spec_05.zip / spec_05 / ras.cab / enumnamespaces.vbs < prev    next >
Text File  |  1999-11-04  |  22KB  |  645 lines

  1. '********************************************************************
  2. '*
  3. '* File:           EnumNameSpaces.VBS 
  4. '* Created:        March 1999
  5. '* Version:        1.0
  6. '*
  7. '*  Main Function:  Enumerates WBEM namespaces on a server.
  8. '*
  9. '*  EnumNameSpaces.vbs [/S <server>] [/U <username>] "
  10.  '*                    [/W <password>] [/O <outputfile>]"
  11. '*
  12. '* Copyright (C) 1999 Microsoft Corporation
  13. '*
  14. '********************************************************************
  15.  
  16. OPTION EXPLICIT
  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 strServer, strUserName, strPassword, strOutputFile
  28.     Dim strNameSpace, strClass
  29.     Dim blnRecursive
  30.  
  31.     'Make sure the host is csript, if not then abort
  32.     VerifyHostIsCscript()
  33.  
  34.     'Parse the command line
  35.     intOpMode = intParseCmdLine(strServer     ,  _
  36.                                 strUserName   ,  _
  37.                                 strPassword   ,  _
  38.                                 strOutputFile    )
  39.  
  40.  
  41.     Select Case intOpMode
  42.  
  43.         Case CONST_SHOW_USAGE
  44.             Call ShowUsage()
  45.  
  46.         Case CONST_PROCEED                 
  47.             Call EnumNameSpaces(strServer     ,  _
  48.                                 strUserName   ,  _
  49.                                 strPassword   ,  _
  50.                                 strOutputFile    )
  51.  
  52.  
  53.         Case CONST_ERROR
  54.             'Do Nothing
  55.  
  56.         Case Else                    'Default -- should never happen
  57.             Call Wscript.Echo("Error occurred in passing parameters.")
  58.  
  59.     End Select
  60.  
  61. '********************************************************************
  62. '* End of Script
  63. '********************************************************************
  64. '********************************************************************
  65. '*
  66. '* Sub EnumNameSpaces()
  67. '* Purpose: Enumerates WBEM classes on a server.
  68. '* Input:   strServer is the name of the machine to be checked
  69. '*          strUserName     the current user's name
  70. '*          strPassword     the current user's password
  71. '*          strOutputFile   an output file name
  72. '* Output:  Results of the search are either printed on screen or saved 
  73. '*          in strOutputFile.
  74. '*
  75. '********************************************************************
  76.  
  77. Private Sub EnumNameSpaces(strServer, strUserName, strPassword, strOutputFile)
  78.  
  79.     ON ERROR RESUME NEXT
  80.  
  81.     Dim objFileSystem, objOutputFile, objService
  82.  
  83.     'Open a text file for output if the file is requested
  84.     If Not IsEmpty(strOutputFile) Then
  85.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  86.             Call Wscript.Echo ("Could not open an output file.")
  87.             Exit Sub
  88.         End If
  89.     End If
  90.  
  91.     If blnConnect("Root"       , _
  92.                    strUserName , _
  93.                    strPassword , _
  94.                    strServer   , _
  95.                    objService    ) Then
  96.         Call Wscript.Echo("")
  97.         Call Wscript.Echo("Please check the server name, " _
  98.                         & "credentials and WBEM Core.")
  99.         Exit Sub
  100.     End If
  101.     
  102.     Call WriteLine("Namespaces on machine " & strServer & ":", objOutputFile)
  103.     Call WriteLine("", objOutputFile)
  104.  
  105.     'Enumerate the namespace recursively
  106.     WriteLine "root", objOutputFile
  107.     Call EnumNameSpaceRecursive(objService, "root", 1, objOutputFile)
  108.  
  109.     If IsObject(objOutputFile) Then
  110.         objOutputFile.Close
  111.         Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
  112.     End If
  113.  
  114. End Sub
  115.  
  116. '********************************************************************
  117. '*
  118. '* Sub EnumNameSpaceRecursive()
  119. '* Purpose: Recursively queries subclasses of a class.
  120. '* Input:   objService      the service object
  121. '*          strNameSpace    the name of a namespace
  122. '*          intLevel        the level of then recursion
  123. '*          objOutputFile   an output file object
  124. '* Output:  Results are either printed on screen or saved in objOutputFile.
  125. '*
  126. '********************************************************************
  127.  
  128. Private Sub EnumNameSpaceRecursive(objService         ,  _
  129.                                   ByVal strNameSpace  ,  _
  130.                                   intLevel            ,  _
  131.                                   objOutputFile          )
  132.  
  133.     ON ERROR RESUME NEXT
  134.  
  135.     Dim strName, colNameSpaces, objNameSpace, strNewNameSpace
  136.  
  137.     Set colNameSpaces = objService.InstancesOf ("__NAMESPACE")
  138.     For Each objNameSpace In colNameSpaces
  139.         strName = objNameSpace.Name
  140.         strNewNameSpace = strNameSpace & "\" & strName
  141.         set objService = GetObject("winmgmts:" & strNewNameSpace)
  142.         WriteLine Space(6*intLevel) & strName, objOutputFile
  143.         Call EnumNameSpaceRecursive (objService, strNewNameSpace, _
  144.                                      intLevel+1, objOutputFile)
  145.     Next
  146.  
  147. End Sub
  148.  
  149. '********************************************************************
  150. '*
  151. '* Function intParseCmdLine()
  152. '*
  153. '* Purpose: Parses the command line.
  154. '* Input:   
  155. '*
  156. '* Output:  strServer         a remote server ("" = local server")
  157. '*          strUserName       the current user's name
  158. '*          strPassword       the current user's password
  159. '*          strOutputFile     an output file name
  160. '*          strNameSpace      a namespace
  161. '*          strClass          a class name
  162. '*          blnRecursive      specifies a recursive enumeration
  163.  
  164. '*
  165. '********************************************************************
  166. Private Function intParseCmdLine( ByRef strServer     ,  _
  167.                                   ByRef strUserName   ,  _
  168.                                   ByRef strPassword   ,  _
  169.                                   ByRef strOutputFile    )
  170.  
  171.  
  172.  
  173.     ON ERROR RESUME NEXT
  174.  
  175.     Dim strFlag
  176.     Dim intState, intArgIter
  177.     Dim objFileSystem
  178.  
  179.     If Wscript.Arguments.Count > 0 Then
  180.         strFlag = Wscript.arguments.Item(0)
  181.     End If
  182.  
  183.     If IsEmpty(strFlag) Then                'No arguments have been received
  184.         intParseCmdLine = CONST_PROCEED
  185.         Exit Function
  186.     End If
  187.  
  188.     'Check if the user is asking for help or is just confused
  189.     If (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
  190.         OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "?") _ 
  191.         OR (strFlag="h") Then
  192.         intParseCmdLine = CONST_SHOW_USAGE
  193.         Exit Function
  194.     End If
  195.  
  196.     'Retrieve the command line and set appropriate variables
  197.      intArgIter = 0
  198.     Do While intArgIter <= Wscript.arguments.Count - 1
  199.         Select Case Left(LCase(Wscript.arguments.Item(intArgIter)),2)
  200.   
  201.             Case "/s"
  202.                 If Not blnGetArg("Server", strServer, intArgIter) Then
  203.                     intParseCmdLine = CONST_ERROR
  204.                     Exit Function
  205.                 End If
  206.                 intArgIter = intArgIter + 1
  207.  
  208.             Case "/o"
  209.                 If Not blnGetArg("Output File", strOutputFile, intArgIter) Then
  210.                     intParseCmdLine = CONST_ERROR
  211.                     Exit Function
  212.                 End If
  213.                 intArgIter = intArgIter + 1
  214.  
  215.             Case "/u"
  216.                 If Not blnGetArg("User Name", strUserName, intArgIter) Then
  217.                     intParseCmdLine = CONST_ERROR
  218.                     Exit Function
  219.                 End If
  220.                 intArgIter = intArgIter + 1
  221.  
  222.             Case "/w"
  223.                 If Not blnGetArg("User Password", strPassword, intArgIter) Then
  224.                     intParseCmdLine = CONST_ERROR
  225.                     Exit Function
  226.                 End If
  227.                 intArgIter = intArgIter + 1
  228.  
  229.             Case Else 'We shouldn't get here
  230.                 Call Wscript.Echo("Invalid or misplaced parameter: " _
  231.                    & Wscript.arguments.Item(intArgIter) & vbCRLF _
  232.                    & "Please check the input and try again," & vbCRLF _
  233.                    & "or invoke with '/?' for help with the syntax.")
  234.                 Wscript.Quit
  235.  
  236.         End Select
  237.  
  238.     Loop '** intArgIter <= Wscript.arguments.Count - 1
  239.  
  240.     If IsEmpty(intParseCmdLine) Then _
  241.         intParseCmdLine = CONST_PROCEED
  242.  
  243. End Function
  244.  
  245.  
  246. '********************************************************************
  247. '*
  248. '* Sub ShowUsage()
  249. '*
  250. '* Purpose: Shows the correct usage to the user.
  251. '*
  252. '* Input:   None
  253. '*
  254. '* Output:  Help messages are displayed on screen.
  255. '*
  256. '********************************************************************
  257. Private Sub ShowUsage()
  258.  
  259.     Wscript.Echo ""
  260.     Wscript.Echo "Enumerates WBEM namespaces on a server." 
  261.     Wscript.Echo ""
  262.     Wscript.Echo "SYNTAX:"
  263.     Wscript.Echo "  EnumNameSpaces.vbs [/S <server>] [/U <username>] "
  264.     Wscript.Echo "                     [/W <password>] [/O <outputfile>]"
  265.     Wscript.Echo ""
  266.     Wscript.Echo "PARAMETER SPECIFIERS:"
  267.     Wscript.Echo "   server        A machine name."
  268.     Wscript.Echo "   username      The current user's name."
  269.     Wscript.Echo "   password      Password of the current user."
  270.     Wscript.Echo "   outputfile    The output file name."
  271.     Wscript.Echo ""
  272.     Wscript.Echo "EXAMPLE:"
  273.     Wscript.Echo "1. cscript EnumNameSpaces.vbs"
  274.     Wscript.Echo "     Lists all NameSpaces on the local machine."
  275.     Wscript.Echo "1. cscript EnumNameSpaces.vbs /s MyMachine2"
  276.     Wscript.Echo "     Lists all NameSpaces on MyMachine2."
  277.  
  278. End Sub
  279.  
  280. '********************************************************************
  281. '* General Routines
  282. '********************************************************************
  283.  
  284. '********************************************************************
  285. '*
  286. '* Function strPackString()
  287. '*
  288. '* Purpose: Attaches spaces to a string to increase the length to intWidth.
  289. '*
  290. '* Input:   strString   a string
  291. '*          intWidth    the intended length of the string
  292. '*          blnAfter    Should spaces be added after the string?
  293. '*          blnTruncate specifies whether to truncate the string or not if
  294. '*                      the string length is longer than intWidth
  295. '*
  296. '* Output:  strPackString is returned as the packed string.
  297. '*
  298. '********************************************************************
  299. Private Function strPackString( ByVal strString, _
  300.                                 ByVal intWidth,  _
  301.                                 ByVal blnAfter,  _
  302.                                 ByVal blnTruncate)
  303.  
  304.     ON ERROR RESUME NEXT
  305.  
  306.     intWidth      = CInt(intWidth)
  307.     blnAfter      = CBool(blnAfter)
  308.     blnTruncate   = CBool(blnTruncate)
  309.  
  310.     If Err.Number Then
  311.         Call Wscript.Echo ("Argument type is incorrect!")
  312.         Err.Clear
  313.         Wscript.Quit
  314.     End If
  315.  
  316.     If IsNull(strString) Then
  317.         strPackString = "null" & Space(intWidth-4)
  318.         Exit Function
  319.     End If
  320.  
  321.     strString = CStr(strString)
  322.     If Err.Number Then
  323.         Call Wscript.Echo ("Argument type is incorrect!")
  324.         Err.Clear
  325.         Wscript.Quit
  326.     End If
  327.  
  328.     If intWidth > Len(strString) Then
  329.         If blnAfter Then
  330.             strPackString = strString & Space(intWidth-Len(strString))
  331.         Else
  332.             strPackString = Space(intWidth-Len(strString)) & strString & " "
  333.         End If
  334.     Else
  335.         If blnTruncate Then
  336.             strPackString = Left(strString, intWidth-1) & " "
  337.         Else
  338.             strPackString = strString & " "
  339.         End If
  340.     End If
  341.  
  342. End Function
  343.  
  344. '********************************************************************
  345. '* 
  346. '*  Function blnGetArg()
  347. '*
  348. '*  Purpose: Helper to intParseCmdLine()
  349. '* 
  350. '*  Usage:
  351. '*
  352. '*     Case "/s" 
  353. '*       blnGetArg ("server name", strServer, intArgIter)
  354. '*
  355. '********************************************************************
  356. Private Function blnGetArg ( ByVal StrVarName,   _
  357.                              ByRef strVar,       _
  358.                              ByRef intArgIter) 
  359.  
  360.     blnGetArg = False 'failure, changed to True upon successful completion
  361.  
  362.     If Len(Wscript.Arguments(intArgIter)) > 2 then
  363.         If Mid(Wscript.Arguments(intArgIter),3,1) = ":" then
  364.             If Len(Wscript.Arguments(intArgIter)) > 3 then
  365.                 strVar = Right(Wscript.Arguments(intArgIter), _
  366.                          Len(Wscript.Arguments(intArgIter)) - 3)
  367.                 blnGetArg = True
  368.                 Exit Function
  369.             Else
  370.                 intArgIter = intArgIter + 1
  371.                 If intArgIter > (Wscript.Arguments.Count - 1) Then
  372.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  373.                     Call Wscript.Echo( "Please check the input and try again.")
  374.                     Exit Function
  375.                 End If
  376.  
  377.                 strVar = Wscript.Arguments.Item(intArgIter)
  378.                 If Err.Number Then
  379.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  380.                     Call Wscript.Echo( "Please check the input and try again.")
  381.                     Exit Function
  382.                 End If
  383.  
  384.                 If InStr(strVar, "/") Then
  385.                     Call Wscript.Echo( "Invalid " & StrVarName)
  386.                     Call Wscript.Echo( "Please check the input and try again.")
  387.                     Exit Function
  388.                 End If
  389.  
  390.                 blnGetArg = True 'success
  391.             End If
  392.         Else
  393.             strVar = Right(Wscript.Arguments(intArgIter), _
  394.                      Len(Wscript.Arguments(intArgIter)) - 2)
  395.             blnGetArg = True 'success
  396.             Exit Function
  397.         End If
  398.     Else
  399.         intArgIter = intArgIter + 1
  400.         If intArgIter > (Wscript.Arguments.Count - 1) Then
  401.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  402.             Call Wscript.Echo( "Please check the input and try again.")
  403.             Exit Function
  404.         End If
  405.  
  406.         strVar = Wscript.Arguments.Item(intArgIter)
  407.         If Err.Number Then
  408.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  409.             Call Wscript.Echo( "Please check the input and try again.")
  410.             Exit Function
  411.         End If
  412.  
  413.         If InStr(strVar, "/") Then
  414.             Call Wscript.Echo( "Invalid " & StrVarName)
  415.             Call Wscript.Echo( "Please check the input and try again.")
  416.             Exit Function
  417.         End If
  418.         blnGetArg = True 'success
  419.     End If
  420. End Function
  421.  
  422. '********************************************************************
  423. '*
  424. '* Function blnConnect()
  425. '*
  426. '* Purpose: Connects to machine strServer.
  427. '*
  428. '* Input:   strServer       a machine name
  429. '*          strNameSpace    a namespace
  430. '*          strUserName     name of the current user
  431. '*          strPassword     password of the current user
  432. '*
  433. '* Output:  objService is returned  as a service object.
  434. '*          strServer is set to local host if left unspecified
  435. '*
  436. '********************************************************************
  437. Private Function blnConnect(ByVal strNameSpace, _
  438.                             ByVal strUserName,  _
  439.                             ByVal strPassword,  _
  440.                             ByRef strServer,    _
  441.                             ByRef objService)
  442.  
  443.     ON ERROR RESUME NEXT
  444.  
  445.     Dim objLocator, objWshNet
  446.  
  447.     blnConnect = False     'There is no error.
  448.  
  449.     'Create Locator object to connect to remote CIM object manager
  450.     Set objLocator = CreateObject("WbemScripting.SWbemLocator")
  451.     If Err.Number then
  452.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  453.                            " occurred in creating a locator object." )
  454.         If Err.Description <> "" Then
  455.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  456.         End If
  457.         Err.Clear
  458.         blnConnect = True     'An error occurred
  459.         Exit Function
  460.     End If
  461.  
  462.     'Connect to the namespace which is either local or remote
  463.     Set objService = objLocator.ConnectServer (strServer, strNameSpace, _
  464.        strUserName, strPassword)
  465.     ObjService.Security_.impersonationlevel = 3
  466.     If Err.Number then
  467.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  468.                            " occurred in connecting to server " _
  469.            & strServer & ".")
  470.         If Err.Description <> "" Then
  471.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  472.         End If
  473.         Err.Clear
  474.         blnConnect = True     'An error occurred
  475.     End If
  476.  
  477.     'Get the current server's name if left unspecified
  478.     If IsEmpty(strServer) Then
  479.         Set objWshNet = CreateObject("Wscript.Network")
  480.     strServer     = objWshNet.ComputerName
  481.     End If
  482.  
  483. End Function
  484.  
  485. '********************************************************************
  486. '*
  487. '* Sub      VerifyHostIsCscript()
  488. '*
  489. '* Purpose: Determines which program is used to run this script.
  490. '*
  491. '* Input:   None
  492. '*
  493. '* Output:  If host is not cscript, then an error message is printed 
  494. '*          and the script is aborted.
  495. '*
  496. '********************************************************************
  497. Sub VerifyHostIsCscript()
  498.  
  499.     ON ERROR RESUME NEXT
  500.  
  501.     Dim strFullName, strCommand, i, j, intStatus
  502.  
  503.     strFullName = WScript.FullName
  504.  
  505.     If Err.Number then
  506.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & " occurred." )
  507.         If Err.Description <> "" Then
  508.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  509.         End If
  510.         intStatus =  CONST_ERROR
  511.     End If
  512.  
  513.     i = InStr(1, strFullName, ".exe", 1)
  514.     If i = 0 Then
  515.         intStatus =  CONST_ERROR
  516.     Else
  517.         j = InStrRev(strFullName, "\", i, 1)
  518.         If j = 0 Then
  519.             intStatus =  CONST_ERROR
  520.         Else
  521.             strCommand = Mid(strFullName, j+1, i-j-1)
  522.             Select Case LCase(strCommand)
  523.                 Case "cscript"
  524.                     intStatus = CONST_CSCRIPT
  525.                 Case "wscript"
  526.                     intStatus = CONST_WSCRIPT
  527.                 Case Else       'should never happen
  528.                     Call Wscript.Echo( "An unexpected program was used to " _
  529.                                        & "run this script." )
  530.                     Call Wscript.Echo( "Only CScript.Exe or WScript.Exe can " _
  531.                                        & "be used to run this script." )
  532.                     intStatus = CONST_ERROR
  533.                 End Select
  534.         End If
  535.     End If
  536.  
  537.     If intStatus <> CONST_CSCRIPT Then
  538.         Call WScript.Echo( "Please run this script using CScript." & vbCRLF & _
  539.              "This can be achieved by" & vbCRLF & _
  540.              "1. Using ""CScript EnumNameSpaces.vbs arguments"" for Windows 95/98 or" _
  541.              & vbCRLF & "2. Changing the default Windows Scripting Host " _
  542.              & "setting to CScript" & vbCRLF & "    using ""CScript " _
  543.              & "//H:CScript //S"" and running the script using" & vbCRLF & _
  544.              "    ""EnumNameSpaces.vbs arguments"" for Windows NT/2000." )
  545.         WScript.Quit
  546.     End If
  547.  
  548. End Sub
  549.  
  550. '********************************************************************
  551. '*
  552. '* Sub WriteLine()
  553. '* Purpose: Writes a text line either to a file or on screen.
  554. '* Input:   strMessage  the string to print
  555. '*          objFile     an output file object
  556. '* Output:  strMessage is either displayed on screen or written to a file.
  557. '*
  558. '********************************************************************
  559. Sub WriteLine(ByVal strMessage, ByVal objFile)
  560.  
  561.     On Error Resume Next
  562.     If IsObject(objFile) then        'objFile should be a file object
  563.         objFile.WriteLine strMessage
  564.     Else
  565.         Call Wscript.Echo( strMessage )
  566.     End If
  567.  
  568. End Sub
  569.  
  570. '********************************************************************
  571. '* 
  572. '* Function blnErrorOccurred()
  573. '*
  574. '* Purpose: Reports error with a string saying what the error occurred in.
  575. '*
  576. '* Input:   strIn        string saying what the error occurred in.
  577. '*
  578. '* Output:  displayed on screen 
  579. '* 
  580. '********************************************************************
  581. Private Function blnErrorOccurred (ByVal strIn)
  582.  
  583.     If Err.Number Then
  584.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & ": " & strIn)
  585.         If Err.Description <> "" Then
  586.             Call Wscript.Echo( "Error description: " & Err.Description)
  587.         End If
  588.         Err.Clear
  589.         blnErrorOccurred = True
  590.     Else
  591.         blnErrorOccurred = False
  592.     End If
  593.  
  594. End Function
  595.  
  596. '********************************************************************
  597. '* 
  598. '* Function blnOpenFile
  599. '*
  600. '* Purpose: Opens a file.
  601. '*
  602. '* Input:   strFileName        A string with the name of the file.
  603. '*
  604. '* Output:  Sets objOpenFile to a FileSystemObject and setis it to 
  605. '*            Nothing upon Failure.
  606. '* 
  607. '********************************************************************
  608. Private Function blnOpenFile(ByVal strFileName, ByRef objOpenFile)
  609.  
  610.     ON ERROR RESUME NEXT
  611.  
  612.     Dim objFileSystem
  613.  
  614.     Set objFileSystem = Nothing
  615.  
  616.     If IsEmpty(strFileName) OR strFileName = "" Then
  617.         blnOpenFile = False
  618.         Set objOpenFile = Nothing
  619.         Exit Function
  620.     End If
  621.  
  622.     'Create a file object
  623.     Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  624.     If blnErrorOccurred("Could not create filesystem object.") Then
  625.         blnOpenFile = False
  626.         Set objOpenFile = Nothing
  627.         Exit Function
  628.     End If
  629.  
  630.     'Open the file for output
  631.     Set objOpenFile = objFileSystem.OpenTextFile(strFileName, 8, True)
  632.     If blnErrorOccurred("Could not open") Then
  633.         blnOpenFile = False
  634.         Set objOpenFile = Nothing
  635.         Exit Function
  636.     End If
  637.     blnOpenFile = True
  638.  
  639. End Function
  640.  
  641. '********************************************************************
  642. '*                                                                  *
  643. '*                           End of File                            *
  644. '*                                                                  *
  645. '********************************************************************