How to shut down a computer which is in network

anilgupt
anilgupt used Ask the Experts™
on
Hi,
I would like to know that is it possible to shut down a computer which is in network.
+Anil
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Interesting...
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
<listening>
Ryan ChongSoftware Team Lead

Commented:
<ping..>

Commented:
This is excellent! see the remote command option on the menu! You do need administrator rites on the machine you want to shut down/restart

http://www.freevbcode.com/code/CS_Tools_2.zip

Commented:
the important bit is:

On Error Resume Next

Dim computername As String
computername = Text1.Text  'need to type in the computer name here
Set OpSysSet = GetObject("winmgmts:{(Debug,RemoteShutdown)}//" & computername & "/root/cimv2").ExecQuery("select * from Win32_OperatingSystem where Primary=true")

For Each OpSys In OpSysSet
    Call OpSys.Shutdown
Next

Commented:
'---CLASS MODULE (clsShutDown)
Option Explicit


Private Const ANYSIZE_ARRAY = 1
Private Type LUID
    lowpart As Long
    highpart As Long
End Type
Private Type LUID_AND_ATTRIBUTES
    pLuid As LUID
    Attributes As Long
End Type
Private Type TOKEN_PRIVILEGES
    PrivilegeCount As Long
    Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type

Private Declare Function OpenProcessToken Lib "advapi32.dll" ( _
        ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) _
        As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias _
        "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, _
        lpLuid As LUID) As Long
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
Private Declare Function GetLastError Lib "kernel32" () As Long

Private Const DELETE = &H10000
Private Const READ_CONTROL = &H20000
Private Const WRITE_DAC = &H40000
Private Const WRITE_OWNER = &H80000
Private Const SYNCHRONIZE = &H100000

Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const STANDARD_RIGHTS_READ = READ_CONTROL
Private Const STANDARD_RIGHTS_WRITE = READ_CONTROL
Private Const STANDARD_RIGHTS_EXECUTE = READ_CONTROL
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const SPECIFIC_RIGHTS_ALL = &HFFFF&

Private Const TOKEN_ASSIGN_PRIMARY = &H1&
Private Const TOKEN_DUPLICATE = &H2&
Private Const TOKEN_IMPERSONATE = &H4&
Private Const TOKEN_QUERY = &H8&
Private Const TOKEN_QUERY_SOURCE = &H10&
Private Const TOKEN_ADJUST_PRIVILEGES = &H20&
Private Const TOKEN_ADJUST_GROUPS = &H40&
Private Const TOKEN_ADJUST_DEFAULT = &H80&
Private Const TOKEN_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED Or _
                                 TOKEN_ASSIGN_PRIMARY Or _
                                 TOKEN_DUPLICATE Or _
                                 TOKEN_IMPERSONATE Or _
                                 TOKEN_QUERY Or _
                                 TOKEN_QUERY_SOURCE Or _
                                 TOKEN_ADJUST_PRIVILEGES Or _
                                 TOKEN_ADJUST_GROUPS Or _
                                 TOKEN_ADJUST_DEFAULT
Private Const TOKEN_READ = STANDARD_RIGHTS_READ Or TOKEN_QUERY
Private Const TOKEN_WRITE = STANDARD_RIGHTS_WRITE Or _
                            TOKEN_ADJUST_PRIVILEGES Or _
                            TOKEN_ADJUST_GROUPS Or _
                            TOKEN_ADJUST_DEFAULT
Private Const TOKEN_EXECUTE = STANDARD_RIGHTS_EXECUTE

