Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write Restart.vbs source code to restart the vbs of a remote computer

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to write Restart.vbs source code to restart the vbs of a remote computer", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to write Restart.vbs source code to restart the vbs of a remote computer.

The copy code is as follows:

'*

'*

'* File: Restart.vbs

'* Created: March 1999

'* Version: 1.0

'*

'* Main Function: Shutsdown, PowerOff, LogOff, Restarts a machine.

'*

'* Restart.vbs / S [/ U] [/ W]

'* [/ O] [/ L} [/ P] [/ R] [/ Q] [/ F] [/ T]

'*

'* Copyright (C) 1999 Microsoft Corporation

'*

'*

OPTION EXPLICIT

'Define constants

CONST CONST_ERROR = 0

CONST CONST_WSCRIPT = 1

CONST CONST_CSCRIPT = 2

CONST CONST_SHOW_USAGE = 3

CONST CONST_PROCEED = 4

'Shutdown Method Constants

CONST CONST_SHUTDOWN = 1

CONST CONST_LOGOFF = 0

CONST CONST_POWEROFF = 8

CONST CONST_REBOOT = 2

CONST CONST_FORCE_REBOOT = 6

CONST CONST_FORCE_POWEROFF = 12

CONST CONST_FORCE_LOGOFF = 4

CONST CONST_FORCE_SHUTDOWN = 5

'Declare variables

Dim intOpMode, i

Dim strServer, strUserName, strPassword, strOutputFile

Dim blnLogoff, blnPowerOff, blnReBoot, blnShutDown

Dim blnForce

Dim intTimer

Dim UserArray (3)

Dim MyCount

'Make sure the host is csript, if not then abort

VerifyHostIsCscript ()

'Parse the command line

IntOpMode = intParseCmdLine (strServer, _

StrUserName, _

StrPassword, _

StrOutputFile, _

BlnLogoff, _

BlnPowerOff, _

BlnReBoot, _

BlnShutdown, _

BlnForce, _

IntTimer)

Select Case intOpMode

Case CONST_SHOW_USAGE

Call ShowUsage ()

Case CONST_PROCEED

Call Reboot (strServer, _

StrOutputFile, _

StrUserName, _

StrPassword, _

BlnReboot, _

BlnForce, _

IntTimer)

Call LogOff (strServer, _

StrOutputFile, _

StrUserName, _

StrPassword, _

BlnLogoff, _

BlnForce, _

IntTimer)

Call PowerOff (strServer, _

StrOutputFile, _

StrUserName, _

StrPassword, _

BlnPowerOff, _

BlnForce, _

IntTimer)

Call ShutDown (strServer, _

StrOutputFile, _

StrUserName, _

StrPassword, _

BlnShutDown, _

BlnForce, _

IntTimer)

Case CONST_ERROR

'Do Nothing

Case Else 'Default-- should never happen

Call Wscript.Echo ("Error occurred in passing parameters.")

End Select

'*

'*

'* Sub Reboot ()

'*

'* Purpose: Reboots a machine.

'*

'* Input: strServer a machine name

'* strOutputFile an output file name

'* strUserName the current user's name

'* strPassword the current user's password

'* blnForce specifies whether to force the logoff

'* intTimer specifies the amount of time to perform the function

'*

'* Output: Results are either printed on screen or saved in strOutputFile.

'*

'*

Private Sub Reboot (strServer, strOutputFile, strUserName, strPassword, blnReboot, blnForce, intTimer)

ON ERROR RESUME NEXT

Dim objFileSystem, objOutputFile, objService, objEnumerator, objInstance

Dim strQuery, strMessage

Dim intStatus

ReDim strID (0), strName (0)

If blnreboot = false then

Exit Sub

End if

If intTimer > 0 then

Wscript.echo "Rebooting machine" & strServer & "in" & intTimer & "seconds..."

Wscript.sleep (intTimer * 1000)

End if

'Open a text file for output if the file is requested

If Not IsEmpty (strOutputFile) Then

If (NOT blnOpenFile (strOutputFile, objOutputFile)) Then

