From a92e4136236d85bea0931ba69d8732e36abec7c2 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sun, 25 Jun 2023 18:25:52 +0200 Subject: [PATCH] Add scripts for handling auto login --- scripts/Windows/Scripts/Context.ps1 | 33 +++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/scripts/Windows/Scripts/Context.ps1 b/scripts/Windows/Scripts/Context.ps1 index 1b8b7a00..3ed8e1ed 100644 --- a/scripts/Windows/Scripts/Context.ps1 +++ b/scripts/Windows/Scripts/Context.ps1 @@ -73,6 +73,11 @@ class Context { reg unload $regRoot; } + [void] ProcessLogonKey([System.Action[Microsoft.Win32.RegistryKey]] $action) { + $key = Get-Item "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\Winlogon"; + $action.Invoke($key); + } + [Microsoft.Win32.RegistryKey] GetRunOnceKey() { return $this.GetRunOnceKey($null); } @@ -110,6 +115,34 @@ class Context { $this.ProcessDefaultUserKey({ param ($root) Remove-Item -Path $this.GetRunOnceKey($root).PSPath }); } + [void] SetAutologin($user, $pw) { + $this.ProcessLogonKey( + { + param ($logon) + $path = $logon.PSPath; + Set-ItemProperty $path -Name "AutoAdminLogon" 1; + Set-ItemProperty $path -Name "DefaultUserName" $user; + + if ($password) { + Set-ItemProperty $path -Name "DefaultPassword" $password; + } else { + Remove-ItemProperty $path -Name "DefaultPassword"; + } + }); + } + + [void] RemoveAutologin() { + $this.ProcessLogonKey( + { + param ($logon) + $path = $logon.PSPath; + Remove-ItemProperty $path -Name "AutoAdminLogon"; + Remove-ItemProperty $path -Name "DefaultDomainName"; + Remove-ItemProperty $path -Name "DefaultUserName"; + Remove-ItemProperty $path -Name "DefaultPassword"; + }); + } + [void] Reboot() { Write-Host "Restarting Computer..."; $this.RegisterReboot();