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

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