From bcb2845cd4b46ff91ad9a24c923be69274b137d5 Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Tue, 27 Aug 2024 17:04:17 +0200 Subject: [PATCH] Create dedicated script for creating users --- scripts/Windows/OS/Install.ps1 | 97 +--------------------------- scripts/Windows/Scripts/Users.ps1 | 103 ++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 96 deletions(-) diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1 index 244157ec..4702595a 100644 --- a/scripts/Windows/OS/Install.ps1 +++ b/scripts/Windows/OS/Install.ps1 @@ -91,102 +91,7 @@ $null = New-Module { Set-Stage ([SetupStage]::CreateUser); } ([SetupStage]::CreateUser) { - $users = @(Get-Users); - $i = Get-CurrentUser; - - for (; $i -lt $users.Count; $i++) { - $name = $users[$i]; - $msAccount = Get-UserConfig -UserName $name "microsoftAccount"; - Set-CurrentUser $i; - - if (Test-Admin) { - Disable-BootMessage; - } - - while ((Get-UserStage) -ne ([UserStage]::Completed)) { - switch (Get-UserStage) { - ($null) { - Set-UserStage ([UserStage]::Create); - continue; - } - ([UserStage]::Create) { - if ($env:UserName -ne $name) { - $userInfo = @{ - name = $name; - msAccount = $msAccount; - }; - - New-ValhallaUser @userInfo; - - if ($msAccount) { - logoff; - } else { - Restart-Intermediate; - } - - exit; - } else { - if ($msAccount) { - if (-not (Test-Admin)) { - Invoke-OneShot DisableUAC; - Restart-Intermediate -NoRegister; - return; - } - - Clear-SetupRegistration; - Disable-OneShotListener; - } - - Set-UserStage ([UserStage]::Configure); - } - } - (([UserStage]::Configure)) { - $displayName = Get-UserConfig -UserName $name "displayName"; - - $userArguments = @{ - name = $name; - }; - - if ($displayName) { - $userArguments.fullName = $displayName; - } - - $adminGroup = @{ - SID = [SecurityIdentifier]::new([WellKnownSidType]::BuiltinAdministratorsSid, $null); - }; - - Set-LocalUser @userArguments; - Deploy-SoftwareAction -Action ([InstallerAction]::ConfigureUser); - Remove-LocalGroupMember -Member "$name" @adminGroup -ErrorAction SilentlyContinue; - - foreach ($group in Get-UserConfig -UserName "$name" "groups") { - Add-LocalGroupMember -Member "$name" -Name "$group"; - } - - if (-not $msAccount) { - net user $name /logonpasswordchg:yes; - } - - Set-UserStage ([UserStage]::Cleanup); - } - ([UserStage]::Cleanup) { - $user = Get-SetupUser; - Disable-LocalUser $name; - Enable-LocalUser $user; - Set-AutologinUser $user; - Unregister-WslDistribution; - Set-UserStage ([UserStage]::Completed); - Restart-Intermediate; - exit; - } - } - } - } - - foreach ($user in $users) { - Enable-LocalUser $user; - } - + Install-ValhallaUsers; Set-IsFinished $true; } } diff --git a/scripts/Windows/Scripts/Users.ps1 b/scripts/Windows/Scripts/Users.ps1 index 0cf94e58..6d66257b 100644 --- a/scripts/Windows/Scripts/Users.ps1 +++ b/scripts/Windows/Scripts/Users.ps1 @@ -2,11 +2,114 @@ using namespace System.Management.Automation.Host; using namespace System.Security.Principal; $null = New-Module { + . "$PSScriptRoot/Deployment.ps1"; . "$PSScriptRoot/../../Common/Scripts/Config.ps1"; . "$PSScriptRoot/../../Common/Scripts/Operations.ps1"; . "$PSScriptRoot/../../Common/Types/OneShotTask.ps1"; $loggedInUserOption = "LoggedInUser"; + <# + .SYNOPSIS + Installs all pending users to the system. + #> + function Install-ValhallaUsers { + $users = @(Get-Users); + $i = Get-CurrentUser; + + for (; $i -lt $users.Count; $i++) { + Set-CurrentUser $i; + $name = $users[$i]; + $msAccount = Get-UserConfig -UserName $name "microsoftAccount"; + + if (Test-Admin) { + Disable-BootMessage; + } + + while ((Get-UserStage) -ne ([UserStage]::Completed)) { + switch (Get-UserStage) { + ($null) { + Set-UserStage ([UserStage]::Create); + break; + } + ([UserStage]::Create) { + if ($env:UserName -ne $name) { + $userInfo = @{ + name = $name; + msAccount = $msAccount; + }; + + New-ValhallaUser @userInfo; + + if ($msAccount) { + logoff; + } else { + Restart-Intermediate; + } + + exit; + } else { + if ($msAccount) { + if (-not (Test-Admin)) { + Invoke-OneShot DisableUAC; + Restart-Intermediate -NoRegister; + exit; + } + + Clear-SetupRegistration; + Disable-OneShotListener; + } + + Set-UserStage ([UserStage]::Configure); + } + } + ([UserStage]::Configure) { + $displayName = Get-UserConfig -UserName $name "displayName"; + + $userInfo = @{ + name = $name; + }; + + if ($displayName) { + $userInfo.fullName = $displayName; + } + + $adminGroup = @{ + SID = [SecurityIdentifier]::new([WellKnownSidType]::BuiltinAdministratorsSid, $null); + }; + + Set-LocalUser @userInfo; + Deploy-SoftwareAction -Action ConfigureUser; + Remove-LocalGroupMember -Member "$name" @adminGroup -ErrorAction SilentlyContinue; + + foreach ($group in Get-UserConfig -UserName "$name" "groups") { + Add-LocalGroupMember -Member "$name" -Name "$group"; + } + + if (-not $msAccount) { + net user $name /logonpasswordchg:yes; + } + + Set-UserStage ([UserStage]::Cleanup); + } + ([UserStage]::Cleanup) { + $user = Get-SetupUser; + Disable-LocalUser $name; + Enable-LocalUser $user; + Set-AutologinUser $user; + Unregister-WslDistribution; + Set-UserStage ([UserStage]::Completed); + Restart-Intermediate; + exit; + } + } + } + } + + foreach ($user in $users) { + Enable-LocalUser $user; + } + } + <# .SYNOPSIS Creates a new user for the PortValhalla setup.