Set the Autologon Script using WMI

Another great code-tip from ASPFree

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 9
March 29, 2003
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement


This script will set the appropriate registry keys to auto-logon a server.  This would be handy when wanting to do something that would require the server to auto-logon, run a script and reboot.  This script could also be used to disable auto-logon, just set the AutoLogonCount=0.  Be very careful when running this script, as the dis-claimer, backup your registry before running.  If your not comfortable in how or why to use this script, don't use it then.   This can be saved in a file (autologon.vbs computer1 computer2 etc..) This accepts any number of arguments.   The argument would set the DefaultDomainName to the local computer name, login as the local Administrator and run the script.   Definitely test out the script on a development machine before using. 

Option Explicit
On Error Resume Next
Err.Clear

dim StdOut, objReg, strKeyPath, strStringValueName, strSetStringValue, strEntryName, strValue, strLoginPath
dim Wshshell, ArgObj, sArgCount, x

Set WshShell = WScript.CreateObject("WScript.Shell")

const HKEY_LOCAL_MACHINE = &H80000002
Set StdOut = WScript.StdOut

strSetStringValue = "c:\winnt\system32\wscript.exe c:\someWSHFile.vbs"
strStringValueName = "Description of the process"

Set ArgObj = WScript.Arguments
sArgCount = ArgObj.Count

For x = 0 to sArgCount - 1
    Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_
    ArgObj(x) & "\root\default:StdRegProv")

    strKeyPath = "SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\ANewRegistryKey"
    strLoginPath = "Software\Microsoft\Windows NT\CurrentVersion\Winlogon"
    objReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
    objReg.SetStringValue HKEY_LOCAL_MACHINE, strLoginPath, "AutoAdminLogon", "1"
    objReg.SetStringValue HKEY_LOCAL_MACHINE, strLoginPath, "DefaultUserName", "Administrator"
    objReg.SetStringValue HKEY_LOCAL_MACHINE, strLoginPath, "DefaultPassword", "Password"
    objReg.SetStringValue HKEY_LOCAL_MACHINE, strLoginPath, "AutoLogonCount", "1"
    objReg.SetStringValue HKEY_LOCAL_MACHINE, strLoginPath, "DefaultDomainName", ArgObj(x)
    objReg.SetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strStringValueName, strSetStringValue
    set objReg = Nothing
Next

set wshshell = nothing
set argobj = nothing

Sub ErrorHandler(byVal errornum, byVal errorDesc)
    theDesc = "Error Number: " & errornum & " Error Description: " & errorDesc
    WshShell.LogEvent 1, theDesc
    err.clear
End Sub

blog comments powered by Disqus
WINDOWS SCRIPTING ARTICLES

- More Windows Scripting Workarounds from Nilpo
- Overloading Methods and More in VBScript
- Improving MFC for Windows Vista
- Regular Expressions in VBScript
- Working with Dates in WMI
- Completing Calendars with VBScript Date Func...
- Building Calendars with VBScript Date Functi...
- Working With Dates and Times in VBScript
- Designing WCF DataContract Classes Using the...
- Understanding Dates and Times in VBScript
- Working With Arrays in VBScript
- Compressed Folders in WSH
- Using .NET Interops in VBScript
- Nilpo`s Scripting Secrets, Vol I
- Database operations using Silverlight 2.0 WC...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials