From 04c6ef125fa634944b08bea69ad828f3dd0f81ac Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Sat, 23 Mar 2024 15:47:31 +0100 Subject: [PATCH] Refactor user creation script --- scripts/Windows/OS/User/Add.ps1 | 91 ++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 41 deletions(-) diff --git a/scripts/Windows/OS/User/Add.ps1 b/scripts/Windows/OS/User/Add.ps1 index 95140cd7..115b60c8 100644 --- a/scripts/Windows/OS/User/Add.ps1 +++ b/scripts/Windows/OS/User/Add.ps1 @@ -9,6 +9,9 @@ $uacDisablerTriggerProperty = "UACDisablerTrigger"; function New-PersonalUser([Context] $context, [string] $userName) { $userStageProperty = "UserStage"; + $creationStage = "Create"; + $disableUACStage = "DisableUAC"; + $adminRemovalStage = "RemoveAdmin"; $null = New-Module { Get-UserStage { @@ -21,61 +24,67 @@ function New-PersonalUser([Context] $context, [string] $userName) } } - if (-not (Get-LocalUser $userName -ErrorAction SilentlyContinue)) - { - Write-Host "Creating Personal User"; + switch (Get-UserStage) { + { $_ -in $null,$creationStage } { + Set-UserStage $creationStage; - while ($true) { - Write-Host ( - [string]::Join( - "`n", - "So... Windows is too dumb to create users which are bound to a Microsoft Account.", - "Thus, you have to do it by yourself.", - "So sorry...")); + if (-not (Get-LocalUser $userName -ErrorAction SilentlyContinue)) { + Write-Host "Creating Personal User ``$userName``"; - $users = Get-LocalUser | ForEach-Object { $_.Name }; - Write-Host "Following users exist already:" - Write-Host $users; - Read-Host "Please hit enter once you're done..."; + while ($true) { + Write-Host ( + [string]::Join( + "`n", + "So... Windows is too dumb to create users which are bound to a Microsoft Account.", + "Thus, you have to do it by yourself.", + "So sorry...")); - $user = Get-LocalUser | Where-Object { -not ($users -contains $_.Name) } | Select-Object -Last 1; + $users = Get-LocalUser | ForEach-Object { $_.Name }; + Write-Host "Following users exist already:" + Write-Host $users; + Read-Host "Please hit enter once you're done..."; - if ($user) { - Write-Information "Found New User:"; - Write-Information $user; - break; + $user = Get-LocalUser | Where-Object { -not ($users -contains $_.Name) } | Select-Object -Last 1; + + if ($user) { + Write-Information "Found New User:"; + Write-Information $user; + break; + } + } + + 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"; + $context.RegisterNewUserReboot(); + + Write-Information "Enabling UAC for the next login (Microsoft Account login won't work otherwise, lol)"; + Enable-UACNextLogin $context; + + Write-Information "Disabling Auto login"; + $context.RemoveAutologin(); + Set-UserStage $disableUACStage; + Restart-Computer -Force; + exit; } } - - 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"; - $context.RegisterNewUserReboot(); - - Write-Information "Enabling UAC for the next login (Microsoft Account login won't work otherwise, lol)"; - Enable-UACNextLogin $context; - - Write-Information "Disabling Auto login"; - $context.RemoveAutologin(); - Set-UserStage "DisableUAC"; - Restart-Computer -Force; - exit; - } - - switch (Get-UserStage) { - "DisableUAC" { + $disableUACStage { Enable-PersonalUserAutologon $context $userName; $context.RegisterReboot(); - Set-UserStage "RemoveAdmin"; + Set-UserStage $adminRemovalStage; Start-EventDrivenTask $context.Get($uacDisablerTriggerProperty); exit; } - "RemoveAdmin" { + $adminRemovalStage { Write-Information "Removing Admin Account"; Get-CimInstance -ClassName "Win32_UserProfile" -Filter "SID = '$((Get-LocalUser $context.AdminName).SID)'" | Remove-CimInstance; $context.Remove($userStageProperty); + break; } } }