How to shutdown the computer with WSH 
Author Message
 How to shutdown the computer with WSH

Quote:

> Hi to everybody.

> I want to do a wsh that shutdown the computer, it will be run under Windows
> 2000 and Windows XP.  I want to power off the computer don't re-start or
> change user, only power off.

You can use the WMI method win32shutdown:

set wmi = getobject("winmgmts:{(Shutdown)}")
set objset = wmi.instancesof("win32_operatingsystem")
for each obj in objset
  set os = obj :  exit for
next

'os.shutdown
'os.reboot

Const EWX_LOGOFF = 0
Const EWX_SHUTDOWN = 1
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Const EWX_POWEROFF = 8
'
'os.win32shutdown EWX_blablah
' Power off:
os.win32shutdown EWX_POWEROFF

--
torgeir



Fri, 31 Dec 2004 17:26:24 GMT  
 How to shutdown the computer with WSH
With script, you couldn't implement it, unless

1. You call a COM Dll. I have attached a sample code:

Option Explicit

Private Type LUID

  UsedPart As Long

  IgnoredForNowHigh32BitPart As Long

End Type

Private Type TOKEN_PRIVILEGES

  PrivilegeCount As Long

  TheLuid As LUID

  Attributes As Long

End Type

' Beginning of Code

Private Const EWX_SHUTDOWN As Long = 1

Private Const EWX_FORCE As Long = 4

Private Const EWX_REBOOT = 2

Private Declare Function ExitWindowsEx Lib "user32" ( _

  ByVal dwOptions As Long, ByVal dwReserved As Long) As Long

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

Private Declare Function OpenProcessToken Lib "advapi32" ( _

  ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _

  TokenHandle As Long) As Long

Private Declare Function LookupPrivilegeValue Lib "advapi32" _

  Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, _

  ByVal lpName As String, lpLuid As LUID) As Long

Private Declare Function AdjustTokenPrivileges Lib "advapi32" ( _

  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 Sub AdjustToken()

  Const TOKEN_ADJUST_PRIVILEGES = &H20

  Const TOKEN_QUERY = &H8

  Const SE_PRIVILEGE_ENABLED = &H2

  Dim hdlProcessHandle As Long

  Dim hdlTokenHandle As Long

  Dim tmpLuid As LUID

  Dim tkp As TOKEN_PRIVILEGES

  Dim tkpNewButIgnored As TOKEN_PRIVILEGES

  Dim lBufferNeeded As Long

  hdlProcessHandle = GetCurrentProcess()

  OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or _

     TOKEN_QUERY), hdlTokenHandle

  ' Get the LUID for shutdown privilege.

  LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid

  tkp.PrivilegeCount = 1    ' One privilege to set

  tkp.TheLuid = tmpLuid

  tkp.Attributes = SE_PRIVILEGE_ENABLED

  ' Enable the shutdown privilege in the access token of this

  ' process.

  AdjustTokenPrivileges hdlTokenHandle, False, tkp, _

     Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded

End Sub

'NOTE: The code below is just used for test. You can call the methods from
within the script-side.

Private Sub Main()

  AdjustToken

  ' If you don't want to reboot the machine, remove the EWX_REBOOT option
below.

  ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), &HFFFF

End Sub

2. Or make use of the WMI, as torgeir provided.

Gary

This posting is provided "AS IS", with no warranties, and confers no
rights. Enjoyed ASP.NET? http://www.asp.net



Sat, 01 Jan 2005 15:31:39 GMT  
 How to shutdown the computer with WSH
With script, you couldn't implement it, unless

1. You call a COM Dll. I have attached a sample code:

Option Explicit

Private Type LUID

  UsedPart As Long

  IgnoredForNowHigh32BitPart As Long

End Type

Private Type TOKEN_PRIVILEGES

  PrivilegeCount As Long

  TheLuid As LUID

  Attributes As Long

End Type

' Beginning of Code

Private Const EWX_SHUTDOWN As Long = 1

Private Const EWX_FORCE As Long = 4

Private Const EWX_REBOOT = 2

Private Declare Function ExitWindowsEx Lib "user32" ( _

  ByVal dwOptions As Long, ByVal dwReserved As Long) As Long

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

Private Declare Function OpenProcessToken Lib "advapi32" ( _

  ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _

  TokenHandle As Long) As Long

Private Declare Function LookupPrivilegeValue Lib "advapi32" _

  Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, _

  ByVal lpName As String, lpLuid As LUID) As Long

Private Declare Function AdjustTokenPrivileges Lib "advapi32" ( _

  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 Sub AdjustToken()

  Const TOKEN_ADJUST_PRIVILEGES = &H20

  Const TOKEN_QUERY = &H8

  Const SE_PRIVILEGE_ENABLED = &H2

  Dim hdlProcessHandle As Long

  Dim hdlTokenHandle As Long

  Dim tmpLuid As LUID

  Dim tkp As TOKEN_PRIVILEGES

  Dim tkpNewButIgnored As TOKEN_PRIVILEGES

  Dim lBufferNeeded As Long

  hdlProcessHandle = GetCurrentProcess()

  OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or _

     TOKEN_QUERY), hdlTokenHandle

  ' Get the LUID for shutdown privilege.

  LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid

  tkp.PrivilegeCount = 1    ' One privilege to set

  tkp.TheLuid = tmpLuid

  tkp.Attributes = SE_PRIVILEGE_ENABLED

  ' Enable the shutdown privilege in the access token of this

  ' process.

  AdjustTokenPrivileges hdlTokenHandle, False, tkp, _

     Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded

End Sub

'NOTE: The code below is just used for test. You can call the methods from
within the script-side.

Private Sub Main()

  AdjustToken

  ' If you don't want to reboot the machine, remove the EWX_REBOOT option
below.

  ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), &HFFFF

End Sub

2. Or make use of the WMI, as torgeir provided.

Gary

This posting is provided "AS IS", with no warranties, and confers no
rights. Enjoyed ASP.NET? http://www.asp.net



Sat, 01 Jan 2005 15:31:39 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Can I shutdown computer with WSH?

2. Shutdown Computer using VBScript

3. Shutdown computer?

4. Rerun a program automatically in wsh if it dies or is shutdown

5. Shutdown using WSH??

6. Unable to Shutdown Windows95 & reboot via WSH

7. Rerun a program automatically in wsh if it dies or is shutdown

8. Remote shutdown using wsh

9. Shutdown within WSH

10. Execute a cmd proc with WSH on remote computer

11. run a batch file on a remote computer with WSH

12. how to get computer IP address in WSH

 

 
Powered by phpBB® Forum Software