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

  1. '********************************************************************
  2. '*
  3. '* File:           Restart.vbs
  4. '* Created:        March 1999
  5. '* Version:        1.0
  6. '*
  7. '*  Main Function:  Shutsdown, PowerOff, LogOff, Restarts a machine.
  8. '*
  9. '*  Restart.vbs    /S <server> [/U <username>] [/W <password>] 
  10. '*                 [/O <outputfile>] [/L} [/P] [/R] [/Q] [/F] [/T <time in seconds>]
  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.     'Shutdown Method Constants
  26.     CONST CONST_SHUTDOWN                = 1
  27.     CONST CONST_LOGOFF                  = 0
  28.     CONST CONST_POWEROFF                = 8
  29.     CONST CONST_REBOOT                  = 2
  30.     CONST CONST_FORCE_REBOOT            = 6
  31.     CONST CONST_FORCE_POWEROFF          = 12
  32.     CONST CONST_FORCE_LOGOFF            = 4
  33.     CONST CONST_FORCE_SHUTDOWN          = 5
  34.      
  35.     'Declare variables
  36.     Dim intOpMode, i
  37.     Dim strServer, strUserName, strPassword, strOutputFile
  38.     Dim blnLogoff, blnPowerOff, blnReBoot, blnShutDown
  39.     Dim blnForce
  40.     Dim intTimer
  41.     Dim UserArray(3)
  42.     Dim MyCount
  43.  
  44.     'Make sure the host is csript, if not then abort
  45.     VerifyHostIsCscript()
  46.  
  47.     'Parse the command line
  48.     intOpMode = intParseCmdLine(strServer     ,  _
  49.                                 strUserName   ,  _
  50.                                 strPassword   ,  _
  51.                                 strOutputFile ,  _
  52.                                 blnLogoff     ,  _
  53.                                 blnPowerOff   ,  _
  54.                                 blnReBoot     ,  _
  55.                                 blnShutdown   ,  _
  56.                                 blnForce      ,  _
  57.                                 intTimer         )
  58.  
  59.     Select Case intOpMode
  60.  
  61.         Case CONST_SHOW_USAGE
  62.             Call ShowUsage()
  63.         
  64.         Case CONST_PROCEED                 
  65.             Call Reboot(strServer     , _
  66.                           strOutputFile , _
  67.                           strUserName   , _
  68.                           strPassword   , _
  69.                           blnReboot     , _
  70.                           blnForce      , _
  71.                           intTimer        )
  72.  
  73.             Call LogOff(strServer     , _
  74.                           strOutputFile , _
  75.                           strUserName   , _
  76.                           strPassword   , _
  77.                           blnLogoff     , _
  78.                           blnForce      , _
  79.                           intTimer        )
  80.  
  81.             Call PowerOff(strServer     , _
  82.                           strOutputFile , _
  83.                           strUserName   , _
  84.                           strPassword   , _
  85.                           blnPowerOff   , _
  86.                           blnForce      , _
  87.                           intTimer        )
  88.  
  89.             Call ShutDown(strServer     , _
  90.                           strOutputFile , _
  91.                           strUserName   , _
  92.                           strPassword   , _
  93.                           blnShutDown   , _
  94.                           blnForce      , _
  95.                           intTimer        )
  96.  
  97.         Case CONST_ERROR
  98.             'Do Nothing
  99.  
  100.         Case Else                    'Default -- should never happen
  101.             Call Wscript.Echo("Error occurred in passing parameters.")
  102.  
  103.     End Select
  104.  
  105.  
  106. '********************************************************************
  107. '*
  108. '* Sub Reboot()
  109. '*
  110. '* Purpose: Reboots a machine.
  111. '*
  112. '* Input:   strServer           a machine name
  113. '*          strOutputFile       an output file name
  114. '*          strUserName         the current user's name
  115. '*          strPassword         the current user's password
  116. '*          blnForce            specifies whether to force the logoff
  117. '*          intTimer            specifies the amount of time to perform the function
  118. '*
  119. '* Output:  Results are either printed on screen or saved in strOutputFile.
  120. '*
  121. '********************************************************************
  122. Private Sub Reboot(strServer, strOutputFile, strUserName, strPassword, blnReboot, blnForce, intTimer)
  123.  
  124.  
  125.     ON ERROR RESUME NEXT
  126.  
  127.     Dim objFileSystem, objOutputFile, objService, objEnumerator, objInstance
  128.     Dim strQuery, strMessage
  129.     Dim intStatus
  130.     ReDim strID(0), strName(0)
  131.  
  132.     if blnreboot = false then
  133.          Exit Sub
  134.     End if
  135.  
  136.     if intTimer > 0 then
  137.         wscript.echo "Rebooting machine " & strServer & " in " & intTimer & " seconds..."
  138.         wscript.sleep (intTimer * 1000)
  139.     End if
  140.  
  141.     'Open a text file for output if the file is requested
  142.     If Not IsEmpty(strOutputFile) Then
  143.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  144.             Call Wscript.Echo ("Could not open an output file.")
  145.             Exit Sub
  146.         End If
  147.     End If
  148.  
  149.     'Establish a connection with the server.
  150.     If blnConnect("root\cimv2" , _
  151.                    strUserName , _
  152.                    strPassword , _
  153.                    strServer   , _
  154.                    objService  ) Then
  155.         Call Wscript.Echo("")
  156.         Call Wscript.Echo("Please check the server name, " _
  157.                         & "credentials and WBEM Core.")
  158.         Exit Sub
  159.     End If
  160.  
  161.     strID(0) = ""
  162.     strName(0) = ""
  163.     strMessage = ""
  164.     strQuery = "Select * From Win32_OperatingSystem"
  165.  
  166.     Set objEnumerator = objService.ExecQuery(strQuery,,0)
  167.     If Err.Number Then
  168.         Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred during the query."
  169.         If Err.Description <> "" Then
  170.             Print "Error description: " & Err.Description & "."
  171.         End If
  172.         Err.Clear
  173.         Exit Sub
  174.     End If
  175.  
  176.     i = 0
  177.     For Each objInstance in objEnumerator
  178.         If blnForce Then
  179.             intStatus = objInstance.Win32ShutDown(CONST_FORCE_REBOOT)
  180.         Else
  181.             intStatus = objInstance.Win32ShutDown(CONST_REBOOT)
  182.         End If
  183.  
  184.         IF intStatus = 0 Then
  185.             strMessage = "Reboot a machine " & strServer & "."
  186.         Else
  187.             strMessage = "Failed to reboot a machine " & strServer & "."
  188.         End If
  189.         Call WriteLine(strMessage,objOutputFile)
  190.     Next
  191.  
  192.     If IsObject(objOutputFile) Then
  193.         objOutputFile.Close
  194.         Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
  195.     End If
  196. End Sub
  197.  
  198.  
  199. '********************************************************************
  200. '*
  201. '* Sub LogOff()
  202. '*
  203. '* Purpose: Logs off the user currently logged onto a machine.
  204. '*
  205. '* Input:   strServer           a machine name
  206. '*          strOutputFile       an output file name
  207. '*          strUserName         the current user's name
  208. '*          strPassword         the current user's password
  209. '*          blnForce            specifies whether to force the logoff
  210. '*          intTimer            specifies the amount of time to preform the function
  211. '*
  212. '* Output:  Results are either printed on screen or saved in strOutputFile.
  213. '*
  214. '********************************************************************
  215. Private Sub LogOff(strServer, strOutputFile, strUserName, strPassword, blnLogoff, blnForce, intTimer)
  216.  
  217.  
  218.     ON ERROR RESUME NEXT
  219.  
  220.     Dim objFileSystem, objOutputFile, objService, objEnumerator, objInstance
  221.     Dim strQuery, strMessage
  222.     Dim intStatus
  223.     ReDim strID(0), strName(0)
  224.  
  225.      If blnlogoff = false then
  226.           Exit Sub
  227.      End if
  228.  
  229.     if intTimer > 1 then 
  230.      wscript.echo "Logging off machine " & strServer & " in " & intTimer & " seconds..."
  231.         wscript.sleep (intTimer * 1000)
  232.     End if
  233.  
  234.     'Open a text file for output if the file is requested
  235.     If Not IsEmpty(strOutputFile) Then
  236.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  237.             Call Wscript.Echo ("Could not open an output file.")
  238.             Exit Sub
  239.         End If
  240.     End If
  241.  
  242.     'Establish a connection with the server.
  243.     If blnConnect("root\cimv2" , _
  244.                    strUserName , _
  245.                    strPassword , _
  246.                    strServer   , _
  247.                    objService  ) Then
  248.         Call Wscript.Echo("")
  249.         Call Wscript.Echo("Please check the server name, " _
  250.                         & "credentials and WBEM Core.")
  251.         Exit Sub
  252.     End If
  253.  
  254.     strID(0) = ""
  255.     strName(0) = ""
  256.     strMessage = ""
  257.     strQuery = "Select * From Win32_OperatingSystem"
  258.  
  259.     Set objEnumerator = objService.ExecQuery(strQuery,,0)
  260.     If Err.Number Then
  261.         Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred during the query."
  262.         If Err.Description <> "" Then
  263.             Print "Error description: " & Err.Description & "."
  264.         End If
  265.         Err.Clear
  266.         Exit Sub
  267.     End If
  268.  
  269.     i = 0
  270.     For Each objInstance in objEnumerator
  271.         If blnForce Then
  272.             intStatus = objInstance.Win32ShutDown(CONST_FORCE_LOGOFF)
  273.         Else
  274.             intStatus = objInstance.Win32ShutDown(CONST_LOGOFF)
  275.         End If
  276.  
  277.         IF intStatus = 0 Then
  278.             strMessage = "Logging off the current user on machine " & _
  279.                          strServer & "..."
  280.         Else
  281.             strMessage = "Failed to log off the current user from machine " _
  282.                 & strServer & "."
  283.         End If
  284.         Call WriteLine(strMessage,objOutputFile)
  285.     Next
  286.  
  287.     If IsObject(objOutputFile) Then
  288.         objOutputFile.Close
  289.         Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
  290.     End If
  291. End Sub
  292.  
  293.  
  294. '********************************************************************
  295. '*
  296. '* Sub PowerOff()
  297. '*
  298. '* Purpose: Powers off a machine.
  299. '*
  300. '* Input:   strServer           a machine name
  301. '*          strOutputFile       an output file name
  302. '*          strUserName         the current user's name
  303. '*          strPassword         the current user's password
  304. '*          blnForce            specifies whether to force the logoff
  305. '*          intTimer            specifies the amount of time to perform the function
  306. '*
  307. '* Output:  Results are either printed on screen or saved in strOutputFile.
  308. '*
  309. '********************************************************************
  310. Private Sub PowerOff(strServer, strOutputFile, strUserName, strPassword, blnPowerOff, blnForce, intTimer)
  311.  
  312.  
  313.     ON ERROR RESUME NEXT
  314.  
  315.     Dim objFileSystem, objOutputFile, objService, objEnumerator, objInstance
  316.     Dim strQuery, strMessage
  317.     Dim intStatus
  318.     ReDim strID(0), strName(0)
  319.  
  320.       if blnPoweroff = false then
  321.              Exit sub
  322.       End if
  323.  
  324.     If intTimer > 0 then     
  325.         wscript.echo "Powering off machine " & strServer & " in " & intTimer & " seconds..."
  326.     wscript.sleep (intTimer * 1000)
  327.     End if
  328.  
  329.     'Open a text file for output if the file is requested
  330.     If Not IsEmpty(strOutputFile) Then
  331.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  332.             Call Wscript.Echo ("Could not open an output file.")
  333.             Exit Sub
  334.         End If
  335.     End If
  336.  
  337.     'Establish a connection with the server.
  338.     If blnConnect("root\cimv2" , _
  339.                    strUserName , _
  340.                    strPassword , _
  341.                    strServer   , _
  342.                    objService  ) Then
  343.         Call Wscript.Echo("")
  344.         Call Wscript.Echo("Please check the server name, " _
  345.                         & "credentials and WBEM Core.")
  346.         Exit Sub
  347.     End If
  348.  
  349.     strID(0) = ""
  350.     strName(0) = ""
  351.     strMessage = ""
  352.     strQuery = "Select * From Win32_OperatingSystem"
  353.  
  354.     Set objEnumerator = objService.ExecQuery(strQuery,,0)
  355.     If Err.Number Then
  356.         Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred during the query."
  357.         If Err.Description <> "" Then
  358.             Print "Error description: " & Err.Description & "."
  359.         End If
  360.         Err.Clear
  361.         Exit Sub
  362.     End If
  363.  
  364.     i = 0
  365.     For Each objInstance in objEnumerator
  366.         If blnForce Then
  367.             intStatus = objInstance.Win32ShutDown(CONST_FORCE_POWEROFF)
  368.         Else
  369.             intStatus = objInstance.Win32ShutDown(CONST_POWEROFF)
  370.         End If
  371.  
  372.         IF intStatus = 0 Then
  373.             strMessage = "Power off machine " & strServer & "."
  374.         Else
  375.             strMessage = "Failed to power off machine " & strServer & "."
  376.         End If
  377.         Call WriteLine(strMessage,objOutputFile)
  378.     Next
  379.  
  380.     If IsObject(objOutputFile) Then
  381.         objOutputFile.Close
  382.         Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
  383.     End If
  384. End Sub
  385.  
  386.  
  387. '********************************************************************
  388. '*
  389. '* Sub Shutdown()
  390. '*
  391. '* Purpose: Shutsdown a machine.
  392. '*
  393. '* Input:   strServer           a machine name
  394. '*          strOutputFile       an output file name
  395. '*          strUserName         the current user's name
  396. '*          strPassword         the current user's password
  397. '*          blnForce            specifies whether to force the logoff
  398. '*          intTimer            specifies the amount of time to perform the function
  399. '*
  400. '* Output:  Results are either printed on screen or saved in strOutputFile.
  401. '*
  402. '********************************************************************
  403. Private Sub Shutdown(strServer, strOutputFile, strUserName, strPassword, blnShutDown, blnForce, intTimer)
  404.  
  405.  
  406.     ON ERROR RESUME NEXT
  407.  
  408.     Dim objFileSystem, objOutputFile, objService, objEnumerator, objInstance
  409.     Dim strQuery, strMessage
  410.     Dim intStatus
  411.     ReDim strID(0), strName(0)
  412.  
  413.     If blnShutdown = False then
  414.           Exit Sub
  415.     End if   
  416.  
  417.     if intTimer > 0 then 
  418.               wscript.echo "Shutting down computer " & strServer & " in " & intTimer & " seconds..."
  419.          wscript.sleep (intTimer * 1000)
  420.     End if
  421.  
  422.  
  423.     'Open a text file for output if the file is requested
  424.     If Not IsEmpty(strOutputFile) Then
  425.         If (NOT blnOpenFile(strOutputFile, objOutputFile)) Then
  426.             Call Wscript.Echo ("Could not open an output file.")
  427.             Exit Sub
  428.         End If
  429.     End If
  430.  
  431.     'Establish a connection with the server.
  432.     If blnConnect("root\cimv2" , _
  433.                    strUserName , _
  434.                    strPassword , _
  435.                    strServer   , _
  436.                    objService  ) Then
  437.         Call Wscript.Echo("")
  438.         Call Wscript.Echo("Please check the server name, " _
  439.                         & "credentials and WBEM Core.")
  440.         Exit Sub
  441.     End If
  442.  
  443.     strID(0) = ""
  444.     strName(0) = ""
  445.     strMessage = ""
  446.     strQuery = "Select * From Win32_OperatingSystem"
  447.  
  448.     Set objEnumerator = objService.ExecQuery(strQuery,,0)
  449.     If Err.Number Then
  450.         Print "Error 0x" & CStr(Hex(Err.Number)) & " occurred during the query."
  451.         If Err.Description <> "" Then
  452.             Print "Error description: " & Err.Description & "."
  453.         End If
  454.         Err.Clear
  455.         Exit Sub
  456.     End If
  457.  
  458.     i = 0
  459.     For Each objInstance in objEnumerator
  460.         If blnForce Then
  461.             intStatus = objInstance.Win32ShutDown(CONST_FORCE_SHUTDOWN)
  462.         Else
  463.             intStatus = objInstance.Win32ShutDown(CONST_SHUTDOWN)
  464.         End If
  465.  
  466.         IF intStatus = 0 Then
  467.             strMessage = "Shuts down machine " & strServer & "."
  468.         Else
  469.             strMessage = "Failed to shutdown machine " & strServer & "."
  470.         End If
  471.         Call WriteLine(strMessage,objOutputFile)
  472.     Next
  473.  
  474.     If IsObject(objOutputFile) Then
  475.         objOutputFile.Close
  476.         Call Wscript.Echo ("Results are saved in file " & strOutputFile & ".")
  477.     End If
  478. End Sub
  479.  
  480.  
  481.  
  482. '********************************************************************
  483. '*
  484. '* Function intParseCmdLine()
  485. '*
  486. '* Purpose: Parses the command line.
  487. '* Input:   
  488. '*
  489. '* Output:  strServer         a remote server ("" = local server")
  490. '*          strUserName       the current user's name
  491. '*          strPassword       the current user's password
  492. '*          strOutputFile     an output file name
  493. '*          intTimer          amount of time in seconds
  494. '*
  495. '********************************************************************
  496. Private Function intParseCmdLine( ByRef strServer,        _
  497.                                   ByRef strUserName,      _
  498.                                   ByRef strPassword,      _
  499.                                   ByRef strOutputFile,    _
  500.                                   ByRef blnLogoff,        _
  501.                                   ByRef blnShutdown,      _
  502.                                   ByRef blnReboot,        _
  503.                                   ByRef blnPowerOff,      _
  504.                                   ByRef blnForce,         _
  505.                                   ByRef intTimer          )
  506.  
  507.  
  508.     ON ERROR RESUME NEXT
  509.  
  510.     Dim strFlag
  511.     Dim intState, intArgIter
  512.     Dim objFileSystem
  513.  
  514.     If Wscript.Arguments.Count > 0 Then
  515.         strFlag = Wscript.arguments.Item(0)
  516.     End If
  517.  
  518.     If IsEmpty(strFlag) Then                'No arguments have been received
  519.         Wscript.Echo("Arguments are Required.")
  520.         intParseCmdLine = CONST_ERROR
  521.         Exit Function
  522.     End If
  523.  
  524.     'Check if the user is asking for help or is just confused
  525.     If (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _
  526.         OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "?") _ 
  527.         OR (strFlag="h") Then
  528.         intParseCmdLine = CONST_SHOW_USAGE
  529.         Exit Function
  530.     End If
  531.  
  532.     'Retrieve the command line and set appropriate variables
  533.      intArgIter = 0
  534.     Do While intArgIter <= Wscript.arguments.Count - 1
  535.         Select Case Left(LCase(Wscript.arguments.Item(intArgIter)),2)
  536.   
  537.             Case "/s"
  538.                 intParseCmdLine = CONST_PROCEED
  539.                 If Not blnGetArg("Server", strServer, intArgIter) Then
  540.                     intParseCmdLine = CONST_ERROR
  541.                     Exit Function
  542.                 End If
  543.                 intArgIter = intArgIter + 1
  544.  
  545.             Case "/o"
  546.                 If Not blnGetArg("Output File", strOutputFile, intArgIter) Then
  547.                     intParseCmdLine = CONST_ERROR
  548.                     Exit Function
  549.                 End If
  550.                 intArgIter = intArgIter + 1
  551.  
  552.             Case "/u"
  553.                 If Not blnGetArg("User Name", strUserName, intArgIter) Then
  554.                     intParseCmdLine = CONST_ERROR
  555.                     Exit Function
  556.                 End If
  557.                 intArgIter = intArgIter + 1
  558.  
  559.             Case "/w"
  560.                 If Not blnGetArg("User Password", strPassword, intArgIter) Then
  561.                     intParseCmdLine = CONST_ERROR
  562.                     Exit Function
  563.                 End If
  564.                 intArgIter = intArgIter + 1
  565.             
  566.             Case "/f"
  567.                 blnForce = True
  568.                 intArgIter = intArgIter + 1
  569.  
  570.             Case "/r"
  571.                 blnReBoot = True
  572.                 userarray(0) = blnReBoot
  573.                 intArgIter = intArgIter + 1
  574.  
  575.             Case "/q"
  576.                 blnPowerOff = True
  577.                 userarray(1) = blnPowerOff
  578.                 intArgIter = intArgIter + 1
  579.  
  580.             Case "/l"
  581.                 blnLogOff = True
  582.                 userarray(2) = blnLogoff
  583.                 intArgIter = intArgIter + 1
  584.  
  585.             Case "/p"
  586.                 blnShutDown = True
  587.                 userarray(3) = blnShutDown
  588.                 intArgIter = intArgIter + 1
  589.  
  590.             Case "/t"
  591.                 If Not blnGetArg("Timer", intTimer, intArgIter) Then
  592.                     intParseCmdLine = CONST_ERROR
  593.                     Exit Function
  594.                 End If
  595.                 intArgIter = intArgIter + 1
  596.  
  597.             Case Else 'We shouldn't get here
  598.                 Call Wscript.Echo("Invalid or misplaced parameter: " _
  599.                    & Wscript.arguments.Item(intArgIter) & vbCRLF _
  600.                    & "Please check the input and try again," & vbCRLF _
  601.                    & "or invoke with '/?' for help with the syntax.")
  602.                 Wscript.Quit
  603.  
  604.         End Select
  605.  
  606.     Loop '** intArgIter <= Wscript.arguments.Count - 1
  607.  
  608.     MyCount = 0
  609.  
  610.     for i = 0 to 3
  611.         if userarray(i) = True then
  612.             MyCount = Mycount + 1
  613.         End if
  614.     Next
  615.  
  616.    if Mycount > 1 then 
  617.         intParseCmdLine = CONST_SHOW_USAGE
  618.    End if
  619.  
  620.     If IsEmpty(intParseCmdLine) Then 
  621.         intParseCmdLine = CONST_ERROR
  622.         Wscript.Echo("Arguments are Required.")
  623.     End If
  624.  
  625.  End Function
  626.  
  627. '********************************************************************
  628. '*
  629. '* Sub ShowUsage()
  630. '*
  631. '* Purpose: Shows the correct usage to the user.
  632. '*
  633. '* Input:   None
  634. '*
  635. '* Output:  Help messages are displayed on screen.
  636. '*
  637. '********************************************************************
  638. Private Sub ShowUsage()
  639.  
  640.     Wscript.Echo ""
  641.     Wscript.Echo "Logoffs, Reboots, Powers Off, or Shuts Down a machine."
  642.     Wscript.Echo ""
  643.     Wscript.Echo "SYNTAX:"
  644.     Wscript.Echo "  Restart.vbs [/S <server>] [/U <username>] [/W <password>]"
  645.     Wscript.Echo "              [/O <outputfile>] </L> </R> </P> </Q> </F> [/T <time in seconds>]"
  646.     Wscript.Echo ""
  647.     Wscript.Echo "PARAMETER SPECIFIERS:"
  648.     wscript.echo "   /T            Amount of time to perform the function."
  649.     Wscript.Echo "   /Q            Perform Shutdown."
  650.     Wscript.Echo "   /P            Perform Poweroff."
  651.     Wscript.Echo "   /R            Perform Reboot."
  652.     Wscript.Echo "   /L            Perform Logoff."
  653.     Wscript.Echo "   /F            Force Function."
  654.     Wscript.Echo "   server        A machine name."
  655.     Wscript.Echo "   username      The current user's name."
  656.     Wscript.Echo "   password      Password of the current user."
  657.     Wscript.Echo "   outputfile    The output file name."
  658.     Wscript.Echo ""
  659.     Wscript.Echo "EXAMPLE:"
  660.     Wscript.Echo "1. cscript Restart.vbs /S MyMachine2 /R"
  661.     Wscript.Echo "   Reboots the current machine MyMachine2."
  662.     Wscript.Echo "2. cscript Restart.vbs /S MyMachine2 /R /F"
  663.     Wscript.Echo "   Forces MyMachine2 to reboot."
  664.     Wscript.Echo "3. cscript Restart.vbs /S MyMachine2 /R /T 30"
  665.     Wscript.Echo "   Reboots the current machine MyMachine2 in 30 seconds."
  666.     Wscript.Echo "NOTE:"
  667.     Wscript.Echo "   The force option will make the machine perform the function even " _
  668.                & "if there are"
  669.     Wscript.Echo "   open and unsaved docuements on the screen."
  670.  
  671. End Sub
  672.  
  673. '********************************************************************
  674. '* General Routines
  675. '********************************************************************
  676.  
  677. '********************************************************************
  678. '*
  679. '* Function strPackString()
  680. '*
  681. '* Purpose: Attaches spaces to a string to increase the length to intWidth.
  682. '*
  683. '* Input:   strString   a string
  684. '*          intWidth    the intended length of the string
  685. '*          blnAfter    Should spaces be added after the string?
  686. '*          blnTruncate specifies whether to truncate the string or not if
  687. '*                      the string length is longer than intWidth
  688. '*
  689. '* Output:  strPackString is returned as the packed string.
  690. '*
  691. '********************************************************************
  692. Private Function strPackString( ByVal strString, _
  693.                                 ByVal intWidth,  _
  694.                                 ByVal blnAfter,  _
  695.                                 ByVal blnTruncate)
  696.  
  697.     ON ERROR RESUME NEXT
  698.  
  699.     intWidth      = CInt(intWidth)
  700.     blnAfter      = CBool(blnAfter)
  701.     blnTruncate   = CBool(blnTruncate)
  702.  
  703.     If Err.Number Then
  704.         Call Wscript.Echo ("Argument type is incorrect!")
  705.         Err.Clear
  706.         Wscript.Quit
  707.     End If
  708.  
  709.     If IsNull(strString) Then
  710.         strPackString = "null" & Space(intWidth-4)
  711.         Exit Function
  712.     End If
  713.  
  714.     strString = CStr(strString)
  715.     If Err.Number Then
  716.         Call Wscript.Echo ("Argument type is incorrect!")
  717.         Err.Clear
  718.         Wscript.Quit
  719.     End If
  720.  
  721.     If intWidth > Len(strString) Then
  722.         If blnAfter Then
  723.             strPackString = strString & Space(intWidth-Len(strString))
  724.         Else
  725.             strPackString = Space(intWidth-Len(strString)) & strString & " "
  726.         End If
  727.     Else
  728.         If blnTruncate Then
  729.             strPackString = Left(strString, intWidth-1) & " "
  730.         Else
  731.             strPackString = strString & " "
  732.         End If
  733.     End If
  734.  
  735. End Function
  736.  
  737. '********************************************************************
  738. '* 
  739. '*  Function blnGetArg()
  740. '*
  741. '*  Purpose: Helper to intParseCmdLine()
  742. '* 
  743. '*  Usage:
  744. '*
  745. '*     Case "/s" 
  746. '*       blnGetArg ("server name", strServer, intArgIter)
  747. '*
  748. '********************************************************************
  749. Private Function blnGetArg ( ByVal StrVarName,   _
  750.                              ByRef strVar,       _
  751.                              ByRef intArgIter) 
  752.  
  753.     blnGetArg = False 'failure, changed to True upon successful completion
  754.  
  755.     If Len(Wscript.Arguments(intArgIter)) > 2 then
  756.         If Mid(Wscript.Arguments(intArgIter),3,1) = ":" then
  757.             If Len(Wscript.Arguments(intArgIter)) > 3 then
  758.                 strVar = Right(Wscript.Arguments(intArgIter), _
  759.                          Len(Wscript.Arguments(intArgIter)) - 3)
  760.                 blnGetArg = True
  761.                 Exit Function
  762.             Else
  763.                 intArgIter = intArgIter + 1
  764.                 If intArgIter > (Wscript.Arguments.Count - 1) Then
  765.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  766.                     Call Wscript.Echo( "Please check the input and try again.")
  767.                     Exit Function
  768.                 End If
  769.  
  770.                 strVar = Wscript.Arguments.Item(intArgIter)
  771.                 If Err.Number Then
  772.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")
  773.                     Call Wscript.Echo( "Please check the input and try again.")
  774.                     Exit Function
  775.                 End If
  776.  
  777.                 If InStr(strVar, "/") Then
  778.                     Call Wscript.Echo( "Invalid " & StrVarName)
  779.                     Call Wscript.Echo( "Please check the input and try again.")
  780.                     Exit Function
  781.                 End If
  782.  
  783.                 blnGetArg = True 'success
  784.             End If
  785.         Else
  786.             strVar = Right(Wscript.Arguments(intArgIter), _
  787.                      Len(Wscript.Arguments(intArgIter)) - 2)
  788.             blnGetArg = True 'success
  789.             Exit Function
  790.         End If
  791.     Else
  792.         intArgIter = intArgIter + 1
  793.         If intArgIter > (Wscript.Arguments.Count - 1) Then
  794.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  795.             Call Wscript.Echo( "Please check the input and try again.")
  796.             Exit Function
  797.         End If
  798.  
  799.         strVar = Wscript.Arguments.Item(intArgIter)
  800.         If Err.Number Then
  801.             Call Wscript.Echo( "Invalid " & StrVarName & ".")
  802.             Call Wscript.Echo( "Please check the input and try again.")
  803.             Exit Function
  804.         End If
  805.  
  806.         If InStr(strVar, "/") Then
  807.             Call Wscript.Echo( "Invalid " & StrVarName)
  808.             Call Wscript.Echo( "Please check the input and try again.")
  809.             Exit Function
  810.         End If
  811.         blnGetArg = True 'success
  812.     End If
  813. End Function
  814.  
  815. '********************************************************************
  816. '*
  817. '* Function blnConnect()
  818. '*
  819. '* Purpose: Connects to machine strServer.
  820. '*
  821. '* Input:   strServer       a machine name
  822. '*          strNameSpace    a namespace
  823. '*          strUserName     name of the current user
  824. '*          strPassword     password of the current user
  825. '*
  826. '* Output:  objService is returned  as a service object.
  827. '*          strServer is set to local host if left unspecified
  828. '*
  829. '********************************************************************
  830. Private Function blnConnect(ByVal strNameSpace, _
  831.                             ByVal strUserName,  _
  832.                             ByVal strPassword,  _
  833.                             ByRef strServer,    _
  834.                             ByRef objService)
  835.  
  836.     ON ERROR RESUME NEXT
  837.  
  838.     Dim objLocator, objWshNet
  839.  
  840.     blnConnect = False     'There is no error.
  841.  
  842.     'Create Locator object to connect to remote CIM object manager
  843.     Set objLocator = CreateObject("WbemScripting.SWbemLocator")
  844.     If Err.Number then
  845.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  846.                            " occurred in creating a locator object." )
  847.         If Err.Description <> "" Then
  848.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  849.         End If
  850.         Err.Clear
  851.         blnConnect = True     'An error occurred
  852.         Exit Function
  853.     End If
  854.  
  855.     'Connect to the namespace which is either local or remote
  856.     Set objService = objLocator.ConnectServer (strServer, strNameSpace, _
  857.        strUserName, strPassword)
  858.     ObjService.Security_.impersonationlevel = 3
  859.     If Err.Number then
  860.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _
  861.                            " occurred in connecting to server " _
  862.            & strServer & ".")
  863.         If Err.Description <> "" Then
  864.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  865.         End If
  866.         Err.Clear
  867.         blnConnect = True     'An error occurred
  868.     End If
  869.  
  870.     'Get the current server's name if left unspecified
  871.     If IsEmpty(strServer) Then
  872.         Set objWshNet = CreateObject("Wscript.Network")
  873.     strServer     = objWshNet.ComputerName
  874.     End If
  875.  
  876. End Function
  877.  
  878. '********************************************************************
  879. '*
  880. '* Sub      VerifyHostIsCscript()
  881. '*
  882. '* Purpose: Determines which program is used to run this script.
  883. '*
  884. '* Input:   None
  885. '*
  886. '* Output:  If host is not cscript, then an error message is printed 
  887. '*          and the script is aborted.
  888. '*
  889. '********************************************************************
  890. Sub VerifyHostIsCscript()
  891.  
  892.     ON ERROR RESUME NEXT
  893.  
  894.     Dim strFullName, strCommand, i, j, intStatus
  895.  
  896.     strFullName = WScript.FullName
  897.  
  898.     If Err.Number then
  899.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & " occurred." )
  900.         If Err.Description <> "" Then
  901.             Call Wscript.Echo( "Error description: " & Err.Description & "." )
  902.         End If
  903.         intStatus =  CONST_ERROR
  904.     End If
  905.  
  906.     i = InStr(1, strFullName, ".exe", 1)
  907.     If i = 0 Then
  908.         intStatus =  CONST_ERROR
  909.     Else
  910.         j = InStrRev(strFullName, "\", i, 1)
  911.         If j = 0 Then
  912.             intStatus =  CONST_ERROR
  913.         Else
  914.             strCommand = Mid(strFullName, j+1, i-j-1)
  915.             Select Case LCase(strCommand)
  916.                 Case "cscript"
  917.                     intStatus = CONST_CSCRIPT
  918.                 Case "wscript"
  919.                     intStatus = CONST_WSCRIPT
  920.                 Case Else       'should never happen
  921.                     Call Wscript.Echo( "An unexpected program was used to " _
  922.                                        & "run this script." )
  923.                     Call Wscript.Echo( "Only CScript.Exe or WScript.Exe can " _
  924.                                        & "be used to run this script." )
  925.                     intStatus = CONST_ERROR
  926.                 End Select
  927.         End If
  928.     End If
  929.  
  930.     If intStatus <> CONST_CSCRIPT Then
  931.         Call WScript.Echo( "Please run this script using CScript." & vbCRLF & _
  932.              "This can be achieved by" & vbCRLF & _
  933.              "1. Using ""CScript Restart.vbs arguments"" for Windows 95/98 or" _
  934.              & vbCRLF & "2. Changing the default Windows Scripting Host " _
  935.              & "setting to CScript" & vbCRLF & "    using ""CScript " _
  936.              & "//H:CScript //S"" and running the script using" & vbCRLF & _
  937.              "    ""Restart.vbs arguments"" for Windows NT/2000." )
  938.         WScript.Quit
  939.     End If
  940.  
  941. End Sub
  942.  
  943. '********************************************************************
  944. '*
  945. '* Sub WriteLine()
  946. '* Purpose: Writes a text line either to a file or on screen.
  947. '* Input:   strMessage  the string to print
  948. '*          objFile     an output file object
  949. '* Output:  strMessage is either displayed on screen or written to a file.
  950. '*
  951. '********************************************************************
  952. Sub WriteLine(ByVal strMessage, ByVal objFile)
  953.  
  954.     On Error Resume Next
  955.     If IsObject(objFile) then        'objFile should be a file object
  956.         objFile.WriteLine strMessage
  957.     Else
  958.         Call Wscript.Echo( strMessage )
  959.     End If
  960.  
  961. End Sub
  962.  
  963. '********************************************************************
  964. '* 
  965. '* Function blnErrorOccurred()
  966. '*
  967. '* Purpose: Reports error with a string saying what the error occurred in.
  968. '*
  969. '* Input:   strIn        string saying what the error occurred in.
  970. '*
  971. '* Output:  displayed on screen 
  972. '* 
  973. '********************************************************************
  974. Private Function blnErrorOccurred (ByVal strIn)
  975.  
  976.     If Err.Number Then
  977.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & ": " & strIn)
  978.         If Err.Description <> "" Then
  979.             Call Wscript.Echo( "Error description: " & Err.Description)
  980.         End If
  981.         Err.Clear
  982.         blnErrorOccurred = True
  983.     Else
  984.         blnErrorOccurred = False
  985.     End If
  986.  
  987. End Function
  988.  
  989. '********************************************************************
  990. '* 
  991. '* Function blnOpenFile
  992. '*
  993. '* Purpose: Opens a file.
  994. '*
  995. '* Input:   strFileName        A string with the name of the file.
  996. '*
  997. '* Output:  Sets objOpenFile to a FileSystemObject and setis it to 
  998. '*            Nothing upon Failure.
  999. '* 
  1000. '********************************************************************
  1001. Private Function blnOpenFile(ByVal strFileName, ByRef objOpenFile)
  1002.  
  1003.     ON ERROR RESUME NEXT
  1004.  
  1005.     Dim objFileSystem
  1006.  
  1007.     Set objFileSystem = Nothing
  1008.  
  1009.     If IsEmpty(strFileName) OR strFileName = "" Then
  1010.         blnOpenFile = False
  1011.         Set objOpenFile = Nothing
  1012.         Exit Function
  1013.     End If
  1014.  
  1015.     'Create a file object
  1016.     Set objFileSystem = CreateObject("Scripting.FileSystemObject")
  1017.     If blnErrorOccurred("Could not create filesystem object.") Then
  1018.         blnOpenFile = False
  1019.         Set objOpenFile = Nothing
  1020.         Exit Function
  1021.     End If
  1022.  
  1023.     'Open the file for output
  1024.     Set objOpenFile = objFileSystem.OpenTextFile(strFileName, 8, True)
  1025.     If blnErrorOccurred("Could not open") Then
  1026.         blnOpenFile = False
  1027.         Set objOpenFile = Nothing
  1028.         Exit Function
  1029.     End If
  1030.     blnOpenFile = True
  1031.  
  1032. End Function
  1033.  
  1034. '********************************************************************
  1035. '*                                                                  *
  1036. '*                           End of File                            *
  1037. '*                                                                  *
  1038. '********************************************************************
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.