Allow creation of multiple users

This commit is contained in:
Manuel Thalmann 2024-03-23 15:38:06 +01:00
parent a49a926135
commit be49a7d80c
4 changed files with 41 additions and 12 deletions

View file

@ -3,13 +3,13 @@
. "$PSScriptRoot/../Software/Firefox/Install.ps1";
. "$PSScriptRoot/Manage.ps1";
. "$PSScriptRoot/Upgrade.ps1";
. "$PSScriptRoot/User/Add.ps1";
. "$PSScriptRoot/User/Install.ps1";
function Invoke-WindowsInstallation([Context] $context)
{
$ErrorActionPreference = "Inquire";
. "$PSScriptRoot/../../../scripts/Windows/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;
}

View file

@ -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);
}

View file

@ -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);
}
}

View file

@ -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";