Private Const SE_CREATE_TOKEN_NAME = "SeCreateTokenPrivilege"
Private Const SE_ASSIGNPRIMARYTOKEN_NAME = "SeAssignPrimaryTokenPrivilege"
Private Const SE_LOCK_MEMORY_NAME = "SeLockMemoryPrivilege"
Private Const SE_INCREASE_QUOTA_NAME = "SeIncreaseQuotaPrivilege"
Private Const SE_UNSOLICITED_INPUT_NAME = "SeUnsolicitedInputPrivilege"
Private Const SE_MACHINE_ACCOUNT_NAME = "SeMachineAccountPrivilege"
Private Const SE_TCB_NAME = "SeTcbPrivilege"
Private Const SE_SECURITY_NAME = "SeSecurityPrivilege"
Private Const SE_TAKE_OWNERSHIP_NAME = "SeTakeOwnershipPrivilege"
Private Const SE_LOAD_DRIVER_NAME = "SeLoadDriverPrivilege"
Private Const SE_SYSTEM_PROFILE_NAME = "SeSystemProfilePrivilege"
Private Const SE_SYSTEMTIME_NAME = "SeSystemtimePrivilege"
Private Const SE_PROF_SINGLE_PROCESS_NAME = "SeProfileSingleProcessPrivilege"
Private Const SE_INC_BASE_PRIORITY_NAME = "SeIncreaseBasePriorityPrivilege"
Private Const SE_CREATE_PAGEFILE_NAME = "SeCreatePagefilePrivilege"
Private Const SE_CREATE_PERMANENT_NAME = "SeCreatePermanentPrivilege"
Private Const SE_BACKUP_NAME = "SeBackupPrivilege"
Private Const SE_RESTORE_NAME = "SeRestorePrivilege"
Private Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
Private Const SE_DEBUG_NAME = "SeDebugPrivilege"
Private Const SE_AUDIT_NAME = "SeAuditPrivilege"
Private Const SE_SYSTEM_ENVIRONMENT_NAME = "SeSystemEnvironmentPrivilege"
Private Const SE_CHANGE_NOTIFY_NAME = "SeChangeNotifyPrivilege"
Private Const SE_REMOTE_SHUTDOWN_NAME = "SeRemoteShutdownPrivilege"

Private Const SE_PRIVILEGE_ENABLED_BY_DEFAULT = &H1&
Private Const SE_PRIVILEGE_ENABLED = &H2&
Private Const SE_PRIVILEGE_USED_FOR_ACCESS = &H80000000

Private Const ERROR_SUCCESS = 0&

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 Boolean
Private Declare Function AbortSystemShutdown Lib "advapi32.dll" _
                Alias "AbortSystemShutdownA" (ByVal lpMachineName As String) As Long

Private tkpSaved As TOKEN_PRIVILEGES
Private hToken As Long
'//private property vars
Private m_intTimeout As Integer
Private m_strCompName As String
Private m_blnReboot As Boolean
Private m_blnForce As Boolean
'//Shutdown Message
Private Const MESSAGE_SHUTDOWN As String = "FastTrack Administrator Shutdown"




Public Sub AbortShutDown()
Dim bDummy As Boolean
If Len(m_strCompName) = 0 Then
      bDummy = SetPrivilege(SE_SHUTDOWN_NAME)
Else
      bDummy = SetPrivilege(SE_REMOTE_SHUTDOWN_NAME)
End If
If bDummy Then
      If AbortSystemShutdown(m_strCompName) Then
            ResetPrivilege
      End If
End If
End Sub


Public Sub ResetPrivilege()

    Dim tkp As TOKEN_PRIVILEGES
    Dim lDummy As Long
    Dim lRequired As Long

    lDummy = AdjustTokenPrivileges(hToken, False, tkpSaved, Len(tkp), tkp, lRequired)

End Sub
Public Function SetPrivilege(ByVal cPrivilegeName As String) As Boolean

Dim tkp As TOKEN_PRIVILEGES
Dim lDummy As Long
Dim lRequired As Long
Dim bRetVal As Boolean


' Get the current process token handle so we can get shutdown privilege.
'Not sure if this privilage will work for lower users
'trying to shut down a administrator/need to test.
If OpenProcessToken(GetCurrentProcess(), _
      TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) Then
            ' Get the LUID for shutdown privilege.
            If LookupPrivilegeValue(vbNullString, cPrivilegeName, tkp.Privileges(0).pLuid) > 0 Then
                  tkp.PrivilegeCount = 1      ' one privilege to set
                  tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
                 
                  ' Get shutdown privilege for this process.
                  lDummy = AdjustTokenPrivileges(hToken, False, tkp, Len(tkpSaved), tkpSaved, lRequired)
                  ' Cannot test the return value of AdjustTokenPrivileges.
                  lDummy = GetLastError
                  If lDummy = ERROR_SUCCESS Then
                        bRetVal = True
                  Else
                        'Debug.Print lDummy
                  End If
            End If
