Results 1 to 5 of 5

Thread: How to Shutdown Network Computer?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    78

    How to Shutdown Network Computer?

    How can I shutdown a computer using different computer?
    To become a PROFESSIONAL,
    Start from SCRATCH...

  2. #2
    Member
    Join Date
    Jul 2006
    Posts
    47

    Re: How to Shutdown Network Computer?

    Use InitiateSystemShutdown API Call

    VB Code:
    1. Option Explicit
    2.  
    3. Private Type LARGE_INTEGER
    4.     LowPart As Long
    5.     HighPart As Long
    6. End Type
    7. Private Type LUID
    8.     LowPart As Long
    9.     HighPart As Long
    10. End Type
    11. Private Type LUID_AND_ATTRIBUTES
    12.     pLuid As LUID
    13.     Attributes As Long
    14. End Type
    15. Private Type TOKEN_PRIVILEGES
    16.     PrivilegeCount As Long
    17.     Privileges(0 To 0) As LUID_AND_ATTRIBUTES
    18. End Type
    19. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    20. Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    21. Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    22. Private Declare Function GetTokenInformation Lib "advapi32.dll" (ByVal TokenHandle As Long, TokenInformationClass As Integer, TokenInformation As Any, ByVal TokenInformationLength As Long, ReturnLength As Long) As Long
    23. Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
    24. Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
    25. Private Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
    26. Private Const SE_PRIVILEGE_ENABLED = &H2
    27.  
    28. Private Const READ_CONTROL = &H20000
    29. Private Const STANDARD_RIGHTS_ALL = &H1F0000
    30. Private Const STANDARD_RIGHTS_EXECUTE = (READ_CONTROL)
    31. Private Const STANDARD_RIGHTS_READ = (READ_CONTROL)
    32. Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
    33. Private Const STANDARD_RIGHTS_WRITE = (READ_CONTROL)
    34.  
    35. Private Const TOKEN_ASSIGN_PRIMARY = &H1
    36. Private Const TOKEN_DUPLICATE = (&H2)
    37. Private Const TOKEN_IMPERSONATE = (&H4)
    38. Private Const TOKEN_QUERY = (&H8)
    39. Private Const TOKEN_QUERY_SOURCE = (&H10)
    40. Private Const TOKEN_ADJUST_PRIVILEGES = (&H20)
    41. Private Const TOKEN_ADJUST_GROUPS = (&H40)
    42. Private Const TOKEN_ADJUST_DEFAULT = (&H80)
    43. Private Const TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or TOKEN_ASSIGN_PRIMARY Or TOKEN_DUPLICATE Or TOKEN_IMPERSONATE Or TOKEN_QUERY Or _
    44.                 TOKEN_QUERY_SOURCE Or TOKEN_ADJUST_PRIVILEGES Or TOKEN_ADJUST_GROUPS Or TOKEN_ADJUST_DEFAULT)
    45.  
    46. Private Const TOKEN_READ = (STANDARD_RIGHTS_READ Or TOKEN_QUERY)
    47. Private Const TOKEN_WRITE = (STANDARD_RIGHTS_WRITE Or TOKEN_ADJUST_PRIVILEGES Or TOKEN_ADJUST_GROUPS Or TOKEN_ADJUST_DEFAULT)
    48. Private Const TOKEN_EXECUTE = (STANDARD_RIGHTS_EXECUTE)
    49.  
    50. Private Const TokenDefaultDacl = 6, TokenGroups = 2
    51. Private Const TokenImpersonationLevel = 9, TokenOwner = 4
    52. Private Const TokenPrimaryGroup = 5, TokenPrivileges = 3
    53. Private Const TokenSource = 7, TokenStatistics = 10
    54. Private Const TokenType = 8, TokenUser = 1
    55.  
    56. Private Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, ByVal lpMessage As String, ByVal dwTimeout As Long, ByVal bForceAppsClosed As Long, ByVal bRebootAfterShutdown As Long) As Long
    57. Private Declare Function AbortSystemShutdown Lib "advapi32.dll" Alias "AbortSystemShutdownA" (ByVal lpMachineName As String) As Long
    58.  
    59.  
    60.  
    61. 'Purpose     :  Starts a timed remote shut down
    62. 'Inputs      :  sMachineNetworkName                 The name of the network machine. If sMachineNetworkName
    63. '                                                   is an empty string the function shuts down the local computer
    64. '               lTimeOut                            The time in seconds to display the shutdown dialog. While this
    65. '                                                   dialog box is displayed, the shutdown can be stopped by the ShutDownAbort function
    66. '               sMessageTitle                       The message sent to the remote machine during the shutdown
    67. '               bForceAppsToClose                   Specifies whether applications with unsaved changes are to be forcibly closed.
    68. '                                                   If this parameter is TRUE, such applications are closed. If this parameter is FALSE,
    69. '                                                   a dialog box is displayed prompting the user to close the applications
    70. '               bReboot                             Specifies whether the computer is to restart immediately after shutting down.
    71. '                                                   If this parameter is TRUE, the computer is to restart. If this parameter is FALSE,
    72. '                                                   the system flushes all caches to disk, clears the screen, and displays a message
    73. '                                                   indicating that it is safe to power down.
    74.  
    75.  
    76. 'Notes       :  To shut down the local computer, the calling process must have the SE_SHUTDOWN_NAME privilege. To shut down a remote computer,
    77. '               the calling process must have the SE_REMOTE_SHUTDOWN_NAME privilege on the remote computer. By default, users can enable the
    78. '               SE_SHUTDOWN_NAME privilege on the computer they are logged onto, and administrators can enable the SE_REMOTE_SHUTDOWN_NAME privilege on remote computers.
    79.  
    80.  
    81. Public Function ShutDownTimedBegin(sMachineNetworkName As String, Optional ByVal lTimeOut As Long = 60, Optional ByVal sMessageTitle As String = "Your Machine is going to be Shutdown", Optional ByVal bForceAppsToClose As Boolean = False, Optional ByVal bReboot As Boolean = True) As Boolean
    82.     Dim lRet As Long
    83.    
    84.     On Error Resume Next
    85.     'Make sure we have enabled the privilege to shutdown
    86.     'for this process
    87.     If zEnableShutDown(sMessageTitle) = True Then
    88.         'carry out timed shutdown
    89.         lRet = InitiateSystemShutdown(sMachineNetworkName, sMessageTitle, lTimeOut, bForceAppsToClose, bReboot)
    90.         If lRet Then
    91.             'Succeeded
    92.             ShutDownTimedBegin = True
    93.         Else
    94.             'Failed
    95.             ShutDownTimedBegin = False
    96.         End If
    97.     Else
    98.         'Failed
    99.         ShutDownTimedBegin = False
    100.     End If
    101.     On Error GoTo 0
    102. End Function
    103.  
    104. 'Purpose     :  Aborts a shut down process
    105. 'Inputs      :  sMachineNetworkName                 The name of the network machine
    106.  
    107.  
    108. Public Function ShutDownAbort(ByVal sMachineNetworkName As String) As Long
    109.     ShutDownAbort = AbortSystemShutdown(sMachineNetworkName)
    110. End Function
    111.  
    112.  
    113. Private Function zEnableShutDown(ByRef sMsg As String) As Boolean
    114.     Dim tLUID As LUID
    115.     Dim hProcess As Long
    116.     Dim hToken As Long
    117.     Dim tTP As TOKEN_PRIVILEGES, tTPOld As TOKEN_PRIVILEGES
    118.     Dim lTpOld As Long
    119.     Dim lR As Long
    120.  
    121.     'Under NT we must enable the SE_SHUTDOWN_NAME privilege in the
    122.     'process we're trying to shutdown from, otherwise a call to
    123.     'try to shutdown has no effect
    124.  
    125.     'Find the LUID of the Shutdown privilege token
    126.     lR = LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tLUID)
    127.    
    128.     If (lR <> 0) Then
    129.         'Get the current process handle
    130.         hProcess = GetCurrentProcess
    131.         If (hProcess <> 0) Then
    132.             'Open the token for adjusting and querying (if we can - user may not have rights)
    133.             lR = OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken)
    134.             If (lR <> 0) Then
    135.                        
    136.                 'Adjust the shutdown priviledges
    137.                 With tTP
    138.                     .PrivilegeCount = 1
    139.                     With .Privileges(0)
    140.                         .Attributes = SE_PRIVILEGE_ENABLED
    141.                         .pLuid.HighPart = tLUID.HighPart
    142.                         .pLuid.LowPart = tLUID.LowPart
    143.                     End With
    144.                 End With
    145.                
    146.                 'Now allow this process to shutdown the system
    147.                 lR = AdjustTokenPrivileges(hToken, 0, tTP, Len(tTP), tTPOld, lTpOld)
    148.                
    149.                 If (lR <> 0) Then
    150.                     zEnableShutDown = True
    151.                 Else
    152.                     'You do not have the privileges to shutdown this system
    153.                     zEnableShutDown = False
    154.                 End If
    155.                 CloseHandle hToken
    156.             Else
    157.                 'You do not have the privileges to shutdown this system
    158.                 zEnableShutDown = False
    159.             End If
    160.         Else
    161.             'Can't enable shutdown (Can't determine the current process)
    162.             zEnableShutDown = False
    163.         End If
    164.     Else
    165.         'Can't enable shutdown (Can't find the SE_SHUTDOWN_NAME privilege value)
    166.         zEnableShutDown = False
    167.     End If
    168.  
    169. End Function

  3. #3
    New Member
    Join Date
    Oct 2005
    Posts
    9

    Re: How to Shutdown Network Computer?

    Gee, isn't that a bit of an overkill?

    How about this?

    Option Explicit

    Public gobjWshShell

    Sub ScriptInit()
    Set gobjWshShell = CreateObject("WScript.Shell")
    'WScript.Echo "Inside the Init subroutine."
    End Sub

    Function Sleep(lintNumOfSeconds, lblnUseMilliseconds)
    ' Put False as the second argument to use standard seconds.
    ' Put True to use milliseconds.
    If lblnUseMilliseconds = False Then
    lintNumOfSeconds = lintNumOfSeconds * 1000
    End If
    WScript.Sleep lintNumOfSeconds
    End Function

    Sub ShutDown(lintShutdownType)
    Dim strComputer
    Dim OpSys
    Dim OpSysSet
    strComputer = "."
    If lintShutDownType = "" then
    lintShutDownType = 4
    End If
    Set OpSysSet = GetObject("winmgmts:{(Debug,Shutdown)}//" & strComputer & "/root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")
    For Each OpSys in OpSysSet
    OpSys.win32Shutdown(lintShutdownType)
    Next
    ' The numbers in the Decimal column are for the lintShutDownType variable.
    ' Action Decimal Binary
    ' Logoff 0 0000
    ' Reboot 2 0010
    ' Force Logoff 4 0100
    ' Force Reboot 6 0110
    ' Powerdown 8 1000
    ' Force Powerdown 12 1100
    End Sub

    Sub RunFor30()
    'WScript.Echo "Inside the RunFor30 subroutine."
    On Error Resume Next
    Dim lblnIsDone
    Dim lintNumMinutes
    lintNumMinutes = 0
    Do
    lblnIsDone = False
    Sleep 5, False
    lintNumMinutes = lintNumMinutes + 1
    'WScript.Echo "lintNumMinutes = " & lintNumMinutes
    Select Case lintNumMinutes
    Case 2
    gobjWshShell.Popup "Attention!! " & vbCrLf & vbCrLf & "This workstation will be logged off in 15 seconds!" & vbCrLf & "AHLTA Version 2.1.837.4 has been detected and must be upgraded to version 2.1.837.9." & vbCrLf & "Please call the Helpdesk at 7-9966." & vbCrLf & vbCrLf & "This box will close in 15 seconds.", 15, "Logoff Warning", vbExclamation + vbOKOnly + vbSystemModal

    Case 5
    lblnIsDone = True
    ShutDown 4
    'WScript.Echo "Shutdown would have occurred here ... click OK button to continue."
    Case Else
    ' Do nothing
    End Select
    Loop Until lblnIsDone = True
    End Sub

    ' Processing starts here

    ScriptInit
    RunFor30
    WScript.Quit

  4. #4
    Fanatic Member TokersBall_CDXX's Avatar
    Join Date
    Mar 2003
    Location
    America
    Posts
    571

    Re: How to Shutdown Network Computer?

    if you have admin rights on the system
    considering shelling shutdown.exe


    Code:
    Usage: shutdown.exe [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] -c "comment"] [-d up:xx:yy]
    
            No args                 Display this message (same as -?)
            -i                      Display GUI interface, must be the first option
            -l                      Log off (cannot be used with -m option)
            -s                      Shutdown the computer
            -r                      Shutdown and restart the computer
            -a                      Abort a system shutdown
            -m \\computername       Remote computer to shutdown/restart/abort
            -t xx                   Set timeout for shutdown to xx seconds
            -c "comment"            Shutdown comment (maximum of 127 characters)
            -f                      Forces running applications to close without warning
            -d [u][p]:xx:yy         The reason code for the shutdown
                                    u is the user code
                                    p is a planned shutdown code
                                    xx is the major reason code (positive integer less than 256)
                                    yy is the minor reason code (positive integer less than 65536)
    Build your own personalized flash based chat room for your webpage for FREE! http://www.4computerheaven.com

  5. #5
    Member
    Join Date
    Jul 2006
    Posts
    47

    Re: How to Shutdown Network Computer?

    I admit you don't need all that code but it ensures you have all the correct token priveliges.

    Wolfess's code is ok providing you have wmi scripting and admin rights for wmi on the remote machine.

    The shutdown.exe method is easiest but some people might not want to use it in certain circumstances and would raher write their own

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width