home *** CD-ROM | disk | FTP | other *** search
- <?xml version="1.0" ?>
- <package>
- <comment>
- ' ***
- ' *** ------------------------------------------------------------------------------
- ' *** Filename: AutoLogon.wsf
- ' *** ------------------------------------------------------------------------------
- ' *** Description: Command-line tool to Autologon Windows XP with a specific account
- ' *** ------------------------------------------------------------------------------
- ' *** Version: 1.0
- ' *** Notes:
- ' *** ------------------------------------------------------------------------------
- ' *** Copyright (C) Microsoft Corporation 2005, All Rights Reserved
- ' *** ------------------------------------------------------------------------------
- ' ***
- </comment>
- <job>
- <runtime>
- <description>AutoLogon Tool</description>
- <named name="enable" required="false" many="false" helpstring="Enable account to log on to Windows automatically" />
- <named name="disable" required="false" many="false" helpstring="Disable automatic logon to Windows" />
- <usage>
- AutoLogon Tool Usage Instructions
- ---------------------------------
-
- Options:
- Enable - Enable account to log on to Windows automatically.
- Disable - Disable automatic logon to Windows.
-
- Example: AutoLogon.wsf /Enable username password
- Example: AutoLogon.wsf /Disable
- </usage>
- </runtime>
-
- <resource id="CScriptMessage">Restarting script in command-line mode. Run CmdOn.BAT to set command-line mode as the default mode.</resource>
- <resource id="CScriptTitle">Windows SteadyState: Windows Script Mode Detected</resource>
-
- <?job error="True" debug="False" ?>
-
- <script language="VBScript">
- <![CDATA[
-
- ' ~~~
- ' ~~~ Force variables to be declared
- ' ~~~
- Option Explicit
-
- On Error Resume Next
-
- ' ~~~
- ' ~~~ Declare global variables
- ' ~~~
- Dim sUser, sPassword
- Dim oWMIService, oNetwork, oWmiReg
- Dim HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS
-
- HKEY_CURRENT_USER = &H80000001
- HKEY_LOCAL_MACHINE = &H80000002
- HKEY_USERS = &H80000003
-
- Set oNetwork = CreateObject("Wscript.Network")
- Set oWMIService = GetObject("winmgmts:" _
- & "{impersonationLevel=impersonate}!\\" & "." & "\root\cimv2")
- Set oWmiReg = GetObject("winmgmts:" _
- & "{impersonationLevel=impersonate}!\\" & "." & "\root\default:StdRegProv")
-
- If WScript.Arguments.Named.Exists("enable") Then
- sUser = Wscript.Arguments(1)
- If sUser = "" Then
- Wscript.Echo "Error: AutoLogon.wsf /Enable - requires an account"
- Wscript.Arguments.ShowUsage
- ElseIf IsValidUser(sUser) Then
- sPassword = ""
- sPassword = Wscript.Arguments(2)
- RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon", 1, "REG_SZ"
- RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName", sUser, "REG_SZ"
- RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword", sPassword, "REG_SZ"
- RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName", oNetwork.ComputerName, "REG_SZ"
- RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\ForceAutoLogon", 1, "REG_DWORD"
- Wscript.Echo "Enabled the " & sUser & " account with password " & Chr(34) & sPassword & Chr(34) & " to Auto-Logon to Windows."
- Wscript.Echo "To logon as a different user, press and hold the Shift key during logoff or while restarting the computer."
- Else
- Wscript.Echo "Error: AutoLogon.wsf /Enable - Please enter a valid user account"
- Wscript.Arguments.ShowUsage
-
- End If
- ElseIf WScript.Arguments.Named.Exists("disable") Then
- RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\AutoAdminLogon", 0, "REG_SZ"
- RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultUserName", "", "REG_SZ"
- RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultPassword", "", "REG_SZ"
- RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\DefaultDomainName", "", "REG_SZ"
- RegWrite "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\ForceAutoLogon", 0, "REG_DWORD"
- Wscript.Echo "Disabled Auto-Logon to Windows."
- Else
- Wscript.Arguments.ShowUsage
- End If
-
- ' ***
- ' *** --------------------------------------------------------------------------------
- ' *** Name: IsValidUser(sUser)
- ' *** --------------------------------------------------------------------------------
- ' *** Purpose: Checks whether the user is valid
- ' *** --------------------------------------------------------------------------------
- ' ***
- Function IsValidUser(sUser)
- Dim oAccount
-
- IsValidUser = True
-
- ' ~~~ Turn on error handling
- On Error Resume Next
-
- ' ~~~ Create wmi object & get sid
- Set oAccount = oWMIService.Get("Win32_UserAccount.Name='" & sUser & "',Domain='" & oNetwork.ComputerName & "'")
- If oAccount.SID = "" Then IsValidUser = False
-
- Set oAccount = Nothing
- End Function
-
- ' ***
- ' *** ------------------------------------------------------------------------------
- ' *** Name: RegWrite(sRegKey, sValue, sType)
- ' *** ------------------------------------------------------------------------------
- ' *** Purpose: Writes a registry key using WMI
- ' *** ------------------------------------------------------------------------------
- ' ***
- Function RegWrite(sRegKey, sValue, sType)
-
- Dim sRegHive, iCharStart, sRegKeyPath, sRegKeyName, iCharEnd, iReturn
-
- ' ~~~ Turn on error 'handling'
- On Error Resume Next
-
- iReturn = 0
-
- iCharStart = InStr(sRegKey, "\")
- ' ~~~ Get HKLM\HKCU\HKU
- sRegHive = Left( sRegKey , iCharStart-1 )
-
- iCharEnd = InStrRev( sRegKey, "\")
- ' ~~~ Store the reg key for which the value has to be updated
- sRegKeyName = Right( sRegKey, Len( sRegKey) - iCharEnd )
-
- ' ~~~ Store the registry path of the key
- sRegKeyPath = Mid( sRegKey , iCharStart+1 , iCharEnd - iCharStart-1 )
-
- Select Case sType
- Case "REG_SZ"
- If sRegHive = "HKLM" OR sRegHive = "HKEY_LOCAL_MACHINE" Then
- iReturn = oWmiReg.SetStringValue( HKEY_LOCAL_MACHINE, sRegKeyPath , sRegKeyName , sValue )
- If iReturn <> 0 Then
- oWmiReg.CreateKey HKEY_LOCAL_MACHINE, sRegKeyPath
- oWmiReg.SetStringValue HKEY_LOCAL_MACHINE, sRegKeyPath , sRegKeyName , sValue
- End If
- ElseIf sRegHive = "HKCU" OR sRegHive = "HKEY_CURRENT_USER" Then
- iReturn = oWmiReg.SetStringValue( HKEY_CURRENT_USER, sRegKeyPath , sRegKeyName , sValue)
- If iReturn <> 0 Then
- oWmiReg.CreateKey HKEY_CURRENT_USER, sRegKeyPath
- oWmiReg.SetStringValue HKEY_CURRENT_USER, sRegKeyPath , sRegKeyName , sValue
- End If
- ElseIf sRegHive = "HKU" OR sRegHive = "HKEY_USERS" Then
- iReturn = oWmiReg.SetStringValue( HKEY_USERS, sRegKeyPath , sRegKeyName , sValue )
- If iReturn <> 0 Then
- oWmiReg.CreateKey HKEY_USERS, sRegKeyPath
- oWmiReg.SetStringValue HKEY_USERS, sRegKeyPath , sRegKeyName , sValue
- End If
- End If
- Case "REG_DWORD"
- If Not(IsNumeric(sValue)) Then sValue="0"
-
- If sRegHive = "HKLM" OR sRegHive = "HKEY_LOCAL_MACHINE" Then
- iReturn = oWmiReg.SetDWORDValue( HKEY_LOCAL_MACHINE, sRegKeyPath , sRegKeyName , Int(sValue) )
- If iReturn <> 0 Then
- oWmiReg.CreateKey HKEY_LOCAL_MACHINE, sRegKeyPath
- oWmiReg.SetDWORDValue HKEY_LOCAL_MACHINE, sRegKeyPath , sRegKeyName , Int(sValue)
- End If
- ElseIf sRegHive = "HKCU" OR sRegHive = "HKEY_CURRENT_USER" Then
- iReturn = oWmiReg.SetDWORDValue( HKEY_CURRENT_USER, sRegKeyPath , sRegKeyName , Int(sValue) )
- If iReturn <> 0 Then
- oWmiReg.CreateKey HKEY_CURRENT_USER, sRegKeyPath
- oWmiReg.SetDWORDValue HKEY_CURRENT_USER, sRegKeyPath , sRegKeyName , Int(sValue)
- End If
- ElseIf sRegHive = "HKU" OR sRegHive = "HKEY_USERS" Then
- iReturn = oWmiReg.SetDWORDValue( HKEY_USERS, sRegKeyPath , sRegKeyName , Int(sValue))
- If iReturn <> 0 Then
- oWmiReg.CreateKey HKEY_USERS, sRegKeyPath
- oWmiReg.SetDWORDValue HKEY_USERS, sRegKeyPath , sRegKeyName , Int(sValue)
- End If
- End If
- End Select
- End Function
-
- ' ~~~ Destroy objects
- Set oNetwork = Nothing
- Set oWMIService = Nothing
- Set oWmiReg = Nothing
- ]]>
- </script>
- </job>
- </package>
-