shutdown, reboot, logoff, poweroff





3
Date Submitted Thu. Mar. 2nd, 2006 11:27 AM
Revision 1 of 1
Syntax Master dannyboy
Tags logoff | poweroff | Reboot | shutdown | VBSCRIPT
Comments 1 comments
Shutsdown, PowerOff, LogOff, Restarts a machine.

OPTION EXPLICIT

    CONST CONST_ERROR                   = 0
    CONST CONST_WSCRIPT                 = 1
    CONST CONST_CSCRIPT                 = 2
    CONST CONST_SHOW_USAGE              = 3
    CONST CONST_PROCEED                 = 4
    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

    Dim intOpMode, i
    Dim strServer, strUserName, strPassword, strOutputFile
    Dim blnLogoff, blnPowerOff, blnReBoot, blnShutDown
    Dim blnForce
    Dim intTimer
    Dim UserArray(3)
    Dim MyCount

    VerifyHostIsCscript()

    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
        Case Else
            Call Wscript.Echo("Error occurred in passing parameters.")
    End Select


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
    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
    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

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
    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
    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

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
    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
    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

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
    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
    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

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
    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
     intArgIter = 0
    Do While intArgIter <= Wscript.arguments.Count - 1
        Select Case Left(LCase(Wscript.arguments.Item(intArgIter)),2)
            Case "/s"
                intParseCmdLine = CONST_PROCEED
                If Not blnGetArg("Server", strServer, intArgIter) Then
                    intParseCmdLine = CONST_ERROR
                    Exit Function
                End If
                intArgIter = intArgIter + 1
            Case "/o"
                If Not blnGetArg("Output File", strOutputFile, intArgIter) Then
                    intParseCmdLine = CONST_ERROR
                    Exit Function
                End If
                intArgIter = intArgIter + 1
            Case "/u"
                If Not blnGetArg("User Name", strUserName, intArgIter) Then
                    intParseCmdLine = CONST_ERROR
                    Exit Function
                End If
                intArgIter = intArgIter + 1
            Case "/w"
                If Not blnGetArg("User Password", strPassword, intArgIter) Then
                    intParseCmdLine = CONST_ERROR
                    Exit Function
                End If
                intArgIter = intArgIter + 1
            Case "/f"
                blnForce = True
                intArgIter = intArgIter + 1
            Case "/r"
                blnReBoot = True
                userarray(0) = blnReBoot
                intArgIter = intArgIter + 1
            Case "/q"
                blnPowerOff = True
                userarray(1) = blnPowerOff
                intArgIter = intArgIter + 1
            Case "/l"
                blnLogOff = True
                userarray(2) = blnLogoff
                intArgIter = intArgIter + 1
            Case "/p"
                blnShutDown = True
                userarray(3) = blnShutDown
                intArgIter = intArgIter + 1
            Case "/t"
                If Not blnGetArg("Timer", intTimer, intArgIter) Then
                    intParseCmdLine = CONST_ERROR
                    Exit Function
                End If
                intArgIter = intArgIter + 1
            Case Else '
We shouldn't get here
                Call Wscript.Echo("Invalid or misplaced parameter: " & Wscript.arguments.Item(intArgIter) & vbCRLF & "Please check the input and try again," & vbCRLF & "or invoke with '
/?' for help with the syntax.")
                Wscript.Quit
        End Select
    Loop '
** intArgIter <= Wscript.arguments.Count - 1
    MyCount = 0
    for i = 0 to 3
        if userarray(i) = True then
            MyCount = Mycount + 1
        End if
    Next
   if Mycount > 1 then
        intParseCmdLine = CONST_SHOW_USAGE
   End if
   If IsEmpty(intParseCmdLine) Then
        intParseCmdLine = CONST_ERROR
        Wscript.Echo("Arguments are Required.")
   End If
End Function

Private Sub ShowUsage()
    Wscript.Echo ""
    Wscript.Echo "Logoffs, Reboots, Powers Off, or Shuts Down a machine."
    Wscript.Echo ""
    Wscript.Echo "SYNTAX:"
    Wscript.Echo "  Restart.vbs [/S <server>] [/U <username>] [/W <password>]"
    Wscript.Echo "              [/O <outputfile>] </L> </R> </P> </Q> </F> [/T <time in seconds>]"
    Wscript.Echo ""
    Wscript.Echo "PARAMETER SPECIFIERS:"
    wscript.echo "   /T            Amount of time to perform the function."
    Wscript.Echo "   /Q            Perform Shutdown."
    Wscript.Echo "   /P            Perform Poweroff."
    Wscript.Echo "   /R            Perform Reboot."
    Wscript.Echo "   /L            Perform Logoff."
    Wscript.Echo "   /F            Force Function."
    Wscript.Echo "   server        A machine name."
    Wscript.Echo "   username      The current user's name."
    Wscript.Echo "   password      Password of the current user."
    Wscript.Echo "   outputfile    The output file name."
    Wscript.Echo ""
    Wscript.Echo "EXAMPLE:"
    Wscript.Echo "1. cscript Restart.vbs /S MyMachine2 /R"
    Wscript.Echo "   Reboots the current machine MyMachine2."
    Wscript.Echo "2. cscript Restart.vbs /S MyMachine2 /R /F"
    Wscript.Echo "   Forces MyMachine2 to reboot."
    Wscript.Echo "3. cscript Restart.vbs /S MyMachine2 /R /T 30"
    Wscript.Echo "   Reboots the current machine MyMachine2 in 30 seconds."
    Wscript.Echo "NOTE:"
    Wscript.Echo "   The force option will make the machine perform the function even if there are"
    Wscript.Echo "   open and unsaved docuements on the screen."
End Sub

Private Function strPackString( ByVal strString, ByVal intWidth, ByVal blnAfter, ByVal blnTruncate)
    ON ERROR RESUME NEXT
    intWidth      = CInt(intWidth)
    blnAfter      = CBool(blnAfter)
    blnTruncate   = CBool(blnTruncate)
    If Err.Number Then
        Call Wscript.Echo ("Argument type is incorrect!")
        Err.Clear
        Wscript.Quit
    End If
    If IsNull(strString) Then
        strPackString = "null" & Space(intWidth-4)
        Exit Function
    End If
    strString = CStr(strString)
    If Err.Number Then
        Call Wscript.Echo ("Argument type is incorrect!")
        Err.Clear
        Wscript.Quit
    End If
    If intWidth > 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

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),3,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

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.
    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
    objLocator.Security_.Privileges.Add 18, True    'wbemPrivilegeShutdown
    objLocator.Security_.Privileges.Add 23, True    '
wbemPrivilegeRemoteShutdown
    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
    If IsEmpty(strServer) Then
        Set objWshNet = CreateObject("Wscript.Network")
    strServer     = objWshNet.ComputerName
    End If
End Function

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, j+1, 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/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(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

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

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
    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    If blnErrorOccurred("Could not create filesystem object.") Then
        blnOpenFile = False
        Set objOpenFile = Nothing
        Exit Function
    End If
    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


 

Comments

Comments small code
Thu. Aug. 3rd, 2006 9:42 PM    Beginner Devian

Voting