Hi, all
The solution below has been provided by jkaios. But I would like to have this restated / reformated so that it could be pasted into notepad and saved as a vbs-file. What I would like to achieve here is to use the Scheduler in XP to execute the reboot with automatic log on. The SetAutoLogon-function will therefore be the only choice, and the username: User, and password: User1, needs to be hard-coded into the script. Also, if the pc has been shut-down then the user will have to put in his username and password. Can this be worked out?
Option Explicit
Sub Reboot()
If MsgBox("Do you want to reboot now?", vbInformation + vbYesNo, "Reboot") = vbYes Then
If Not RebootMyMachine Then
MsgBox "An error occurred while attempting to reboot."
End If
End If
End Sub
Function RebootMyMachine()
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 oServices = GetObject("winmgmts:{imper
sonationLe
vel=impers
onate,(shu
tdown)}!\\
.\root\cim
v2")
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
T) = 0 Then
RebootMyMachine = True
End If
End With
Next
End Function
Sub Reboot_LogOn()
If MsgBox("Do you want to reboot now?", vbInformation + vbYesNo, "Reboot") = vbYes Then
If MsgBox("Do you want to set the Automatic Logon feature?", vbInformation + vbYesNo, "Reboot") = vbYes Then
If SetAutoLogon("User", "User1") Then
MsgBox "The AutoLogon feature successfully set and will be applied after reboot."
End If
End If
If Not RebootMyMachine Then
MsgBox "An error occurred while attempting to reboot."
End If
End If
End Sub
Public Function SetAutoLogon(sUsername, sPassword, Optional sDomain)
On Error Resume Next
Dim oReg, sRoot
Dim sKey1, sKey2, sKey3, sKey4
Set oReg = CreateObject("WScript.Shel
l")
sRoot = "HKLM\Software\Microsoft\W
indows NT\CurrentVersion\Winlogon
\"
sKey1 = "AutoAdminLogon"
sKey2 = "DefaultUserName"
sKey3 = "DefaultPassword"
sKey4 = "DefaultDomainName"
oReg.RegWrite sRoot & "AutoLogonCount", 1, "REG_DWORD"
'// create AutoAdminLogon key
oReg.RegWrite sRoot & sKey1, "1", "REG_SZ"
If Err Then
MsgBox "ERROR: " & Err.Description, vbCritical, sKey1
End If
'// create DefaultUserName key - THIS KEY MAY ALREADY EXIST
oReg.RegWrite sRoot & sKey2, sUsername, "REG_SZ"
If Err Then
MsgBox "ERROR: " & Err.Description, vbCritical, sKey2
End If
'// create DefaultPassword key
oReg.RegWrite sRoot & sKey3, sPassword, "REG_SZ"
If Err Then
MsgBox "ERROR: " & Err.Description, vbCritical, sKey3
End If
'// create DefautlDomainName key - THIS KEY MAY ALREADY EXIST
If Not IsMissing(sDomain) Then
oReg.RegWrite sRoot & sKey4, sDomain, "REG_SZ"
If Err Then
MsgBox "ERROR: " & Err.Description, vbCritical, sKey4
End If
End If
SetAutoLogon = (Err.Number = 0)
End Function
Public Function DisableAutoLogon()
On Error Resume Next
Dim oReg, sRoot
Dim sKey1, sKey2
' Set oReg = WScript.CreateObject("WScr
ipt.Shell"
)
Set oReg = CreateObject("WScript.Shel
l")
sRoot = "HKLM\Software\Microsoft\W
indows NT\CurrentVersion\Winlogon
\"
sKey1 = "AutoAdminLogon"
sKey2 = "DefaultPassword"
'// either delete or reset the AutoAdminLogon key to zero
oReg.RegDelete sRoot & sKey1
If Err Then
Err.Clear
oReg.RegWrite sRoot & sKey1, "0", "REG_SZ"
If Err Then
MsgBox "ERROR: " & Err.Description, vbCritical, sKey1
End If
End If
'// either delete or reset the DefaultPassword key to NULL
oReg.RegDelete sRoot & sKey2
If Err Then
Err.Clear
oReg.RegWrite sRoot & sKey2, "", "REG_SZ"
If Err Then
MsgBox "ERROR: " & Err.Description, vbCritical, sKey2
End If
End If
DisableAutoLogon = (Err.Number = 0)
End Function
Regards,
Wiley
Start Free Trial