Advertisement

01.09.2007 at 02:50AM PST, ID: 22115894
[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!

8.0

Reboot using vbs - follow up: Log-in automatically, force manual log-in after shut-down - to be saved as a vbs-file

Asked by WileyPowers in Visual Basic Programming

Tags:

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:{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 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.Shell")
   
   sRoot = "HKLM\Software\Microsoft\Windows 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("WScript.Shell")
   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,
Wiley
Start Free Trial
[+][-]01.09.2007 at 04:04AM PST, ID: 18274725

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.09.2007 at 05:08AM PST, ID: 18274931

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.

 
[+][-]01.09.2007 at 05:15AM PST, ID: 18274959

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.26.2007 at 02:22AM PST, ID: 18403054

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.

 
[+][-]03.06.2007 at 07:58PM PST, ID: 18667799

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: const_force_reboot
Sign Up Now!
Solution Provided By: jkaios
Participating Experts: 2
Solution Grade: A
 
 
[+][-]03.07.2007 at 02:01AM PST, ID: 18668888

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.

 
[+][-]03.07.2007 at 03:48AM PST, ID: 18669249

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.

 
[+][-]03.08.2007 at 02:48AM PST, ID: 18677684

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.

 
[+][-]03.08.2007 at 06:46PM PST, ID: 18684761

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.

 
[+][-]03.09.2007 at 03:13AM PST, ID: 18686378

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...
20081112-EE-VQP-42