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. '* Function intParseCmdLine()  
  484. '*  
  485. '* Purpose: Parses the command line.  
  486. '* Input:     
  487. '*  
  488. '* Output:  strServer         a remote server ("" = local server")  
  489. '*          strUserName       the current user's name  
  490. '*          strPassword       the current user's password  
  491. '*          strOutputFile     an output file name  
  492. '*          intTimer          amount of time in seconds  
  493. '*  
  494. '********************************************************************  
  495. Private Function intParseCmdLine( ByRef strServer,        _  
  496.                                   ByRef strUserName,      _  
  497.                                   ByRef strPassword,      _  
  498.                                   ByRef strOutputFile,    _  
  499.                                   ByRef blnLogoff,        _  
  500.                                   ByRef blnShutdown,      _  
  501.                                   ByRef blnReboot,        _  
  502.                                   ByRef blnPowerOff,      _  
  503.                                   ByRef blnForce,         _  
  504.                                   ByRef intTimer          )  
  505.  
  506.  
  507.     ON ERROR RESUME NEXT  
  508.  
  509.     Dim strFlag  
  510.     Dim intState, intArgIter  
  511.     Dim objFileSystem  
  512.  
  513.     If Wscript.Arguments.Count > 0 Then 
  514.         strFlag = Wscript.arguments.Item(0)  
  515.     End If 
  516.  
  517.     If IsEmpty(strFlag) Then                'No arguments have been received  
  518.         Wscript.Echo("Arguments are Required.")  
  519.         intParseCmdLine = CONST_ERROR  
  520.         Exit Function 
  521.     End If 
  522.  
  523.     'Check if the user is asking for help or is just confused  
  524.     If (strFlag="help") OR (strFlag="/h") OR (strFlag="\h") OR (strFlag="-h") _  
  525.         OR (strFlag = "\?") OR (strFlag = "/?") OR (strFlag = "?") _   
  526.         OR (strFlag="h"Then 
  527.         intParseCmdLine = CONST_SHOW_USAGE  
  528.         Exit Function 
  529.     End If 
  530.  
  531.     'Retrieve the command line and set appropriate variables  
  532.      intArgIter = 0  
  533.     Do While intArgIter <= Wscript.arguments.Count - 1  
  534.         Select Case Left(LCase(Wscript.arguments.Item(intArgIter)),2)  
  535.     
  536.             Case "/s" 
  537.                 intParseCmdLine = CONST_PROCEED  
  538.                 If Not blnGetArg("Server", strServer, intArgIter) Then 
  539.                     intParseCmdLine = CONST_ERROR  
  540.                     Exit Function 
  541.                 End If 
  542.                 intArgIter = intArgIter + 1  
  543.  
  544.             Case "/o" 
  545.                 If Not blnGetArg("Output File", strOutputFile, intArgIter) Then 
  546.                     intParseCmdLine = CONST_ERROR  
  547.                     Exit Function 
  548.                 End If 
  549.                 intArgIter = intArgIter + 1  
  550.  
  551.             Case "/u" 
  552.                 If Not blnGetArg("User Name", strUserName, intArgIter) Then 
  553.                     intParseCmdLine = CONST_ERROR  
  554.                     Exit Function 
  555.                 End If 
  556.                 intArgIter = intArgIter + 1  
  557.  
  558.             Case "/w" 
  559.                 If Not blnGetArg("User Password", strPassword, intArgIter) Then 
  560.                     intParseCmdLine = CONST_ERROR  
  561.                     Exit Function 
  562.                 End If 
  563.                 intArgIter = intArgIter + 1  
  564.               
  565.             Case "/f" 
  566.                 blnForce = True 
  567.                 intArgIter = intArgIter + 1  
  568.  
  569.             Case "/r" 
  570.                 blnReBoot = True 
  571.                 userarray(0) = blnReBoot  
  572.                 intArgIter = intArgIter + 1  
  573.  
  574.             Case "/q" 
  575.                 blnPowerOff = True 
  576.                 userarray(1) = blnPowerOff  
  577.                 intArgIter = intArgIter + 1  
  578.  
  579.             Case "/l" 
  580.                 blnLogOff = True 
  581.                 userarray(2) = blnLogoff  
  582.                 intArgIter = intArgIter + 1  
  583.  
  584.             Case "/p" 
  585.                 blnShutDown = True 
  586.                 userarray(3) = blnShutDown  
  587.                 intArgIter = intArgIter + 1  
  588.  
  589.             Case "/t" 
  590.                 If Not blnGetArg("Timer", intTimer, intArgIter) Then 
  591.                     intParseCmdLine = CONST_ERROR  
  592.                     Exit Function 
  593.                 End If 
  594.                 intArgIter = intArgIter + 1  
  595.  
  596.             Case Else 'We shouldn't get here  
  597.                 Call Wscript.Echo("Invalid or misplaced parameter: " _  
  598.                    & Wscript.arguments.Item(intArgIter) & vbCRLF _  
  599.                    & "Please check the input and try again," & vbCRLF _  
  600.                    & "or invoke with '/?' for help with the syntax.")  
  601.                 Wscript.Quit  
  602.  
  603.         End Select 
  604.  
  605.     Loop '** intArgIter <= Wscript.arguments.Count - 1  
  606.  
  607.     MyCount = 0  
  608.  
  609.     for i = 0 to 3  
  610.         if userarray(i) = True then  
  611.             MyCount = Mycount + 1  
  612.         End if  
  613.     Next 
  614.  
  615.    if Mycount > 1 then   
  616.         intParseCmdLine = CONST_SHOW_USAGE  
  617.    End if  
  618.  
  619.     If IsEmpty(intParseCmdLine) Then   
  620.         intParseCmdLine = CONST_ERROR  
  621.         Wscript.Echo("Arguments are Required.")  
  622.     End If 
  623.  
  624.  End Function 
  625.  
  626. '********************************************************************  
  627. '*  
  628. '* Sub ShowUsage()  
  629. '*  
  630. '* Purpose: Shows the correct usage to the user.  
  631. '*  
  632. '* Input:   None  
  633. '*  
  634. '* Output:  Help messages are displayed on screen.  
  635. '*  
  636. '********************************************************************  
  637. Private Sub ShowUsage()  
  638.  
  639.     Wscript.Echo "" 
  640.     Wscript.Echo "Logoffs, Reboots, Powers Off, or Shuts Down a machine." 
  641.     Wscript.Echo "" 
  642.     Wscript.Echo "SYNTAX:" 
  643.     Wscript.Echo "  Restart.vbs [/S <server>] [/U <username>] [/W <password>]" 
  644.     Wscript.Echo "              [/O <outputfile>] </L> </R> </P> </Q> </F> [/T <time in seconds>]" 
  645.     Wscript.Echo "" 
  646.     Wscript.Echo "PARAMETER SPECIFIERS:" 
  647.     wscript.echo "   /T            Amount of time to perform the function." 
  648.     Wscript.Echo "   /Q            Perform Shutdown." 
  649.     Wscript.Echo "   /P            Perform Poweroff." 
  650.     Wscript.Echo "   /R            Perform Reboot." 
  651.     Wscript.Echo "   /L            Perform Logoff." 
  652.     Wscript.Echo "   /F            Force Function." 
  653.     Wscript.Echo "   server        A machine name." 
  654.     Wscript.Echo "   username      The current user's name." 
  655.     Wscript.Echo "   password      Password of the current user." 
  656.     Wscript.Echo "   outputfile    The output file name." 
  657.     Wscript.Echo "" 
  658.     Wscript.Echo "EXAMPLE:" 
  659.     Wscript.Echo "1. cscript Restart.vbs /S MyMachine2 /R" 
  660.     Wscript.Echo "   Reboots the current machine MyMachine2." 
  661.     Wscript.Echo "2. cscript Restart.vbs /S MyMachine2 /R /F" 
  662.     Wscript.Echo "   Forces MyMachine2 to reboot." 
  663.     Wscript.Echo "3. cscript Restart.vbs /S MyMachine2 /R /T 30" 
  664.     Wscript.Echo "   Reboots the current machine MyMachine2 in 30 seconds." 
  665.     Wscript.Echo "NOTE:" 
  666.     Wscript.Echo "   The force option will make the machine perform the function even " _  
  667.                & "if there are" 
  668.     Wscript.Echo "   open and unsaved docuements on the screen." 
  669.  
  670. End Sub 
  671.  
  672. '********************************************************************  
  673. '* General Routines  
  674. '********************************************************************  
  675.  
  676. '********************************************************************  
  677. '*  
  678. '* Function strPackString()  
  679. '*  
  680. '* Purpose: Attaches spaces to a string to increase the length to intWidth.  
  681. '*  
  682. '* Input:   strString   a string  
  683. '*          intWidth    the intended length of the string  
  684. '*          blnAfter    Should spaces be added after the string?  
  685. '*          blnTruncate specifies whether to truncate the string or not if  
  686. '*                      the string length is longer than intWidth  
  687. '*  
  688. '* Output:  strPackString is returned as the packed string.  
  689. '*  
  690. '********************************************************************  
  691. Private Function strPackString( ByVal strString, _  
  692.                                 ByVal intWidth,  _  
  693.                                 ByVal blnAfter,  _  
  694.                                 ByVal blnTruncate)  
  695.  
  696.     ON ERROR RESUME NEXT  
  697.  
  698.     intWidth      = CInt(intWidth)  
  699.     blnAfter      = CBool(blnAfter)  
  700.     blnTruncate   = CBool(blnTruncate)  
  701.  
  702.     If Err.Number Then 
  703.         Call Wscript.Echo ("Argument type is incorrect!")  
  704.         Err.Clear  
  705.         Wscript.Quit  
  706.     End If 
  707.  
  708.     If IsNull(strString) Then 
  709.         strPackString = "null" & Space(intWidth-4)  
  710.         Exit Function 
  711.     End If 
  712.  
  713.     strString = CStr(strString)  
  714.     If Err.Number Then 
  715.         Call Wscript.Echo ("Argument type is incorrect!")  
  716.         Err.Clear  
  717.         Wscript.Quit  
  718.     End If 
  719.  
  720.     If intWidth > Len(strString) Then 
  721.         If blnAfter Then 
  722.             strPackString = strString & Space(intWidth-Len(strString))  
  723.         Else 
  724.             strPackString = Space(intWidth-Len(strString)) & strString & " " 
  725.         End If 
  726.     Else 
  727.         If blnTruncate Then 
  728.             strPackString = Left(strString, intWidth-1) & " " 
  729.         Else 
  730.             strPackString = strString & " " 
  731.         End If 
  732.     End If 
  733.  
  734. End Function 
  735.  
  736. '********************************************************************  
  737. '*   
  738. '*  Function blnGetArg()  
  739. '*  
  740. '*  Purpose: Helper to intParseCmdLine()  
  741. '*   
  742. '*  Usage:  
  743. '*  
  744. '*     Case "/s"   
  745. '*       blnGetArg ("server name", strServer, intArgIter)  
  746. '*  
  747. '********************************************************************  
  748. Private Function blnGetArg ( ByVal StrVarName,   _  
  749.                              ByRef strVar,       _  
  750.                              ByRef intArgIter)   
  751.  
  752.     blnGetArg = False 'failure, changed to True upon successful completion  
  753.  
  754.     If Len(Wscript.Arguments(intArgIter)) > 2 then  
  755.         If Mid(Wscript.Arguments(intArgIter),3,1) = ":" then  
  756.             If Len(Wscript.Arguments(intArgIter)) > 3 then  
  757.                 strVar = Right(Wscript.Arguments(intArgIter), _  
  758.                          Len(Wscript.Arguments(intArgIter)) - 3)  
  759.                 blnGetArg = True 
  760.                 Exit Function 
  761.             Else 
  762.                 intArgIter = intArgIter + 1  
  763.                 If intArgIter > (Wscript.Arguments.Count - 1) Then 
  764.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")  
  765.                     Call Wscript.Echo( "Please check the input and try again.")  
  766.                     Exit Function 
  767.                 End If 
  768.  
  769.                 strVar = Wscript.Arguments.Item(intArgIter)  
  770.                 If Err.Number Then 
  771.                     Call Wscript.Echo( "Invalid " & StrVarName & ".")  
  772.                     Call Wscript.Echo( "Please check the input and try again.")  
  773.                     Exit Function 
  774.                 End If 
  775.  
  776.                 If InStr(strVar, "/"Then 
  777.                     Call Wscript.Echo( "Invalid " & StrVarName)  
  778.                     Call Wscript.Echo( "Please check the input and try again.")  
  779.                     Exit Function 
  780.                 End If 
  781.  
  782.                 blnGetArg = True 'success  
  783.             End If 
  784.         Else 
  785.             strVar = Right(Wscript.Arguments(intArgIter), _  
  786.                      Len(Wscript.Arguments(intArgIter)) - 2)  
  787.             blnGetArg = True 'success  
  788.             Exit Function 
  789.         End If 
  790.     Else 
  791.         intArgIter = intArgIter + 1  
  792.         If intArgIter > (Wscript.Arguments.Count - 1) Then 
  793.             Call Wscript.Echo( "Invalid " & StrVarName & ".")  
  794.             Call Wscript.Echo( "Please check the input and try again.")  
  795.             Exit Function 
  796.         End If 
  797.  
  798.         strVar = Wscript.Arguments.Item(intArgIter)  
  799.         If Err.Number Then 
  800.             Call Wscript.Echo( "Invalid " & StrVarName & ".")  
  801.             Call Wscript.Echo( "Please check the input and try again.")  
  802.             Exit Function 
  803.         End If 
  804.  
  805.         If InStr(strVar, "/"Then 
  806.             Call Wscript.Echo( "Invalid " & StrVarName)  
  807.             Call Wscript.Echo( "Please check the input and try again.")  
  808.             Exit Function 
  809.         End If 
  810.         blnGetArg = True 'success  
  811.     End If 
  812. End Function 
  813.  
  814. '********************************************************************  
  815. '*  
  816. '* Function blnConnect()  
  817. '*  
  818. '* Purpose: Connects to machine strServer.  
  819. '*  
  820. '* Input:   strServer       a machine name  
  821. '*          strNameSpace    a namespace  
  822. '*          strUserName     name of the current user  
  823. '*          strPassword     password of the current user  
  824. '*  
  825. '* Output:  objService is returned  as a service object.  
  826. '*          strServer is set to local host if left unspecified  
  827. '*  
  828. '********************************************************************  
  829. Private Function blnConnect(ByVal strNameSpace, _  
  830.                             ByVal strUserName,  _  
  831.                             ByVal strPassword,  _  
  832.                             ByRef strServer,    _  
  833.                             ByRef objService)  
  834.  
  835.     ON ERROR RESUME NEXT  
  836.  
  837.     Dim objLocator, objWshNet  
  838.  
  839.     blnConnect = False     'There is no error.  
  840.  
  841.     'Create Locator object to connect to remote CIM object manager  
  842.     Set objLocator = CreateObject("WbemScripting.SWbemLocator")  
  843.     If Err.Number then  
  844.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _  
  845.                            " occurred in creating a locator object." )  
  846.         If Err.Description <> "" Then 
  847.             Call Wscript.Echo( "Error description: " & Err.Description & "." )  
  848.         End If 
  849.         Err.Clear  
  850.         blnConnect = True     'An error occurred  
  851.         Exit Function 
  852.     End If 
  853.  
  854.     'following two lines added by Ravi  
  855.     objLocator.Security_.Privileges.Add 18, True    'wbemPrivilegeShutdown  
  856.     objLocator.Security_.Privileges.Add 23, True    'wbemPrivilegeRemoteShutdown  
  857.  
  858.     'Connect to the namespace which is either local or remote  
  859.     Set objService = objLocator.ConnectServer (strServer, strNameSpace, _  
  860.        strUserName, strPassword)  
  861.     ObjService.Security_.impersonationlevel = 3  
  862.     If Err.Number then  
  863.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & _  
  864.                            " occurred in connecting to server " _  
  865.            & strServer & ".")  
  866.         If Err.Description <> "" Then 
  867.             Call Wscript.Echo( "Error description: " & Err.Description & "." )  
  868.         End If 
  869.         Err.Clear  
  870.         blnConnect = True     'An error occurred  
  871.     End If 
  872.  
  873.     'Get the current server's name if left unspecified  
  874.     If IsEmpty(strServer) Then 
  875.         Set objWshNet = CreateObject("Wscript.Network")  
  876.     strServer     = objWshNet.ComputerName  
  877.     End If 
  878.  
  879. End Function 
  880.  
  881. '********************************************************************  
  882. '*  
  883. '* Sub      VerifyHostIsCscript()  
  884. '*  
  885. '* Purpose: Determines which program is used to run this script.  
  886. '*  
  887. '* Input:   None  
  888. '*  
  889. '* Output:  If host is not cscript, then an error message is printed   
  890. '*          and the script is aborted.  
  891. '*  
  892. '********************************************************************  
  893. Sub VerifyHostIsCscript()  
  894.  
  895.     ON ERROR RESUME NEXT  
  896.  
  897.     Dim strFullName, strCommand, i, j, intStatus  
  898.  
  899.     strFullName = WScript.FullName  
  900.  
  901.     If Err.Number then  
  902.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & " occurred." )  
  903.         If Err.Description <> "" Then 
  904.             Call Wscript.Echo( "Error description: " & Err.Description & "." )  
  905.         End If 
  906.         intStatus =  CONST_ERROR  
  907.     End If 
  908.  
  909.     i = InStr(1, strFullName, ".exe", 1)  
  910.     If i = 0 Then 
  911.         intStatus =  CONST_ERROR  
  912.     Else 
  913.         j = InStrRev(strFullName, "\", i, 1)  
  914.         If j = 0 Then 
  915.             intStatus =  CONST_ERROR  
  916.         Else 
  917.             strCommand = Mid(strFullName, j+1, i-j-1)  
  918.             Select Case LCase(strCommand)  
  919.                 Case "cscript" 
  920.                     intStatus = CONST_CSCRIPT  
  921.                 Case "wscript" 
  922.                     intStatus = CONST_WSCRIPT  
  923.                 Case Else       'should never happen  
  924.                     Call Wscript.Echo( "An unexpected program was used to " _  
  925.                                        & "run this script." )  
  926.                     Call Wscript.Echo( "Only CScript.Exe or WScript.Exe can " _  
  927.                                        & "be used to run this script." )  
  928.                     intStatus = CONST_ERROR  
  929.                 End Select 
  930.         End If 
  931.     End If 
  932.  
  933.     If intStatus <> CONST_CSCRIPT Then 
  934.         Call WScript.Echo( "Please run this script using CScript." & vbCRLF & _  
  935.              "This can be achieved by" & vbCRLF & _  
  936.              "1. Using ""CScript Restart.vbs arguments"" for Windows 95/98 or" _  
  937.              & vbCRLF & "2. Changing the default Windows Scripting Host " _  
  938.              & "setting to CScript" & vbCRLF & "    using ""CScript " _  
  939.              & "//F:CScript //S"" and running the script using" & vbCRLF & _  
  940.              "    ""Restart.vbs arguments"" for Windows NT/2000." )  
  941.         WScript.Quit  
  942.     End If 
  943.  
  944. End Sub 
  945.  
  946. '********************************************************************  
  947. '*  
  948. '* Sub WriteLine()  
  949. '* Purpose: Writes a text line either to a file or on screen.  
  950. '* Input:   strMessage  the string to print  
  951. '*          objFile     an output file object  
  952. '* Output:  strMessage is either displayed on screen or written to a file.  
  953. '*  
  954. '********************************************************************  
  955. Sub WriteLine(ByVal strMessage, ByVal objFile)  
  956.  
  957.     On Error Resume Next 
  958.     If IsObject(objFile) then        'objFile should be a file object  
  959.         objFile.WriteLine strMessage  
  960.     Else 
  961.         Call Wscript.Echo( strMessage )  
  962.     End If 
  963.  
  964. End Sub 
  965.  
  966. '********************************************************************  
  967. '*   
  968. '* Function blnErrorOccurred()  
  969. '*  
  970. '* Purpose: Reports error with a string saying what the error occurred in.  
  971. '*  
  972. '* Input:   strIn  string saying what the error occurred in.  
  973. '*  
  974. '* Output:  displayed on screen   
  975. '*   
  976. '********************************************************************  
  977. Private Function blnErrorOccurred (ByVal strIn)  
  978.  
  979.     If Err.Number Then 
  980.         Call Wscript.Echo( "Error 0x" & CStr(Hex(Err.Number)) & ": " & strIn)  
  981.         If Err.Description <> "" Then 
  982.             Call Wscript.Echo( "Error description: " & Err.Description)  
  983.         End If 
  984.         Err.Clear  
  985.         blnErrorOccurred = True 
  986.     Else 
  987.         blnErrorOccurred = False 
  988.     End If 
  989.  
  990. End Function 
  991.  
  992. '********************************************************************  
  993. '*   
  994. '* Function blnOpenFile  
  995. '*  
  996. '* Purpose: Opens a file.  
  997. '*  
  998. '* Input:   strFileName  A string with the name of the file.  
  999. '*  
  1000. '* Output:  Sets objOpenFile to a FileSystemObject and setis it to   
  1001. '*            Nothing upon Failure.  
  1002. '*   
  1003. '********************************************************************  
  1004. Private Function blnOpenFile(ByVal strFileName, ByRef objOpenFile)  
  1005.  
  1006.     ON ERROR RESUME NEXT  
  1007.  
  1008.     Dim objFileSystem  
  1009.  
  1010.     Set objFileSystem = Nothing 
  1011.  
  1012.     If IsEmpty(strFileName) OR strFileName = "" Then 
  1013.         blnOpenFile = False 
  1014.         Set objOpenFile = Nothing 
  1015.         Exit Function 
  1016.     End If 
  1017.  
  1018.     'Create a file object  
  1019.     Set objFileSystem = CreateObject("Scripting.FileSystemObject")  
  1020.     If blnErrorOccurred("Could not create filesystem object."Then 
  1021.         blnOpenFile = False 
  1022.         Set objOpenFile = Nothing 
  1023.         Exit Function 
  1024.     End If 
  1025.  
  1026.     'Open the file for output  
  1027.     Set objOpenFile = objFileSystem.OpenTextFile(strFileName, 8, True)  
  1028.     If blnErrorOccurred("Could not open"Then 
  1029.         blnOpenFile = False 
  1030.         Set objOpenFile = Nothing 
  1031.         Exit Function 
  1032.     End If 
  1033.     blnOpenFile = True 
  1034.  
  1035. End Function 
  1036.  
  1037. '********************************************************************  
  1038. '*                                                                  *  
  1039. '*                           End of File                            *  
  1040. '*                                                                  *  
  1041. '******************************************************************** 
此文章由 flyinweb 于 2010-11-08 18:18:43 编辑

本日志由 flyinweb 于 2009-06-18 08:31:46 发表,目前已经被浏览 3893 次,评论 0 次;

作者添加了以下标签: windowsLogOffPowerOff脚本

引用通告:http://www.517sou.net/Article/15/Trackback.ashx

评论订阅:http://www.517sou.net/Article/15/Feeds.ashx

相关文章

评论列表

    暂时没有评论
(必填)
(必填,不会被公开)