Streamline the creation of users

This commit is contained in:
Manuel Thalmann 2024-08-21 18:34:18 +02:00
parent 1c458f8004
commit 596bca0b4e
3 changed files with 227 additions and 98 deletions
scripts/Windows/Scripts

View file

@ -1,15 +1,23 @@
using namespace System.Management.Automation.Host;
using namespace System.Security.Principal;
$null = New-Module {
. "$PSScriptRoot/../../Common/Scripts/Config.ps1";
[string] $userOption = "SetupUser";
. "$PSScriptRoot/../../Common/Scripts/Operations.ps1";
. "$PSScriptRoot/../../Common/Types/OneShotTask.ps1";
$loggedInUserOption = "LoggedInUser";
<#
.SYNOPSIS
Creates the configured users.
Creates a new user for the PortValhalla setup.
.PARAMETER Name
The name of the user to create.
#>
function Start-ValhallaUserSetup {
[string[]] $users = Get-Users;
function New-ValhallaUser {
param(
[string] $Name
)
function Add-MicrosoftAccount {
param(
@ -26,7 +34,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) });
@ -57,7 +65,6 @@ $null = New-Module {
for ($i = 0; $i -lt $newUsers.Count; $i++) {
$name = "$($newUsers[$i])";
[ChoiceDescription]::new("&$($i + 1) - ``$name``", "Your user is ``$name``");
}
}), 0);
@ -74,33 +81,82 @@ $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;
Set-LocalUser $name -PasswordNeverExpires $true;
Set-LocalUser $name -PasswordNeverExpires $false;
}
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 $Name;
Set-LocalUser $Name -PasswordNeverExpires $true;
Set-LocalUser $Name -PasswordNeverExpires $false;
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``";
$userArguments = @{
name = $name;
};
if ($displayName) {
$userArguments.fullName = $displayName;
}
$adminGroup = @{
SID = [SecurityIdentifier]::new([WellKnownSidType]::BuiltinAdministratorsSid, $null);
};
if ($msAccount) {
$accountName = Get-MSAccountName;
Write-Host "Renaming ``$accountName`` to ``$name``"
Rename-LocalUser $accountName $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``.";
} else {
Set-AutologinUser "$name";
}
}
};