TUTOS.EU

Programmer un autologon en Vbs et supprimer automatiquement le mot de passe au prochain logon

En VbScript, comment programmer un autologon pour que la prochaine connexion se fasse automatiquement, poser un runonce pour supprimer le mot de passe et programmer le lancement d'une commande

Option Explicit

'VbScript for schedule a reboot with an autotologon for one time
'Just provide the local admin password in the -localpassword parameter
'Example :
'VbsPrepareAutologonForNextReboot.vbs -localpassword mypassword

Dim strComputer, objWMIService, colAccounts, objAccount
Dim LocalAdminAccountName, varLocalPassword, varLogonCommand, varReboot
Dim CheminScriptActuel
Dim WSHShell, Commande

call ParseCommand() 'Parse the command line.

CheminScriptActuel = Left(wscript.scriptfullname,Len(wscript.scriptfullname)-Len(wscript.scriptname)-1)


strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colAccounts = objWMIService.ExecQuery ("Select * From Win32_UserAccount Where LocalAccount = TRUE")
LocalAdminAccountName = ""
For Each objAccount in colAccounts
	If Left (objAccount.SID, 6) = "S-1-5-" and Right(objAccount.SID, 4) = "-500" Then
		LocalAdminAccountName = objAccount.Name
		Exit for
	End If
Next


Set WSHShell = CreateObject("WScript.Shell")
Commande = "REG ADD ""HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v DefaultDomainName /t REG_SZ /d %COMPUTERNAME% /f"
Wscript.echo "Passage de la commande " & Commande
WSHShell.Run Commande

Commande = "REG ADD ""HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v DefaultUserName /t REG_SZ /d """ & LocalAdminAccountName & """ /f"
Wscript.echo "Passage de la commande " & Commande
WSHShell.Run Commande

Commande = "REG ADD ""HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v DefaultPassword /t REG_SZ /d """ & varLocalPassword & """ /f"
Wscript.echo "Passage de la commande pour paramétrage du mot de passe" & Commande
WSHShell.Run Commande

Commande = "REG ADD ""HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v AutoAdminLogon /t REG_SZ /d 1 /f"
Wscript.echo "Passage de la commande " & Commande
WSHShell.Run Commande

'On programme une commande Reg Delete pour le prochain logon afin de supprimer le mot de passe stocké dans la base de registre.
'Pour cela on passe par la commande Reg Add.
'Donc via Reg Add on programme un Reg Delete
Commande = "REG ADD ""HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce"" /v StopAutoLogon /t REG_SZ /d ""REG DELETE \""HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\"" /v DefaultPassword /f"" /f"
'REG DELETE ""HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v DefaultPassword /f
Wscript.echo "Passage de la commande pour arreter l auto logon" & Commande
WSHShell.Run Commande

If Len(varLogonCommand) > 0 Then
	varLogonCommand = Replace(varLogonCommand,"""", """""") 'On double les quotes afin de les échappers
	'Commande = "REG DELETE ""HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v DefaultPassword /f"
	Commande = "REG ADD ""HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce"" /v LogonCommand /t REG_SZ /d """ & varLogonCommand & """ /f"
	'REG DELETE ""HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon"" /v DefaultPassword /f
	Wscript.echo "Programmation de la commande à exécuter au prochain logon"
	WSHShell.Run Commande
End If

If cstr(varReboot) = "1" Then
	Commande = "Shutdown.exe -r -f -t 1 -d p:2:4"
	Wscript.echo "Passage de la commande " & Commande
	WSHShell.Run Commande
End If

Set WSHShell = Nothing

Function ParseCommand()
	'
	' Parses the command line and fills the script variables 
	' with the appropriate values.
	'
	Dim ArgCount
	Dim objArgs

	Set objArgs = Wscript.Arguments

	ArgCount = 0
	if objArgs.Count = 0 then
		wscript.echo "No arguments specified."
		wscript.echo
		call Help()
	end if

	'Wscript.echo "Nbr args : " & objArgs.Count
	While ArgCount < objArgs.Count
		'Wscript.echo "Argument " & ArgCount & " : " & objArgs(ArgCount)
		Select Case LCase(objArgs(ArgCount))
			Case "-localpassword"
			ArgCount = ArgCount + 1
			varLocalPassword = objArgs(ArgCount)
			wscript.echo "Local password : " & varLocalPassword
			
			Case "-logoncommand"
			ArgCount = ArgCount + 1
			varLogonCommand = objArgs(ArgCount)
			wscript.echo "Logon command : " & varLogonCommand

			Case "-reboot"
			ArgCount = ArgCount + 1
			varReboot = objArgs(ArgCount)
			wscript.echo "Reboot machine : " & varReboot
			
			Case Else:
			wscript.echo "Invalid command."
			wscript.echo
			call Help()
			wscript.quit
		End Select
		ArgCount = ArgCount + 1
	Wend
End Function

sub Help()
	'
	' Display command-line syntax for the script.
	'
	wscript.echo "Script Function details"
	wscript.echo "Syntax:"
	wscript.echo
	wscript.echo "-localpassword ""Password of local admin account"""
	wscript.echo "-LogonCommand ""Command to execute after next reboot"""
	wscript.echo "-reboot ""set to 1 if you want to reboot the machine"""
	wscript.echo
	wscript.echo "Example"
	wscript.echo
	wscript.echo "VbsPrepareAutologonForNextReboot.vbs -localpassword ""MyLocalAdminPassword"" -logoncommand ""dir*.*"" -reboot 1"
	wscript.quit
End Sub
Lien vers le fichier : cliquez ici Copier le code

Article(s) précédent(s)

Article(s) en relation(s)

2