WileyPowers
asked on
Reboot using vbs
Dear all,
I'm looking for a vbs script that will reboot my pc and log me on automatically. For the record, I am a regular user enrolled in the local network and the operating system is Windows XP Professional. Lets assume that my user id is: User, and that my Password is: User_01.
Is this possible to work out?
Regards,
Wiley
I'm looking for a vbs script that will reboot my pc and log me on automatically. For the record, I am a regular user enrolled in the local network and the operating system is Windows XP Professional. Lets assume that my user id is: User, and that my Password is: User_01.
Is this possible to work out?
Regards,
Wiley
ASKER
Hi jkaios,
Thanks for trying, but I got the following error message:
Run-time error '-2147217310 (80041062)': Privilege not held.
In the actual script it halted at: ... If .Win32ShutDown (CONST_REBOOT) = 0 Then ...
Can you still fix it?
Regards,
Wiley
Thanks for trying, but I got the following error message:
Run-time error '-2147217310 (80041062)': Privilege not held.
In the actual script it halted at: ... If .Win32ShutDown (CONST_REBOOT) = 0 Then ...
Can you still fix it?
Regards,
Wiley
Add the following line just before calling the "ExecQuery" method.
Set oLocator = CreateObject("WbemScriptin g.SWbemLoc ator")
Set oServices = oLocator.ConnectServer("lo calhost", "root\cimv2")
oServices.Security_.Impers onationLev el = 3 '<<<-------------------- ADD THIS LINE ------------------------>> >
Set objEnum = oServices.ExecQuery(sQuery )
Set oLocator = CreateObject("WbemScriptin
Set oServices = oLocator.ConnectServer("lo
oServices.Security_.Impers
Set objEnum = oServices.ExecQuery(sQuery
just create a new VB project then cut and paste this code to form's code. This will force your Windows shutdown immediately after you answer "y" (I set the inputbox to ensure you really want to do this, remember to close all your works before run this code).
'============start cut here =================
Option Explicit
'''''''''''''''''''''''''' '''''''''' '''''''''' '
'Use for WINNT Restart, Logoff, Shutdown
Private Const EWX_LOGOFF = 0
Private Const EWX_SHUTDOWN = 1
Private Const EWX_REBOOT = 2
Private Const EWX_POWEROFF = 8
Private Const EWX_FORCE = 4
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_QUERY = &H8
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const ANYSIZE_ARRAY = 1
Private Const VER_PLATFORM_WIN32_NT = 2
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
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
'Shutdown, Restart, LogOff
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags 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 Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) As Long
Private Const Process_Force = True
'------------------------- ---------- ---------- -----
'Detect if the program is running under Windows NT
Private Function IsWinNT() As Boolean
Dim myOS As OSVERSIONINFO
myOS.dwOSVersionInfoSize = Len(myOS)
GetVersionEx myOS
IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Function
'set the shut down privilege for the current application
Private Sub EnableShutDown()
Dim hProc As Long
Dim hToken As Long
Dim mLUID As LUID
Dim mPriv As TOKEN_PRIVILEGES
Dim mNewPriv As TOKEN_PRIVILEGES
hProc = GetCurrentProcess()
OpenProcessToken hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken
LookupPrivilegeValue "", "SeShutdownPrivilege", mLUID
mPriv.PrivilegeCount = 1
mPriv.Privileges(0).Attrib utes = SE_PRIVILEGE_ENABLED
mPriv.Privileges(0).pLuid = mLUID
AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * mPriv.PrivilegeCount), mNewPriv, 4 + (12 * mNewPriv.PrivilegeCount)
End Sub
''Restart NT
Private Sub RebootNT(Force As Boolean)
Dim ret As Long
Dim Flags As Long
Flags = EWX_REBOOT
If Force Then Flags = Flags + EWX_FORCE
If IsWinNT Then EnableShutDown
ExitWindowsEx Flags, 0
End Sub
Private Sub Form_Load()
Dim q
q = InputBox("Are you sure to force Windows to restart (y/n) ?", "Restart Windows", "n")
If LCase(q) = "y" Then
'RebootNT Process_Force
Msgbox "Chet"
End If
End Sub
'============stop cut here =================
'============start cut here =================
Option Explicit
''''''''''''''''''''''''''
'Use for WINNT Restart, Logoff, Shutdown
Private Const EWX_LOGOFF = 0
Private Const EWX_SHUTDOWN = 1
Private Const EWX_REBOOT = 2
Private Const EWX_POWEROFF = 8
Private Const EWX_FORCE = 4
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_QUERY = &H8
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const ANYSIZE_ARRAY = 1
Private Const VER_PLATFORM_WIN32_NT = 2
Private Type OSVERSIONINFO
dwOSVersionInfoSize As Long
dwMajorVersion As Long
dwMinorVersion As Long
dwBuildNumber As Long
dwPlatformId As Long
szCSDVersion As String * 128
End Type
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
'Shutdown, Restart, LogOff
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags 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 Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) As Long
Private Const Process_Force = True
'-------------------------
'Detect if the program is running under Windows NT
Private Function IsWinNT() As Boolean
Dim myOS As OSVERSIONINFO
myOS.dwOSVersionInfoSize = Len(myOS)
GetVersionEx myOS
IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
End Function
'set the shut down privilege for the current application
Private Sub EnableShutDown()
Dim hProc As Long
Dim hToken As Long
Dim mLUID As LUID
Dim mPriv As TOKEN_PRIVILEGES
Dim mNewPriv As TOKEN_PRIVILEGES
hProc = GetCurrentProcess()
OpenProcessToken hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken
LookupPrivilegeValue "", "SeShutdownPrivilege", mLUID
mPriv.PrivilegeCount = 1
mPriv.Privileges(0).Attrib
mPriv.Privileges(0).pLuid = mLUID
AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * mPriv.PrivilegeCount), mNewPriv, 4 + (12 * mNewPriv.PrivilegeCount)
End Sub
''Restart NT
Private Sub RebootNT(Force As Boolean)
Dim ret As Long
Dim Flags As Long
Flags = EWX_REBOOT
If Force Then Flags = Flags + EWX_FORCE
If IsWinNT Then EnableShutDown
ExitWindowsEx Flags, 0
End Sub
Private Sub Form_Load()
Dim q
q = InputBox("Are you sure to force Windows to restart (y/n) ?", "Restart Windows", "n")
If LCase(q) = "y" Then
'RebootNT Process_Force
Msgbox "Chet"
End If
End Sub
'============stop cut here =================
Sorry, wrong place 's posting.
ASKER
Hi jkaios,
I made the amendment you suggested, but still the same error message:
Run-time error '-2147217310 (80041062)': Privilege not held.
In the actual script it halted at: ... If .Win32ShutDown (CONST_REBOOT) = 0 Then ...
Any other suggestions?
Regards,
Wiley
I made the amendment you suggested, but still the same error message:
Run-time error '-2147217310 (80041062)': Privilege not held.
In the actual script it halted at: ... If .Win32ShutDown (CONST_REBOOT) = 0 Then ...
Any other suggestions?
Regards,
Wiley
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
jkaios,
Great work! The reboot was successful, but I would also like the routine to log me in automatically. Let’s assume that my user id is: User and that my password is: User1
Is this possible to work out?
Regards,
Wiley
Great work! The reboot was successful, but I would also like the routine to log me in automatically. Let’s assume that my user id is: User and that my password is: User1
Is this possible to work out?
Regards,
Wiley
Well, if you got what you originally asked for then I think you should close this question and then open a new one for the automatic logon question you just asked. I would love to help you on that new one, too.
If it's logging into a domain then something along these lines :
'------------------
Option Explicit
Dim ws
Set ws = WScript.CreateObject("WScr ipt.Shell" )
Dim strKeyPath
strKeyPath = "HKLM\SOFTWARE\Microsoft\W indows NT\CurrentVersion\Winlogon \"
ws.RegWrite strKeyPath & "AutoAdminLogon", "1", "REG_SZ"
ws.RegWrite strKeyPath & "AutoLogonCount", "1", "REG_SZ"
ws.RegWrite strKeyPath & "DefaultPassword", "password", "REG_SZ"
ws.RegWrite strKeyPath & "DefaultUserName", "Administrator", "REG_SZ"
ws.RegWrite strKeyPath & "DefaultDomainName", "domain", "REG_SZ"
ws.RegWrite strKeyPath & "AltDefaultDomainName", "domain", "REG_SZ"
ws.RegWrite strKeyPath & "CachePrimaryDomain", "domain", "REG_SZ"
'--------------------
Before you run this vbscript I suggest you go to the specific registry key and back it up so that you have a copy of it.
'------------------
Option Explicit
Dim ws
Set ws = WScript.CreateObject("WScr
Dim strKeyPath
strKeyPath = "HKLM\SOFTWARE\Microsoft\W
ws.RegWrite strKeyPath & "AutoAdminLogon", "1", "REG_SZ"
ws.RegWrite strKeyPath & "AutoLogonCount", "1", "REG_SZ"
ws.RegWrite strKeyPath & "DefaultPassword", "password", "REG_SZ"
ws.RegWrite strKeyPath & "DefaultUserName", "Administrator", "REG_SZ"
ws.RegWrite strKeyPath & "DefaultDomainName", "domain", "REG_SZ"
ws.RegWrite strKeyPath & "AltDefaultDomainName", "domain", "REG_SZ"
ws.RegWrite strKeyPath & "CachePrimaryDomain", "domain", "REG_SZ"
'--------------------
Before you run this vbscript I suggest you go to the specific registry key and back it up so that you have a copy of it.
ASKER
jkaios,
If you read my original question you will see that I ask for a routine that both reboots the pc and log me in automatically.
The question I asked was this: "I'm looking for a vbs script that will reboot my pc and log me on automatically. For the record, I am a regular user enrolled in the local network and the operating system is Windows XP Professional. Lets assume that my user id is: User, and that my Password is: User_01."
However, if you feel that this is a separate issue, then, I'm happy to close the question and re-state it, or alternatively, increase the points. Let me know how you feel about it.
Regards,
Wiley
If you read my original question you will see that I ask for a routine that both reboots the pc and log me in automatically.
The question I asked was this: "I'm looking for a vbs script that will reboot my pc and log me on automatically. For the record, I am a regular user enrolled in the local network and the operating system is Windows XP Professional. Lets assume that my user id is: User, and that my Password is: User_01."
However, if you feel that this is a separate issue, then, I'm happy to close the question and re-state it, or alternatively, increase the points. Let me know how you feel about it.
Regards,
Wiley
ASKER
gecko_au2003,
Thanks for joining. As I am a novice at this, could you please demonstrate how your suggestion ties in with the reboot routine? Also, could you please explain what your suggested script does (line by line)? This because I am a bit weary of messing about with the registry.
Regards,
Wiley
Thanks for joining. As I am a novice at this, could you please demonstrate how your suggestion ties in with the reboot routine? Also, could you please explain what your suggested script does (line by line)? This because I am a bit weary of messing about with the registry.
Regards,
Wiley
ASKER
jkaios,
Many thanks for your solution on the reboot. However, since I also would like to be logged in automatically as a part of the routine I will restate the question.
Regards,
Wiley
Many thanks for your solution on the reboot. However, since I also would like to be logged in automatically as a part of the routine I will restate the question.
Regards,
Wiley
Private Sub InitReboot()
If MsgBox("Do you want to reboot now?", vbInformation + vbYesNo) = vbYes Then
If Not RebootMyMachine Then
MsgBox "An error occurred while attempting to reboot."
End If
End If
End Sub
'=========================
Function RebootMyMachine()
Dim oLocator
Dim oServices
Dim objEnum
Dim objInstance
Dim sQuery
'Shutdown Method Constants
Const CONST_LOGOFF = 0
Const CONST_SHUTDOWN = 1
Const CONST_REBOOT = 2
Const CONST_FORCE_LOGOFF = 4
Const CONST_FORCE_SHUTDOWN = 5
Const CONST_FORCE_REBOOT = 6
Const CONST_POWEROFF = 8
Const CONST_FORCE_POWEROFF = 12
sQuery = "SELECT * FROM Win32_OperatingSystem"
Set oLocator = CreateObject("WbemScriptin
Set oServices = oLocator.ConnectServer("lo
Set objEnum = oServices.ExecQuery(sQuery
If Err Then
MsgBox "Error 0x" & Hex(Err.Number) & " - " & Err.Description
Exit Function
End If
For Each objInstance In objEnum
With objInstance
If .Win32ShutDown(CONST_REBOO
RebootMyMachine = True
End If
End With
Next
End Function
'=========================