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

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