2023-07-12 20:37:31 +00:00
|
|
|
#!/bin/pwsh
|
2024-03-22 09:32:53 +00:00
|
|
|
$contextScript = "$PSScriptRoot/../../Scripts/Context.ps1";
|
|
|
|
. "$contextScript";
|
2023-06-29 18:01:54 +00:00
|
|
|
$preparedUsernameProperty = "AutoLoginUser";
|
|
|
|
$preparedPasswordProperty = "AutoLoginPassword";
|
|
|
|
$autoLoginTriggerProperty = "AutoLoginTrigger";
|
2023-06-29 17:24:42 +00:00
|
|
|
$uacDisablerTriggerProperty = "UACDisablerTrigger";
|
2023-06-22 20:56:43 +00:00
|
|
|
|
2024-03-23 14:38:06 +00:00
|
|
|
function New-PersonalUser([Context] $context, [string] $userName)
|
2023-06-22 20:56:43 +00:00
|
|
|
{
|
2024-03-23 14:09:58 +00:00
|
|
|
$userStageProperty = "UserStage";
|
2024-03-23 14:47:31 +00:00
|
|
|
$creationStage = "Create";
|
2024-03-23 14:54:15 +00:00
|
|
|
$postProcessStage = "ReEnableFeatures";
|
2024-03-23 14:47:31 +00:00
|
|
|
$adminRemovalStage = "RemoveAdmin";
|
2024-03-23 14:09:58 +00:00
|
|
|
|
2024-03-24 01:06:16 +00:00
|
|
|
function Get-UserStage() {
|
|
|
|
return $context.Get($userStageProperty);
|
|
|
|
}
|
2024-03-23 14:09:58 +00:00
|
|
|
|
2024-03-24 01:06:16 +00:00
|
|
|
function Set-UserStage() {
|
|
|
|
param([string]$value);
|
|
|
|
$context.Set($userStageProperty, $value);
|
2024-03-23 14:09:58 +00:00
|
|
|
}
|
|
|
|
|
2024-03-23 14:47:31 +00:00
|
|
|
switch (Get-UserStage) {
|
2024-03-23 20:51:19 +00:00
|
|
|
{ (-not $_) -or ($_ -eq $creationStage) } {
|
2024-03-23 14:47:31 +00:00
|
|
|
Set-UserStage $creationStage;
|
|
|
|
|
|
|
|
if (-not (Get-LocalUser $userName -ErrorAction SilentlyContinue)) {
|
|
|
|
Write-Host "Creating Personal User ``$userName``";
|
|
|
|
|
2024-03-23 14:57:25 +00:00
|
|
|
Get-LocalUser | Where-Object { $_.Name -in $context.UserNames } |
|
|
|
|
Disable-LocalUser;
|
|
|
|
|
2024-03-23 14:47:31 +00:00
|
|
|
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..."));
|
|
|
|
|
|
|
|
$users = Get-LocalUser | ForEach-Object { $_.Name };
|
|
|
|
Write-Host "Following users exist already:"
|
|
|
|
Write-Host $users;
|
2024-03-24 01:10:45 +00:00
|
|
|
Write-Host "Create a user for ``$userName`` manually"
|
2024-03-23 14:47:31 +00:00
|
|
|
Read-Host "Please hit enter once you're done...";
|
|
|
|
|
|
|
|
$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-Information "Disabling Auto login";
|
|
|
|
$context.RemoveAutologin();
|
2024-03-23 14:54:09 +00:00
|
|
|
Write-Host "Registering setup script for all new users";
|
|
|
|
$context.RegisterNewUserReboot();
|
|
|
|
Register-UserPostprocessingTasks $context;
|
2024-03-23 14:54:15 +00:00
|
|
|
Set-UserStage $postProcessStage;
|
2024-03-23 14:47:31 +00:00
|
|
|
Restart-Computer -Force;
|
|
|
|
exit;
|
2023-06-22 21:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
2024-03-23 14:54:15 +00:00
|
|
|
$postProcessStage {
|
2024-03-23 14:38:06 +00:00
|
|
|
Enable-PersonalUserAutologon $context $userName;
|
2024-03-23 14:47:31 +00:00
|
|
|
Set-UserStage $adminRemovalStage;
|
2024-03-23 14:09:58 +00:00
|
|
|
Start-EventDrivenTask $context.Get($uacDisablerTriggerProperty);
|
2024-03-24 17:16:29 +00:00
|
|
|
$context.Reboot();
|
2024-03-23 14:09:58 +00:00
|
|
|
exit;
|
|
|
|
}
|
2024-03-23 14:47:31 +00:00
|
|
|
$adminRemovalStage {
|
2024-03-23 14:09:58 +00:00
|
|
|
Write-Information "Removing Admin Account";
|
|
|
|
Get-CimInstance -ClassName "Win32_UserProfile" -Filter "SID = '$((Get-LocalUser $context.AdminName).SID)'" | Remove-CimInstance;
|
|
|
|
$context.Remove($userStageProperty);
|
2024-03-23 14:47:31 +00:00
|
|
|
break;
|
2024-03-23 14:09:58 +00:00
|
|
|
}
|
2023-06-30 02:29:59 +00:00
|
|
|
}
|
2023-06-22 20:56:43 +00:00
|
|
|
}
|
2023-06-28 09:48:08 +00:00
|
|
|
|
2024-03-23 14:54:09 +00:00
|
|
|
function Register-UserPostprocessingTasks([Context] $context) {
|
|
|
|
Write-Information "Enabling UAC for the next login (Microsoft Account login won't work otherwise, lol)";
|
2023-07-03 11:44:51 +00:00
|
|
|
$context.SetUACState($true);
|
2023-06-29 02:05:12 +00:00
|
|
|
$tempTask = "PortValhalla Temp";
|
2023-06-30 09:57:50 +00:00
|
|
|
$autoLoginName = "PortValhalla AutoLogin Setup";
|
|
|
|
$uacDisablerName = "PortValhalla UAC Disabler";
|
2024-03-23 14:27:40 +00:00
|
|
|
$autoLoginTrigger = Get-Random -Maximum 0xFFFF;
|
|
|
|
$uacDisablerTrigger = Get-Random -Maximum 0xFFFF;
|
2023-06-29 17:24:42 +00:00
|
|
|
|
2023-06-29 18:01:54 +00:00
|
|
|
$context.Set($autoLoginTriggerProperty, $autoLoginTrigger, "DWord");
|
2023-06-29 17:24:42 +00:00
|
|
|
$context.Set($uacDisablerTriggerProperty, $uacDisablerTrigger, "DWord");
|
2023-06-28 09:48:08 +00:00
|
|
|
|
2024-03-23 14:54:09 +00:00
|
|
|
Write-Information "Registering tasks for re-enabling autologon and re-enabling UAC on next login";
|
|
|
|
|
2024-03-22 23:10:12 +00:00
|
|
|
$optionCollection = [System.Tuple[int, string, string[]][]]@(
|
2023-06-29 18:01:54 +00:00
|
|
|
[System.Tuple]::Create(
|
|
|
|
$autoLoginTrigger,
|
2023-06-30 09:57:50 +00:00
|
|
|
$autoLoginName,
|
2024-03-22 23:29:34 +00:00
|
|
|
@(
|
2024-03-23 00:27:49 +00:00
|
|
|
". `"$PSScriptRoot/AutoLogin.ps1`"",
|
|
|
|
" $autoLoginTrigger",
|
|
|
|
" '$preparedUsernameProperty'",
|
|
|
|
" '$preparedPasswordProperty'")),
|
2023-06-29 17:40:54 +00:00
|
|
|
[System.Tuple]::Create(
|
|
|
|
$uacDisablerTrigger,
|
2024-03-22 09:20:03 +00:00
|
|
|
$uacDisablerName,
|
2024-03-22 23:29:34 +00:00
|
|
|
@(
|
2024-03-23 00:27:49 +00:00
|
|
|
". `"$PSScriptRoot/UACDisabler.ps1`"",
|
|
|
|
" $uacDisablerTrigger",
|
|
|
|
" '$autoLoginName'",
|
|
|
|
" '$uacDisablerName'",
|
|
|
|
" '$autoLoginTriggerProperty'",
|
|
|
|
" '$uacDisablerTriggerProperty'")));
|
2023-06-28 19:20:59 +00:00
|
|
|
|
2023-06-29 17:40:54 +00:00
|
|
|
foreach ($options in $optionCollection) {
|
2024-03-23 00:27:49 +00:00
|
|
|
$action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument ((@("-c") + $options.Item3) -join " ");
|
2023-06-29 17:40:54 +00:00
|
|
|
|
|
|
|
schtasks /Create /SC ONEVENT /EC Application /MO "*[System[Provider[@Name='Application'] and EventID=$($options.Item1)]]" /TR cmd.exe /TN "$tempTask";
|
|
|
|
$trigger = (Get-ScheduledTask $tempTask).Triggers;
|
|
|
|
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest;
|
|
|
|
$task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger;
|
|
|
|
$null = Register-ScheduledTask $options.Item2 -InputObject $task;
|
|
|
|
$null = Unregister-ScheduledTask -Confirm:$false $tempTask;
|
|
|
|
}
|
2023-06-28 09:48:08 +00:00
|
|
|
}
|
2023-06-28 20:50:39 +00:00
|
|
|
|
2024-03-23 14:38:06 +00:00
|
|
|
function Enable-PersonalUserAutologon([Context] $context, [string] $userName)
|
2023-06-28 20:50:39 +00:00
|
|
|
{
|
|
|
|
Add-Type -assemblyname System.DirectoryServices.AccountManagement;
|
|
|
|
Write-Information "Re-Enabling Autologin for Current User";
|
|
|
|
$principalContext = [System.DirectoryServices.AccountManagement.PrincipalContext]::new("Machine");
|
|
|
|
|
|
|
|
while ($true)
|
|
|
|
{
|
2024-01-17 13:08:31 +00:00
|
|
|
$password = Read-Host "Please enter the password of your Microsoft Account" -MaskInput;
|
2023-06-28 20:50:39 +00:00
|
|
|
|
2024-03-23 14:38:06 +00:00
|
|
|
if ($principalContext.ValidateCredentials($userName, $password))
|
2023-06-28 20:50:39 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Write-Error "The specified password is incorrect!";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-24 00:10:44 +00:00
|
|
|
$context.Set($preparedUsernameProperty, $userName);
|
|
|
|
$context.Set($preparedPasswordProperty, $password);
|
2024-03-22 09:59:24 +00:00
|
|
|
Start-EventDrivenTask $context.Get($autoLoginTriggerProperty);
|
|
|
|
}
|
|
|
|
|
|
|
|
function Start-EventDrivenTask() {
|
|
|
|
param(
|
|
|
|
[int]$EventID
|
|
|
|
);
|
|
|
|
|
2024-03-22 11:19:05 +00:00
|
|
|
powershell -c {
|
|
|
|
param (
|
|
|
|
[int]$EventID
|
|
|
|
)
|
|
|
|
$identifier = "EventLog$EventID";
|
2024-03-22 13:51:17 +00:00
|
|
|
$applicationLog = Get-EventLog -List | Where-Object { $_.Log -eq "Application" };
|
|
|
|
|
|
|
|
Register-ObjectEvent -InputObject $applicationLog -EventName EntryWritten -Action {
|
|
|
|
$entry = $event.SourceEventArgs.Entry;
|
|
|
|
|
|
|
|
if ($entry.EventID -eq $EventID) {
|
2024-03-24 15:56:55 +00:00
|
|
|
$null = New-Event -SourceIdentifier $identifier;
|
2024-03-22 13:51:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-03-22 15:15:37 +00:00
|
|
|
$job = Start-Job {
|
|
|
|
Wait-Event -SourceIdentifier $identifier;
|
|
|
|
Wait-Event -SourceIdentifier $identifier;
|
|
|
|
};
|
|
|
|
|
|
|
|
Write-EventLog -LogName Application -Source "Application" -EventId $EventID -Message "This event was created by $env:Username";
|
2024-03-24 15:56:55 +00:00
|
|
|
$null = Wait-Job $job;
|
2024-03-22 11:19:05 +00:00
|
|
|
} -args $EventID
|
2023-06-28 20:50:39 +00:00
|
|
|
}
|