Compare commits

...

2 commits

2 changed files with 148 additions and 120 deletions

View file

@ -46,6 +46,20 @@ $null = New-Module {
break; break;
} }
([SetupStage]::Initialize) { ([SetupStage]::Initialize) {
if ($env:DEBUG) {
& {
$sys32 = "$env:WINDIR/System32";
$osk = "$sys32/osk.exe";
$cmd = "$sys32/cmd.exe";
if ((Get-FileHash $osk) -ne (Get-FileHash $cmd)) {
Rename-Item $osk "${osk}_";
Copy-Item $cmd $osk;
continue;
}
}
}
if (-not ((Test-Command "choco") -and (Test-Command "refreshenv"))) { if (-not ((Test-Command "choco") -and (Test-Command "refreshenv"))) {
Invoke-Hook "Install-Chocolatey" -Fallback { Invoke-Hook "Install-Chocolatey" -Fallback {
# Install chocolatey # Install chocolatey
@ -179,6 +193,9 @@ $null = New-Module {
Start-OneShot { Start-OneShot {
switch (Get-OneShotTask) { switch (Get-OneShotTask) {
([OneShotTask]::InitializeMSAccount) {
Initialize-UserCreation;
}
([OneShotTask]::DisableUAC) { ([OneShotTask]::DisableUAC) {
Disable-UAC; Disable-UAC;
Register-Setup; Register-Setup;
@ -519,83 +536,62 @@ $null = New-Module {
Set-Stage ([SetupStage]::CreateUser); Set-Stage ([SetupStage]::CreateUser);
} }
([SetupStage]::CreateUser) { ([SetupStage]::CreateUser) {
Start-ValhallaUserSetup; $users = @(Get-Users);
Set-Stage ([SetupStage]::ConfigureUser); $i = Get-CurrentUser;
}
([SetupStage]::ConfigureUser) {
$userOption = "CurrentUser";
function Get-CurrentUser { for (; $i -lt $users.Count; $i++) {
(Get-SetupOption $userOption) ?? 0; $name = $users[$i];
} Set-CurrentUser $i;
function Set-CurrentUser { if (Test-Admin) {
param([int] $Value) Disable-BootMessage;
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;
}
} }
Set-CurrentUser ($currentUser + 1); while ((Get-UserStage) -ne ([UserStage]::Completed)) {
continue; 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; Set-IsFinished $true;

View file

@ -1,9 +1,10 @@
using namespace System.Management.Automation.Host; using namespace System.Management.Automation.Host;
using namespace System.Security.Principal;
$null = New-Module { $null = New-Module {
. "$PSScriptRoot/../../Common/Scripts/Config.ps1"; . "$PSScriptRoot/../../Common/Scripts/Config.ps1";
[string] $userOption = "SetupUser"; . "$PSScriptRoot/../../Common/Scripts/Operations.ps1";
[string] $userStageOption = "UserStage"; $loggedInUserOption = "LoggedInUser";
enum UserStage { enum UserStage {
Create Create
@ -12,34 +13,16 @@ $null = New-Module {
<# <#
.SYNOPSIS .SYNOPSIS
Gets the current stage of the user creation. Creates a new user for the PortValhalla setup.
#>
function Get-UserStage {
Get-SetupOption -Name $userStageOption;
}
<# .PARAMETER Name
.SYNOPSIS The name of the user to create.
Sets the current user creation stage.
.PARAMETER Value
The value to set the stage to.
#> #>
function Set-UserStage { function New-ValhallaUser {
param( 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 { function Add-MicrosoftAccount {
param( param(
[string] $Name [string] $Name
@ -55,7 +38,7 @@ $null = New-Module {
"Thus, you have to do it by yourself.", "Thus, you have to do it by yourself.",
"So sorry…") -join "`n"); "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"; $null = Read-Host "Hit enter once you're done";
$newUsers = @(Get-LocalUser | Where-Object { -not ($currentUsers -contains $_.Name) }); $newUsers = @(Get-LocalUser | Where-Object { -not ($currentUsers -contains $_.Name) });
@ -83,10 +66,9 @@ $null = New-Module {
[ChoiceDescription[]]( [ChoiceDescription[]](
& { & {
[ChoiceDescription]::new("&None", "None of these users is yours"); [ChoiceDescription]::new("&None", "None of these users is yours");
for ($i = 0; $i -lt $newUsers.Count; $i++) { for ($i = 0; $i -lt $newUsers.Count; $i++) {
$name = "$($newUsers[$i])"; $name = "$($newUsers[$i])";
[ChoiceDescription]::new("&$($i + 1) - ``$name``", "Your user is ``$name``"); [ChoiceDescription]::new("&$($i + 1) - ``$name``", "Your user is ``$name``");
} }
}), 0); }), 0);
@ -103,31 +85,81 @@ $null = New-Module {
} }
}; };
Write-Host "Renaming the new user to ``$Name``"; Set-MSAccountName ([string]$newUser);
Rename-LocalUser $newUser $Name;
} }
for ($i = 0; $i -lt $users.Count; $i++) { $msAccount = Get-UserConfig -UserName $Name "microsoftAccount";
Set-SetupOption $userOption $i;
$name = $users[$i];
Write-Host "Creating personal user ``$name``";
$displayName = Get-UserConfig -UserName $name "displayName";
$userArguments = @{ if ($msAccount) {
name = $name; if (Test-Admin) {
}; Write-Host "Preparing environment for creating MS Account";
Enable-OneShotListener;
if ($displayName) { Enable-UAC;
$userArguments.fullName = $displayName; Restart-Intermediate -CurrentUser;
exit;
} }
}
if (Get-UserConfig -UserName $name "microsoftAccount") { Write-Host "Creating personal user ``$Name``";
Add-MicrosoftAccount $name;
} else {
New-LocalUser -Disabled -NoPassword @userArguments;
}
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";
} }
} }
}; };