End If
SetPrivilege = bRetVal

End Function
Public Function ShutDown() As Boolean
Dim bDummy As Boolean
If Len(m_strCompName) = 0 Then
      bDummy = SetPrivilege(SE_SHUTDOWN_NAME)
Else
      bDummy = SetPrivilege(SE_REMOTE_SHUTDOWN_NAME)
End If
If bDummy Then
      ' Display the shutdown dialog box and start the time-out countdown.
      If InitiateSystemShutdown(m_strCompName, MESSAGE_SHUTDOWN, m_intTimeout, _
            m_blnForce, m_blnReboot) Then
            ' Disable shutdown privilege.
            ResetPrivilege
            ShutDown = True
      Else
            ShutDown = False
      End If
End If

End Function



Public Property Get TimeOut() As Integer
      TimeOut = m_intTimeout
End Property

Public Property Let TimeOut(ByVal intNewValue As Integer)
      m_intTimeout = intNewValue
End Property

Public Property Get remotehost() As String
      remotehost = m_strCompName
End Property

Public Property Let remotehost(ByVal strName As String)
      m_strCompName = strName
End Property

Public Property Get RebootAfterShutDown() As Boolean
       RebootAfterShutDown = m_blnReboot
End Property

Public Property Let RebootAfterShutDown(ByVal bNewValue As Boolean)
      m_blnReboot = bNewValue
End Property


Public Property Get ForceReboot() As Boolean
      ForceReboot = m_blnForce
End Property

Public Property Let ForceReboot(ByVal bNewValue As Boolean)
      m_blnForce = bNewValue
End Property

Private Sub Class_Initialize()
      m_blnForce = True
      m_blnReboot = True
      m_intTimeout = 100
      m_strCompName = ""
End Sub






'How to use
Dim oReboot As New clsShutdown
with oReboot
.RebootAfterShutDown = True
.remotehost = "yourcomputername"
.TimeOut = 10
.ForceReboot = True
.ShutDown
end with

anilgupt:

You have asked 6 questions here at EE, and have only resolved ONE. Here is a list of your other open questions:

http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20257302
http://www.experts-exchange.com/jsp/qShow.jsp?ta=visualbasic&qid=20257307
http://www.experts-exchange.com/jsp/qShow.jsp?ta=javascript&qid=20209746
http://www.experts-exchange.com/jsp/qShow.jsp?ta=oracle&qid=20193594

To assist you in your cleanup, I'm providing the following guidelines:

1.  Stay active in your questions and provide feedback whenever possible. Likewise, when feedback has not been provided by the experts, commenting again makes them receive an email notification, and they may provide you with further information. Experts have no other method of searching for questions in which they have commented, except manually.

2.  Award points by hitting the Accept Comment As Answer button located above and to the left of that expert's comment.

3.  When grading, be sure to read:
http://www.experts-exchange.com/jsp/cmtyQuestAnswer.jsp#3
to ensure that you understand the grading system here at EE. If you grade less than an A, you must explain why.

4.  Questions that were not helpful to you should be PAQ'd (stored in the database for their valuable content?even if not valuable to you) or deleted. To PAQ or delete a question, you must first post your intent in that question to make the experts aware. Then, if no experts object after three full days, you can post a zero-point question at community support to request deletion or PAQ. Please include the link(s) to the question(s).
CS:  http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
At that point, a moderator can refund your points and PAQ or delete the question for you. The delete button does not work.

5.  If you fail to respond to this cleanup request, I must report you to the Community Support Administrator for further action.

Our intent is to get the questions cleaned up, and not to embarrass or shame anyone. If you have any questions or need further assistance at all, feel free to ask me in this question or post a zero-point question at CS. We are very happy to help you in this task!


thanks!
amp
community support moderator
Author of the Year 2009

Commented:
Hi anilgupt,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept Richie_Simonetti's comment(s) as an answer.

anilgupt, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
Per recommendation, force-accepted.

Netminder
CS Moderator

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial