Refactor user creation script

This commit is contained in:
Manuel Thalmann 2024-03-23 15:47:31 +01:00
parent 0246633c68
commit 04c6ef125f

View file

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