From 0246633c689016f8cb122a9bfb66a034e1828944 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 23 Mar 2024 15:38:06 +0100 Subject: [PATCH] Allow creation of multiple users --- scripts/Windows/OS/Install.ps1 | 6 +++--- scripts/Windows/OS/User/Add.ps1 | 16 ++++++++-------- scripts/Windows/OS/User/Install.ps1 | 29 +++++++++++++++++++++++++++++ scripts/Windows/Scripts/Context.ps1 | 2 +- 4 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 scripts/Windows/OS/User/Install.ps1 diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1 index dd3fd0e5..15934cf9 100644 --- a/scripts/Windows/OS/Install.ps1 +++ b/scripts/Windows/OS/Install.ps1 @@ -1,7 +1,7 @@ #!/bin/pwsh . "$PSScriptRoot/Manage.ps1"; . "$PSScriptRoot/Upgrade.ps1"; -. "$PSScriptRoot/User/Add.ps1"; +. "$PSScriptRoot/User/Install.ps1"; . "$PSScriptRoot/../Scripts/Context.ps1"; . "$PSScriptRoot/../Software/Firefox/Install.ps1"; @@ -9,7 +9,7 @@ function Invoke-WindowsInstallation([Context] $context) { $ErrorActionPreference = "Inquire"; . "$PSScriptRoot/../Scripts/Prerequisites.ps1"; - $context.UserName ??= "Manuel"; + $context.UserNames ??= @("Manuel"); Start-WindowsInstallation $context; } @@ -96,7 +96,7 @@ function Start-WindowsInstallation([Context] $context) { break; } $userStage { - New-PersonalUser $context; + Install-PersonalUsers $context; $context.SetStage($restorationStage); break; } diff --git a/scripts/Windows/OS/User/Add.ps1 b/scripts/Windows/OS/User/Add.ps1 index 8f71dfc7..95140cd7 100644 --- a/scripts/Windows/OS/User/Add.ps1 +++ b/scripts/Windows/OS/User/Add.ps1 @@ -6,7 +6,7 @@ $preparedPasswordProperty = "AutoLoginPassword"; $autoLoginTriggerProperty = "AutoLoginTrigger"; $uacDisablerTriggerProperty = "UACDisablerTrigger"; -function New-PersonalUser([Context] $context) +function New-PersonalUser([Context] $context, [string] $userName) { $userStageProperty = "UserStage"; @@ -21,7 +21,7 @@ function New-PersonalUser([Context] $context) } } - if (-not (Get-LocalUser $context.UserName -ErrorAction SilentlyContinue)) + if (-not (Get-LocalUser $userName -ErrorAction SilentlyContinue)) { Write-Host "Creating Personal User"; @@ -47,8 +47,8 @@ function New-PersonalUser([Context] $context) } } - Write-Information "Renaming the new User to $($context.UserName)"; - Rename-LocalUser $user $context.UserName; + Write-Information "Renaming the new User to $userName"; + Rename-LocalUser $user $userName; Add-LocalGroupMember -Group "Administrators" -Member $user && Set-LocalUser $context.AdminName -Password (ConvertTo-SecureString -AsPlainText "Admin") && Disable-LocalUser $context.AdminName; Write-Host "Registering setup script for all new users"; @@ -66,7 +66,7 @@ function New-PersonalUser([Context] $context) switch (Get-UserStage) { "DisableUAC" { - Enable-PersonalUserAutologon $context; + Enable-PersonalUserAutologon $context $userName; $context.RegisterReboot(); Set-UserStage "RemoveAdmin"; Start-EventDrivenTask $context.Get($uacDisablerTriggerProperty); @@ -123,7 +123,7 @@ function Enable-UACNextLogin([Context] $context) { } } -function Enable-PersonalUserAutologon([Context] $context) +function Enable-PersonalUserAutologon([Context] $context, [string] $userName) { Add-Type -assemblyname System.DirectoryServices.AccountManagement; Write-Information "Re-Enabling Autologin for Current User"; @@ -133,7 +133,7 @@ function Enable-PersonalUserAutologon([Context] $context) { $password = Read-Host "Please enter the password of your Microsoft Account" -MaskInput; - if ($principalContext.ValidateCredentials($context.UserName, $password)) + if ($principalContext.ValidateCredentials($userName, $password)) { break; } @@ -142,7 +142,7 @@ function Enable-PersonalUserAutologon([Context] $context) } } - $context.Set($preparedUsernameProperty, $context.UserName, "ExpandString"); + $context.Set($preparedUsernameProperty, $userName, "ExpandString"); $context.Set($preparedPasswordProperty, $password, "ExpandString"); Start-EventDrivenTask $context.Get($autoLoginTriggerProperty); } diff --git a/scripts/Windows/OS/User/Install.ps1 b/scripts/Windows/OS/User/Install.ps1 new file mode 100644 index 00000000..4ff19b9a --- /dev/null +++ b/scripts/Windows/OS/User/Install.ps1 @@ -0,0 +1,29 @@ +#!/bin/pwsh +. "$PSScriptRoot/../../Scripts/Context.ps1"; +. "$PSScriptRoot/Add.ps1"; + +function Install-PersonalUsers([Context] $context) { + $userIDProperty = "User"; + + $null = New-Module { + Get-UserID { + return $context.Get($userIDProperty); + } + Set-UserID { + param([int]$value); + $context.Set($userIDProperty, $value); + } + } + + $userID = Get-UserID; + + if ($null -eq $userID) { + Set-UserID ($context.UserNames.Count - 1); + $userID = Get-UserID; + } + + if ($userID -ge 0) { + New-PersonalUser $context $($context.UserNames[$userID]); + Set-UserID ($userID - 1); + } +} diff --git a/scripts/Windows/Scripts/Context.ps1 b/scripts/Windows/Scripts/Context.ps1 index b2783b77..562e62e0 100644 --- a/scripts/Windows/Scripts/Context.ps1 +++ b/scripts/Windows/Scripts/Context.ps1 @@ -6,7 +6,7 @@ class Context { [string]$EntryPoint; [string]$RootDir; [string]$BackupName; - [string]$UserName; + [string[]]$UserNames; [string]$AdminName = "Admin"; [string]$ConfigRoot = "HKLM:\Software\PortValhalla"; [string]$RunOnceName = "PortValhalla";