Advertisement

01.08.2007 at 03:28AM PST, ID: 22114503
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.2

Reboot using vbs - follow up question: Log-in automatically on reboot -  but force manual log-in if the pc has been shut-down

Asked by WileyPowers in Visual Basic Programming

Tags: , ,

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:{impersonationLevel=impersonate,(shutdown)}!\\.\root\cimv2")
   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_REBOOT) = 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.Shell")
   
   sRoot = "HKLM\Software\Microsoft\Windows 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.Shell")
   
   sRoot = "HKLM\Software\Microsoft\Windows 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,
WileyStart Free Trial
[+][-]01.08.2007 at 04:05AM PST, ID: 18266122

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.08.2007 at 12:53PM PST, ID: 18270365

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Visual Basic Programming
Tags: reboot, vbs, using
Sign Up Now!
Solution Provided By: jkaios
Participating Experts: 2
Solution Grade: A
 
 
[+][-]01.09.2007 at 02:43AM PST, ID: 18274463

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32