Call Wscript.Echo ("Could not open an output file.")

Exit Sub

End If

End If

'Establish a connection with the server.

If blnConnect ("root\ cimv2", _

StrUserName, _

StrPassword, _

StrServer, _

ObjService) Then

Call Wscript.Echo ("")

Call Wscript.Echo ("Please check the server name," _

& "credentials and WBEM Core."

Exit Sub

End If

StrID (0) = ""

StrName (0) = ""

StrMessage = ""

StrQuery = "Select * From Win32_OperatingSystem"

Set objEnumerator = objService.ExecQuery (strQuery,0)

If Err.Number Then

Print "Error 0x" & CStr (Hex (Err.Number)) & "occurred during the query."

If Err.Description "" Then

Print "Error description:" & Err.Description & ".

End If

Err.Clear

Exit Sub

End If

I = 0

For Each objInstance in objEnumerator

If blnForce Then

IntStatus = objInstance.Win32ShutDown (CONST_FORCE_REBOOT)

Else

IntStatus = objInstance.Win32ShutDown (CONST_REBOOT)

End If

IF intStatus = 0 Then

StrMessage = "Reboot a machine" & strServer & "."

Else

StrMessage = "Failed to reboot a machine" & strServer & "."

End If

Call WriteLine (strMessage,objOutputFile)

Next

If IsObject (objOutputFile) Then

ObjOutputFile.Close

Call Wscript.Echo ("Results are saved in file" & strOutputFile & ".")

End If

End Sub

'*

'*

'* Sub LogOff ()

'*

'* Purpose: Logs off the user currently logged onto a machine.

'*

'* Input: strServer a machine name

'* strOutputFile an output file name

'* strUserName the current user's name

'* strPassword the current user's password

'* blnForce specifies whether to force the logoff

'* intTimer specifies the amount of time to preform the function

'*

'* Output: Results are either printed on screen or saved in strOutputFile.

'*

'*

Private Sub LogOff (strServer, strOutputFile, strUserName, strPassword, blnLogoff, blnForce, intTimer)

ON ERROR RESUME NEXT

Dim objFileSystem, objOutputFile, objService, objEnumerator, objInstance

Dim strQuery, strMessage

Dim intStatus

ReDim strID (0), strName (0)

If blnlogoff = false then

Exit Sub

End if

If intTimer > 1 then

Wscript.echo "Logging off machine" & strServer & "in" & intTimer & "seconds..."

Wscript.sleep (intTimer * 1000)

End if

'Open a text file for output if the file is requested

If Not IsEmpty (strOutputFile) Then

If (NOT blnOpenFile (strOutputFile, objOutputFile)) Then

Call Wscript.Echo ("Could not open an output file.")

Exit Sub

End If

End If

'Establish a connection with the server.

If blnConnect ("root\ cimv2", _

StrUserName, _

StrPassword, _

StrServer, _

ObjService) Then

Call Wscript.Echo ("")

Call Wscript.Echo ("Please check the server name," _

& "credentials and WBEM Core."

Exit Sub

End If

StrID (0) = ""

StrName (0) = ""

StrMessage = ""

StrQuery = "Select * From Win32_OperatingSystem"

Set objEnumerator = objService.ExecQuery (strQuery,0)

If Err.Number Then

Print "Error 0x" & CStr (Hex (Err.Number)) & "occurred during the query."

If Err.Description "" Then

Print "Error description:" & Err.Description & ".

End If

Err.Clear

Exit Sub

End If

I = 0

For Each objInstance in objEnumerator

If blnForce Then

IntStatus = objInstance.Win32ShutDown (CONST_FORCE_LOGOFF)

Else

IntStatus = objInstance.Win32ShutDown (CONST_LOGOFF)

End If

IF intStatus = 0 Then

StrMessage = "Logging off the current user on machine" & _

StrServer & "..."

Else

StrMessage = "Failed to log off the current user from machine" _

& strServer & "."

End If

Call WriteLine (strMessage,objOutputFile)

Next

If IsObject (objOutputFile) Then

ObjOutputFile.Close

Call Wscript.Echo ("Results are saved in file" & strOutputFile & ".")

End If

End Sub

'*

'*

'* Sub PowerOff ()

'*

'* Purpose: Powers off a machine.

'*

'* Input: strServer a machine name

'* strOutputFile an output file name

'* strUserName the current user's name

'* strPassword the current user's password

'* blnForce specifies whether to force the logoff

'* intTimer specifies the amount of time to perform the function

'*

'* Output: Results are either printed on screen or saved in strOutputFile.

'*

'*

Private Sub PowerOff (strServer, strOutputFile, strUserName, strPassword, blnPowerOff, blnForce, intTimer)

ON ERROR RESUME NEXT

Dim objFileSystem, objOutputFile, objService, objEnumerator, objInstance

Dim strQuery, strMessage

Dim intStatus

ReDim strID (0), strName (0)

If blnPoweroff = false then

Exit sub

End if

If intTimer > 0 then

Wscript.echo "Powering off machine" & strServer & "in" & intTimer & "seconds..."

Wscript.sleep (intTimer * 1000)

End if

'Open a text file for output if the file is requested

If Not IsEmpty (strOutputFile) Then

If (NOT blnOpenFile (strOutputFile, objOutputFile)) Then

Call Wscript.Echo ("Could not open an output file.")

Exit Sub

End If

End If

'Establish a connection with the server.

If blnConnect ("root\ cimv2", _

StrUserName, _

StrPassword, _

StrServer, _

ObjService) Then

Call Wscript.Echo ("")

Call Wscript.Echo ("Please check the server name," _

& "credentials and WBEM Core."

Exit Sub

End If

StrID (0) = ""

StrName (0) = ""

StrMessage = ""

StrQuery = "Select * From Win32_OperatingSystem"

Set objEnumerator = objService.ExecQuery (strQuery,0)

If Err.Number Then

Print "Error 0x" & CStr (Hex (Err.Number)) & "occurred during the query."

If Err.Description "" Then

Print "Error description:" & Err.Description & ".

End If

Err.Clear

Exit Sub

End If

I = 0

For Each objInstance in objEnumerator

If blnForce Then

IntStatus = objInstance.Win32ShutDown (CONST_FORCE_POWEROFF)

Else

IntStatus = objInstance.Win32ShutDown (CONST_POWEROFF)

End If

IF intStatus = 0 Then

StrMessage = "Power off machine" & strServer & "."

Else

StrMessage = "Failed to power off machine" & strServer & "."

End If

Call WriteLine (strMessage,objOutputFile)

Next

If IsObject (objOutputFile) Then

ObjOutputFile.Close

Call Wscript.Echo ("Results are saved in file" & strOutputFile & ".")

End If

End Sub

'*

'*

'* Sub Shutdown ()

'*

'* Purpose: Shutsdown a machine.

'*

'* Input: strServer a machine name

'* strOutputFile an output file name

'* strUserName the current user's name

'* strPassword the current user's password

'* blnForce specifies whether to force the logoff

'* intTimer specifies the amount of time to perform the function

'*

'* Output: Results are either printed on screen or saved in strOutputFile.

'*

'*

Private Sub Shutdown (strServer, strOutputFile, strUserName, strPassword, blnShutDown, blnForce, intTimer)

ON ERROR RESUME NEXT

Dim objFileSystem, objOutputFile, objService, objEnumerator, objInstance

Dim strQuery, strMessage

Dim intStatus

ReDim strID (0), strName (0)

If blnShutdown = False then

Exit Sub

End if

If intTimer > 0 then

Wscript.echo "Shutting down computer" & strServer & "in" & intTimer & "seconds..."

Wscript.sleep (intTimer * 1000)

End if

'Open a text file for output if the file is requested

If Not IsEmpty (strOutputFile) Then

If (NOT blnOpenFile (strOutputFile, objOutputFile)) Then

Call Wscript.Echo ("Could not open an output file.")

Exit Sub

End If

End If

'Establish a connection with the server.

If blnConnect ("root\ cimv2", _

StrUserName, _

StrPassword, _

StrServer, _

ObjService) Then

Call Wscript.Echo ("")

Call Wscript.Echo ("Please check the server name," _

& "credentials and WBEM Core."

Exit Sub

End If

StrID (0) = ""

StrName (0) = ""

StrMessage = ""

StrQuery = "Select * From Win32_OperatingSystem"

Set objEnumerator = objService.ExecQuery (strQuery,0)

If Err.Number Then

Print "Error 0x" & CStr (Hex (Err.Number)) & "occurred during the query."

If Err.Description "" Then

Print "Error description:" & Err.Description & ".

End If

Err.Clear

Exit Sub

End If

I = 0

For Each objInstance in objEnumerator

If blnForce Then

IntStatus = objInstance.Win32ShutDown (CONST_FORCE_SHUTDOWN)

Else

IntStatus = objInstance.Win32ShutDown (CONST_SHUTDOWN)

End If

IF intStatus = 0 Then

StrMessage = "Shuts down machine" & strServer & "."

Else

StrMessage = "Failed to shutdown machine" & strServer & "."

End If

Call WriteLine (strMessage,objOutputFile)

Next

If IsObject (objOutputFile) Then

ObjOutputFile.Close

Call Wscript.Echo ("Results are saved in file" & strOutputFile & ".")

End If

End Sub

'*

'*

'* Function intParseCmdLine ()

'*

'* Purpose: Parses the command line.

'* Input:

'*

'* Output: strServer a remote server ("" = local server ")

'* strUserName the current user's name

'* strPassword the current user's password

'* strOutputFile an output file name

'* intTimer amount of time in seconds

'*

'*

Private Function intParseCmdLine (ByRef strServer, _

ByRef strUserName, _

ByRef strPassword, _

ByRef strOutputFile, _

ByRef blnLogoff, _

ByRef blnShutdown, _

ByRef blnReboot, _

ByRef blnPowerOff, _

ByRef blnForce, _

ByRef intTimer)

ON ERROR RESUME NEXT

Dim strFlag

Dim intState, intArgIter

Dim objFileSystem

If Wscript.Arguments.Count > 0 Then

StrFlag = Wscript.arguments.Item (0)

End If

If IsEmpty (strFlag) Then'No arguments have been received

Wscript.Echo ("Arguments are Required.")

IntParseCmdLine = CONST_ERROR

Exit Function

End If

'Check if the user is asking for help or is just confused

If (strFlag= "help") OR (strFlag= "/ h") OR (strFlag= "\ h") OR (strFlag= "- h") _

OR (strFlag = "\?") OR (strFlag = "/") OR (strFlag = "?") _

OR (strFlag= "h") Then

IntParseCmdLine = CONST_SHOW_USAGE

Exit Function

End If

'Retrieve the command line and set appropriate variables

IntArgIter = 0

Do While intArgIter Len (strString) Then

If blnAfter Then

StrPackString = strString & Space (intWidth-Len (strString))

Else

StrPackString = Space (intWidth-Len (strString)) & strString & ""

End If

Else

If blnTruncate Then

StrPackString = Left (strString, intWidth-1) & ""

Else

StrPackString = strString & ""

End If

End If

End Function

'*

'*

'* Function blnGetArg ()

'*

'* Purpose: Helper to intParseCmdLine ()

'*

'* Usage:

'*

'* Case "/ s"

'* blnGetArg ("server name", strServer, intArgIter)

'*

'*

Private Function blnGetArg (ByVal StrVarName, _

ByRef strVar, _

ByRef intArgIter)

BlnGetArg = False 'failure, changed to True upon successful completion

If Len (Wscript.Arguments (intArgIter)) > 2 then

If Mid (Wscript.Arguments (intArgIter), 3jue 1) = ":" then

If Len (Wscript.Arguments (intArgIter)) > 3 then

StrVar = Right (Wscript.Arguments (intArgIter), _

Len (Wscript.Arguments (intArgIter))-3)

BlnGetArg = True

Exit Function

Else

IntArgIter = intArgIter + 1

If intArgIter > (Wscript.Arguments.Count-1) Then

Call Wscript.Echo ("Invalid" & StrVarName & ".")

Call Wscript.Echo ("Please check the input and try again.")

Exit Function

End If

StrVar = Wscript.Arguments.Item (intArgIter)

If Err.Number Then

Call Wscript.Echo ("Invalid" & StrVarName & ".")

Call Wscript.Echo ("Please check the input and try again.")

Exit Function

End If

If InStr (strVar, "/") Then

Call Wscript.Echo ("Invalid" & StrVarName)

Call Wscript.Echo ("Please check the input and try again.")

Exit Function

End If

BlnGetArg = True 'success

End If

Else

StrVar = Right (Wscript.Arguments (intArgIter), _

Len (Wscript.Arguments (intArgIter))-2)

BlnGetArg = True 'success

Exit Function

End If

Else

IntArgIter = intArgIter + 1

If intArgIter > (Wscript.Arguments.Count-1) Then

Call Wscript.Echo ("Invalid" & StrVarName & ".")

Call Wscript.Echo ("Please check the input and try again.")

Exit Function

End If

StrVar = Wscript.Arguments.Item (intArgIter)

If Err.Number Then

Call Wscript.Echo ("Invalid" & StrVarName & ".")

Call Wscript.Echo ("Please check the input and try again.")

Exit Function

End If

If InStr (strVar, "/") Then

Call Wscript.Echo ("Invalid" & StrVarName)

Call Wscript.Echo ("Please check the input and try again.")

Exit Function

End If

BlnGetArg = True 'success

End If

End Function

'*

'*

'* Function blnConnect ()

'*

'* Purpose: Connects to machine strServer.

'*

'* Input: strServer a machine name

'* strNameSpace a namespace

'* strUserName name of the current user

'* strPassword password of the current user

'*

'* Output: objService is returned as a service object.

'* strServer is set to local host if left unspecified

'*

'*

Private Function blnConnect (ByVal strNameSpace, _

ByVal strUserName, _

ByVal strPassword, _

ByRef strServer, _

ByRef objService)

ON ERROR RESUME NEXT

Dim objLocator, objWshNet

BlnConnect = False 'There is no error.

'Create Locator object to connect to remote CIM object manager

Set objLocator = CreateObject ("WbemScripting.SWbemLocator")

If Err.Number then

Call Wscript.Echo ("Error 0x" & CStr (Hex (Err.Number)) & _

"occurred in creating a locator object."

If Err.Description "" Then

Call Wscript.Echo ("Error description:" & Err.Description & ".")

End If

Err.Clear

BlnConnect = True'An error occurred

Exit Function

End If

'Connect to the namespace which is either local or remote

Set objService = objLocator.ConnectServer (strServer, strNameSpace, _

StrUserName, strPassword)

ObjService.Security_.impersonationlevel = 3

If Err.Number then

Call Wscript.Echo ("Error 0x" & CStr (Hex (Err.Number)) & _

"occurred in connecting to server" _

& strServer & ".")

If Err.Description "" Then

Call Wscript.Echo ("Error description:" & Err.Description & ".")

End If

Err.Clear

BlnConnect = True'An error occurred

End If

'Get the current server's name if left unspecified

If IsEmpty (strServer) Then

Set objWshNet = CreateObject ("Wscript.Network")

StrServer = objWshNet.ComputerName

End If

End Function

'*

'*

'* Sub VerifyHostIsCscript ()

'*

'* Purpose: Determines which program is used to run this script.

'*

'* Input: None

'*

'* Output: If host is not cscript, then an error message is printed

'* and the script is aborted.

'*

'*

Sub VerifyHostIsCscript ()

ON ERROR RESUME NEXT

Dim strFullName, strCommand, i, j, intStatus

StrFullName = WScript.FullName

If Err.Number then

Call Wscript.Echo ("Error 0x" & CStr (Hex (Err.Number)) & "occurred.")

If Err.Description "" Then

Call Wscript.Echo ("Error description:" & Err.Description & ".")

End If

IntStatus = CONST_ERROR

End If

I = InStr (1, strFullName, ".exe", 1)

If I = 0 Then

IntStatus = CONST_ERROR

Else

J = InStrRev (strFullName, "\", I, 1)

If j = 0 Then

IntStatus = CONST_ERROR

Else

StrCommand = Mid (strFullName, juni1, i-j-1)

Select Case LCase (strCommand)

Case "cscript"

IntStatus = CONST_CSCRIPT

Case "wscript"

IntStatus = CONST_WSCRIPT

Case Else 'should never happen

Call Wscript.Echo ("An unexpected program was used to" _

& "run this script."

Call Wscript.Echo ("Only CScript.Exe or WScript.Exe can" _

& "be used to run this script."

IntStatus = CONST_ERROR

End Select

End If

End If

If intStatus CONST_CSCRIPT Then

Call WScript.Echo ("Please run this script using CScript." & vbCRLF & _

"This can be achieved by" & vbCRLF & _

"1. Using"CScript Restart.vbs arguments"for Windows 95 gamma 98 or" _

& vbCRLF & "2. Changing the default Windows Scripting Host" _

& "setting to CScript" & vbCRLF & "using"CScript" _

& "/ / H:CScript / / S"and running the script using" & vbCRLF & _

"" Restart.vbs arguments "" for Windows NT/2000. ")

WScript.Quit

End If

End Sub

'*

'*

'* Sub WriteLine ()

'* Purpose: Writes a text line either to a file or on screen.

'* Input: strMessage the string to print

'* objFile an output file object

'* Output: strMessage is either displayed on screen or written to a file.

'*

'*

Sub WriteLine (ByVal strMessage, ByVal objFile)

On Error Resume Next

If IsObject (objFile) then 'objFile should be a file object

ObjFile.WriteLine strMessage

Else

Call Wscript.Echo (strMessage)

End If

End Sub

'*

'*

'* Function blnErrorOccurred ()

'*

'* Purpose: Reports error with a string saying what the error occurred in.

'*

'* Input: strIn string saying what the error occurred in.

'*

'* Output: displayed on screen

'*

'*

Private Function blnErrorOccurred (ByVal strIn)

If Err.Number Then

Call Wscript.Echo ("Error 0x" & CStr (Hex (Err.Number)) & ":" & strIn)

If Err.Description "" Then

Call Wscript.Echo ("Error description:" & Err.Description)

End If

Err.Clear

BlnErrorOccurred = True

Else

BlnErrorOccurred = False

End If

End Function

'*

'*

'* Function blnOpenFile

'*

'* Purpose: Opens a file.

'*

'* Input: strFileName A string with the name of the file.

'*

'* Output: Sets objOpenFile to a FileSystemObject and setis it to

'* Nothing upon Failure.

'*

'*

Private Function blnOpenFile (ByVal strFileName, ByRef objOpenFile)

ON ERROR RESUME NEXT

Dim objFileSystem

Set objFileSystem = Nothing

If IsEmpty (strFileName) OR strFileName = "" Then

BlnOpenFile = False

Set objOpenFile = Nothing

Exit Function

End If

'Create a file object

Set objFileSystem = CreateObject ("Scripting.FileSystemObject")

If blnErrorOccurred ("Could not create filesystem object.") Then

BlnOpenFile = False

Set objOpenFile = Nothing

Exit Function

End If

'Open the file for output

Set objOpenFile = objFileSystem.OpenTextFile (strFileName, 8, True)

If blnErrorOccurred ("Could not open") Then

BlnOpenFile = False

Set objOpenFile = Nothing

Exit Function

End If

BlnOpenFile = True

End Function

'*

'* *

'* End of File *

'* *

'*

At this point, I believe you have a deeper understanding of "how to write Restart.vbs source code to restart the vbs of a remote computer". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report