Dear all,
The script below has been provided thanks to jkaios. What I would like is for this to be tweaked so that when the pc is shut-down, then, the user is once again forced to enter his log-on name and password (which might be a different one from the previous user). The way it works now is after reboot or shut-down the pc will log in automatically (using the user name and password provided in the script). I would like the automatic log-in feature only in case of reboot, not after shut-down. If the pc has been shut-down the user needs 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 RebootAutoLogIn
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"
'// 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 = 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