diff --git a/scripts/Windows/OS/Install.ps1 b/scripts/Windows/OS/Install.ps1 index 30a00e1a..a5420e46 100644 --- a/scripts/Windows/OS/Install.ps1 +++ b/scripts/Windows/OS/Install.ps1 @@ -193,6 +193,9 @@ $null = New-Module { Start-OneShot { switch (Get-OneShotTask) { + ([OneShotTask]::InitializeMSAccount) { + Initialize-UserCreation; + } ([OneShotTask]::DisableUAC) { Disable-UAC; Register-Setup; @@ -533,83 +536,62 @@ $null = New-Module { Set-Stage ([SetupStage]::CreateUser); } ([SetupStage]::CreateUser) { - Start-ValhallaUserSetup; - Set-Stage ([SetupStage]::ConfigureUser); - } - ([SetupStage]::ConfigureUser) { - $userOption = "CurrentUser"; + $users = @(Get-Users); + $i = Get-CurrentUser; - function Get-CurrentUser { - (Get-SetupOption $userOption) ?? 0; - } + for (; $i -lt $users.Count; $i++) { + $name = $users[$i]; + Set-CurrentUser $i; - function Set-CurrentUser { - param([int] $Value) - Set-SetupOption $userOption $Value; - } - - [string[]] $users = Get-Users; - $currentUser = Get-CurrentUser; - - if (Test-Admin) { - Disable-BootMessage; - } - - if ($currentUser -lt $users.Count) { - $user = Get-LocalUser $users[$currentUser]; - $msAccount = Get-UserConfig -UserName "$user" -Name "microsoftAccount"; - - $adminGroup = @{ - SID = [SecurityIdentifier]::new([WellKnownSidType]::BuiltinAdministratorsSid, $null); - }; - - Add-LocalGroupMember ` - @adminGroup ` - $user ` - -ErrorAction SilentlyContinue; - - if ($env:UserName -ne "$user") { - Disable-LocalUser $env:UserName; - Enable-LocalUser $user; - - if ($msAccount) { - Enable-UAC; - Disable-Autologin; - Enable-OneShotListener; - Set-BootMessage -Caption "Please Log In" -Message "Please log in using your new Microsoft Account ``$user``."; - } else { - Set-AutologinUser "$user"; - } - - Restart-Intermediate -DefaultUser; - return; - } else { - $configure = { - Deploy-SoftwareAction -Action ([InstallerAction]::ConfigureUser); - Remove-LocalGroupMember -Member "$user" @adminGroup -ErrorAction SilentlyContinue; - - foreach ($group in Get-UserConfig "groups") { - Add-LocalGroupMember -Member "$user" -Name "$group"; - } - } - - if ($msAccount) { - if (-not (Test-Admin)) { - Invoke-OneShot DisableUAC; - Restart-Computer; - return; - } else { - & $configure; - Clear-SetupRegistration; - Disable-OneShotListener; - } - } else { - & $configure; - } + if (Test-Admin) { + Disable-BootMessage; } - Set-CurrentUser ($currentUser + 1); - continue; + while ((Get-UserStage) -ne ([UserStage]::Completed)) { + switch (Get-UserStage) { + ($null) { + Set-UserStage ([UserStage]::Create); + continue; + } + ([UserStage]::Create) { + $msAccount = Get-UserConfig -UserName $name "microsoftAccount"; + + if ($env:UserName -ne $name) { + New-ValhallaUser $name; + + if ($msAccount) { + Register-Setup -DefaultUser; + logoff; + } else { + Restart-Intermediate; + } + + exit; + } else { + if ($msAccount) { + if (-not (Test-Admin)) { + Invoke-OneShot DisableUAC; + Restart-Computer; + return; + } + + Clear-SetupRegistration; + Disable-OneShotListener; + } + + Set-UserStage ([UserStage]::Configure); + } + } + (([UserStage]::Configure)) { + Deploy-SoftwareAction -Action ([InstallerAction]::ConfigureUser); + Remove-LocalGroupMember -Member "$user" @adminGroup -ErrorAction SilentlyContinue; + + foreach ($group in Get-UserConfig "groups") { + Add-LocalGroupMember -Member "$user" -Name "$group"; + } + } + } + } } Set-IsFinished $true; diff --git a/scripts/Windows/Scripts/Users.ps1 b/scripts/Windows/Scripts/Users.ps1 index 45fa4319..f2c478f3 100644 --- a/scripts/Windows/Scripts/Users.ps1 +++ b/scripts/Windows/Scripts/Users.ps1 @@ -1,9 +1,10 @@ using namespace System.Management.Automation.Host; +using namespace System.Security.Principal; $null = New-Module { . "$PSScriptRoot/../../Common/Scripts/Config.ps1"; - [string] $userOption = "SetupUser"; - [string] $userStageOption = "UserStage"; + . "$PSScriptRoot/../../Common/Scripts/Operations.ps1"; + $loggedInUserOption = "LoggedInUser"; enum UserStage { Create @@ -12,34 +13,16 @@ $null = New-Module { <# .SYNOPSIS - Gets the current stage of the user creation. - #> - function Get-UserStage { - Get-SetupOption -Name $userStageOption; - } + Creates a new user for the PortValhalla setup. - <# - .SYNOPSIS - Sets the current user creation stage. - - .PARAMETER Value - The value to set the stage to. + .PARAMETER Name + The name of the user to create. #> - function Set-UserStage { + function New-ValhallaUser { param( - [UserStage] $Value + [string] $Name ) - Set-SetupOption -Name $userStageOption -Value $Value; - } - - <# - .SYNOPSIS - Creates the configured users. - #> - function Start-ValhallaUserSetup { - [string[]] $users = Get-Users; - function Add-MicrosoftAccount { param( [string] $Name @@ -55,7 +38,7 @@ $null = New-Module { "Thus, you have to do it by yourself.", "So sorry…") -join "`n"); - Write-Host "Create a user for ``$Name`` manually (because Windows is too stupid)…"; + Write-Host "Create a user for ``$Name`` manually… (because Windows is too stupid)"; $null = Read-Host "Hit enter once you're done"; $newUsers = @(Get-LocalUser | Where-Object { -not ($currentUsers -contains $_.Name) }); @@ -83,10 +66,9 @@ $null = New-Module { [ChoiceDescription[]]( & { [ChoiceDescription]::new("&None", "None of these users is yours"); - + for ($i = 0; $i -lt $newUsers.Count; $i++) { $name = "$($newUsers[$i])"; - [ChoiceDescription]::new("&$($i + 1) - ``$name``", "Your user is ``$name``"); } }), 0); @@ -103,31 +85,81 @@ $null = New-Module { } }; - Write-Host "Renaming the new user to ``$Name``…"; - Rename-LocalUser $newUser $Name; + Set-MSAccountName ([string]$newUser); } - for ($i = 0; $i -lt $users.Count; $i++) { - Set-SetupOption $userOption $i; - $name = $users[$i]; - Write-Host "Creating personal user ``$name``…"; - $displayName = Get-UserConfig -UserName $name "displayName"; + $msAccount = Get-UserConfig -UserName $Name "microsoftAccount"; - $userArguments = @{ - name = $name; - }; - - if ($displayName) { - $userArguments.fullName = $displayName; + if ($msAccount) { + if (Test-Admin) { + Write-Host "Preparing environment for creating MS Account"; + Enable-OneShotListener; + Enable-UAC; + Restart-Intermediate -CurrentUser; + exit; } + } - if (Get-UserConfig -UserName $name "microsoftAccount") { - Add-MicrosoftAccount $name; - } else { - New-LocalUser -Disabled -NoPassword @userArguments; - } + Write-Host "Creating personal user ``$Name``…"; - Set-LocalUser @userArguments; + if ($msAccount) { + Add-MicrosoftAccount $Name; + Set-SetupOption $loggedInUserOption $env:UserName; + Invoke-OneShot ([OneShotTask]::InitializeMSAccount) + } else { + New-LocalUser -NoPassword @userArguments; + Initialize-UserCreation; + } + } + + <# + .SYNOPSIS + Prepares the first login for initializing the current user under configuration. + #> + function Initialize-UserCreation { + $name = (@(Get-Users))[(Get-CurrentUser)]; + $msAccount = Get-UserConfig -UserName $name "microsoftAccount"; + $displayName = Get-UserConfig -UserName $Name "displayName"; + + Write-Host "Initializing user $name"; + Write-Host "MS Account: $msAccount" + + $userArguments = @{ + name = $name; + }; + + if ($displayName) { + $userArguments.fullName = $displayName; + } + + $adminGroup = @{ + SID = [SecurityIdentifier]::new([WellKnownSidType]::BuiltinAdministratorsSid, $null); + } + + if ($msAccount) { + Write-Host "Renaming $(Get-MSAccountName) to $name" + Rename-LocalUser (Get-MSAccountName) $name; + } + + Set-LocalUser @userArguments; + + if ($msAccount) { + Disable-LocalUser (Get-SetupOption $loggedInUserOption); + } else { + Disable-LocalUser $env:UserName; + } + + Add-LocalGroupMember ` + @adminGroup ` + $name ` + -ErrorAction SilentlyContinue; + + if ($msAccount) { + Disable-Autologin; + Set-BootMessage -Caption "Please Log In" -Message "Please log in using your new Microsoft Account ``$name``."; + Write-Host "MS Account successfully initialized"; + } else { + Set-AutologinUser "$name"; } } };