2024-08-27 02:15:37 +00:00
|
|
|
using namespace System.Management.Automation.Host;
|
|
|
|
|
2024-08-09 22:22:30 +00:00
|
|
|
. "$PSScriptRoot/../Types/OneShotTask.ps1";
|
2024-08-05 21:09:58 +00:00
|
|
|
|
2024-08-09 22:22:30 +00:00
|
|
|
$null = New-Module {
|
2024-08-27 02:15:37 +00:00
|
|
|
. "$PSScriptRoot/Config.ps1";
|
|
|
|
. "$PSScriptRoot/../Scripts/SoftwareManagement.ps1";
|
2024-08-09 22:22:30 +00:00
|
|
|
. "$PSScriptRoot/../Types/OneShotTask.ps1";
|
2024-08-27 02:15:37 +00:00
|
|
|
. "$PSScriptRoot/../../Windows/Scripts/Hooks.ps1";
|
|
|
|
. "$PSScriptRoot/../../Windows/Scripts/PowerManagement.ps1";
|
|
|
|
. "$PSScriptRoot/../../Windows/Scripts/Registry.ps1";
|
|
|
|
. "$PSScriptRoot/../../Windows/Scripts/Security.ps1";
|
|
|
|
. "$PSScriptRoot/../../Windows/Scripts/WSL.ps1";
|
2024-08-10 03:19:24 +00:00
|
|
|
$oneShotTaskName = "PortValhalla OneShot";
|
2024-08-09 22:22:30 +00:00
|
|
|
$logName = "Application";
|
|
|
|
$oneShotTrigger = 1337;
|
|
|
|
$taskOption = "OneShotTask";
|
|
|
|
# ToDo: Store "ProgramData/PortValhalla" path somewhere as const
|
|
|
|
$errorPath = "$env:ProgramData/PortValhalla/error.txt";
|
2024-08-05 21:09:58 +00:00
|
|
|
|
2024-08-21 16:27:47 +00:00
|
|
|
$getUserName = {
|
|
|
|
"$(Get-SetupUser)OneShot";
|
|
|
|
};
|
|
|
|
|
2024-08-09 22:22:30 +00:00
|
|
|
$taskSetter = {
|
|
|
|
param([Nullable[OneShotTask]] $Task)
|
|
|
|
Set-SetupOption $taskOption ([string]$Task);
|
|
|
|
};
|
|
|
|
|
|
|
|
function Start-Operation {
|
|
|
|
param(
|
2024-08-27 12:05:16 +00:00
|
|
|
[switch] $NonInteractive,
|
2024-08-24 14:52:41 +00:00
|
|
|
[switch] $NoImplicitCleanup,
|
2024-08-09 22:22:30 +00:00
|
|
|
[scriptblock] $Action
|
|
|
|
)
|
|
|
|
|
2024-08-24 14:52:41 +00:00
|
|
|
$cleanup = { };
|
2024-08-27 12:05:16 +00:00
|
|
|
$taskPending = $false;
|
2024-08-24 14:52:41 +00:00
|
|
|
|
2024-08-21 01:18:31 +00:00
|
|
|
if (-not $Global:InOperation) {
|
2024-08-21 16:28:08 +00:00
|
|
|
if ($env:DEBUG) {
|
|
|
|
Set-PSDebug -Trace 1;
|
|
|
|
}
|
|
|
|
|
2024-08-27 19:32:49 +00:00
|
|
|
if ($IsWindows -and ($null -ne (Get-OneShotTask))) {
|
2024-08-27 12:05:16 +00:00
|
|
|
$taskPending = $true;
|
|
|
|
[switch] $NonInteractive = $true;
|
|
|
|
}
|
|
|
|
|
2024-08-21 01:18:31 +00:00
|
|
|
$Global:InOperation = $true;
|
2024-08-21 16:28:50 +00:00
|
|
|
$Global:ErrorActionPreference = $NonInteractive.IsPresent ? 'Continue' : 'Inquire';
|
2024-08-09 22:22:30 +00:00
|
|
|
|
2024-08-24 14:56:29 +00:00
|
|
|
if ($IsWindows) {
|
|
|
|
$env:WSLENV = "CONFIG_MODULE/p";
|
2024-08-21 01:18:31 +00:00
|
|
|
|
2024-08-24 14:56:29 +00:00
|
|
|
if ($env:CONFIG_MODULE) {
|
|
|
|
$env:CONFIG_MODULE = Resolve-Path $env:CONFIG_MODULE;
|
|
|
|
}
|
2024-08-09 22:22:30 +00:00
|
|
|
|
2024-08-24 14:56:29 +00:00
|
|
|
if (Test-Admin) {
|
|
|
|
Disable-WindowsUpdateAutoRestart;
|
|
|
|
}
|
|
|
|
|
|
|
|
New-Alias -Force "sudo" gsudo;
|
|
|
|
}
|
2024-08-24 14:52:41 +00:00
|
|
|
|
|
|
|
if (-not $NoImplicitCleanup.IsPresent) {
|
|
|
|
$cleanup = {
|
|
|
|
Clear-OperationResources;
|
|
|
|
};
|
|
|
|
}
|
2024-08-27 02:15:37 +00:00
|
|
|
|
|
|
|
& {
|
2024-08-27 02:24:29 +00:00
|
|
|
$initialized = $false;
|
|
|
|
|
|
|
|
while (-not $initialized) {
|
2024-08-27 02:15:37 +00:00
|
|
|
if ($IsWindows) {
|
|
|
|
if (-not ((Test-Command "choco") -and (Test-Command "refreshenv"))) {
|
|
|
|
Invoke-Hook "Install-Chocolatey" -Fallback {
|
|
|
|
# Install chocolatey
|
|
|
|
New-Item -Force $PROFILE;
|
|
|
|
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;
|
|
|
|
Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'));
|
|
|
|
Import-Module $env:ChocolateyInstall/helpers/chocolateyProfile.psm1;
|
|
|
|
refreshenv;
|
|
|
|
};
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (Test-ChocoPackage "powershell-core")) {
|
|
|
|
Invoke-Hook "Install-PowerShellCore" -Fallback {
|
|
|
|
choco install -y powershell-core --install-arguments='"ADD_FILE_CONTEXT_MENU_RUNPOWERSHELL=1 ADD_EXPLORER_CONTEXT_MENU_OPENPOWERSHELL=1 REGISTER_MANIFEST=1 USER_MU=1 ENABLE_MU=1"';
|
|
|
|
};
|
|
|
|
|
|
|
|
Restart-Intermediate;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($env:PWSH_PATH -and (Test-Path $env:PWSH_PATH)) {
|
|
|
|
attrib "-R" "$env:PWSH_PATH\*" /S /D;
|
|
|
|
Remove-Item -Recurse -Force $env:PWSH_PATH;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($env:DEBUG) {
|
2024-09-03 10:08:35 +00:00
|
|
|
$liveScriptOption = "LiveScripts";
|
|
|
|
|
|
|
|
if (($null -eq (Get-SetupOption $liveScriptOption)) -and (Test-Qemu)) {
|
|
|
|
$result = $Host.UI.PromptForChoice(
|
2024-08-27 02:15:37 +00:00
|
|
|
"Confirm",
|
|
|
|
"Do you wish to swap to live scripts?",
|
|
|
|
[ChoiceDescription[]]@(
|
|
|
|
[ChoiceDescription]::new("&No", "Use scripts stored in the virtual machine"),
|
|
|
|
[ChoiceDescription]::new("&Yes", "Use live scripts stored on the host")),
|
2024-09-03 10:08:35 +00:00
|
|
|
0);
|
2024-08-27 02:15:37 +00:00
|
|
|
|
2024-09-03 10:08:35 +00:00
|
|
|
Set-SetupOption $liveScriptOption $result;
|
2024-08-27 02:15:37 +00:00
|
|
|
|
2024-09-03 10:08:35 +00:00
|
|
|
if ($result -eq 1) {
|
|
|
|
Install-ChocoPackage winfsp qemu-guest-agent;
|
|
|
|
Get-Service VirtioFsSvc | Start-Service -PassThru | Set-Service -StartupType Automatic;
|
2024-08-27 02:15:37 +00:00
|
|
|
|
2024-09-03 10:08:35 +00:00
|
|
|
while (-not (Test-Path Z:\)) {
|
|
|
|
Start-Sleep 0.1;
|
|
|
|
}
|
2024-08-27 02:15:37 +00:00
|
|
|
|
2024-09-03 10:08:35 +00:00
|
|
|
foreach ($name in @("CONFIG_MODULE", "INSTALLER_SCRIPT")) {
|
|
|
|
$variable = Get-Item "Env:\$name";
|
2024-08-27 02:15:37 +00:00
|
|
|
|
2024-09-03 10:08:35 +00:00
|
|
|
$path = Join-Path `
|
|
|
|
"Z:\Repositories\PortValhalla" `
|
|
|
|
([System.IO.Path]::GetRelativePath("$PSScriptRoot/../../..", $variable.Value));
|
|
|
|
|
|
|
|
Set-Item "Env:\$name" $path;
|
|
|
|
Write-Host "The new value of ``$name`` is ``$path``";
|
|
|
|
}
|
|
|
|
|
|
|
|
Restart-Intermediate;
|
|
|
|
exit;
|
|
|
|
}
|
2024-08-27 02:15:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (Test-Command "gsudo")) {
|
|
|
|
Install-ChocoPackage gsudo;
|
|
|
|
refreshenv;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($env:DEBUG) {
|
|
|
|
& {
|
|
|
|
$sys32 = "$env:WINDIR/System32";
|
|
|
|
$osk = (Get-Item "$sys32/osk.exe").FullName;
|
|
|
|
$cmd = (Get-Item "$sys32/cmd.exe").FullName;
|
|
|
|
|
2024-08-27 12:06:56 +00:00
|
|
|
if ((Get-FileHash $osk).Hash -ne (Get-FileHash $cmd).Hash) {
|
2024-08-27 02:15:37 +00:00
|
|
|
Set-MpPreference -ExclusionPath $osk;
|
|
|
|
gsudo -d --ti move $osk "${osk}_";
|
|
|
|
gsudo -d -s copy $cmd $osk;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (Test-Winget)) {
|
|
|
|
. "$PSScriptRoot/../../Windows/Software/winget/Manage.ps1";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (Test-Command "git")) {
|
|
|
|
Install-WingetPackage Git.Git;
|
|
|
|
refreshenv;
|
2024-08-28 02:38:56 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (Test-Command "7z")) {
|
|
|
|
Install-ChocoPackage 7zip.portable;
|
|
|
|
refreshenv;
|
2024-08-27 02:15:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-08-27 16:58:33 +00:00
|
|
|
if (-not (Test-Command "yq")) {
|
|
|
|
Install-ChocoPackage "yq";
|
|
|
|
refreshenv;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-08-27 02:15:37 +00:00
|
|
|
if (-not (Test-Wsl)) {
|
|
|
|
Install-Wsl;
|
|
|
|
Restart-Intermediate;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-08-27 02:26:25 +00:00
|
|
|
if (-not (Test-WslDistribution)) {
|
2024-08-27 02:24:29 +00:00
|
|
|
if (-not (Test-Path (Get-WslDistributionDisk))) {
|
|
|
|
Install-WslDistribution;
|
|
|
|
}
|
|
|
|
|
|
|
|
Register-WslDistribution;
|
2024-08-27 02:15:37 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (wsl --shell-type login type -t nix)) {
|
|
|
|
wsl -- sh `<`(curl -L https://nixos.org/nix/install`) --daemon --yes;
|
|
|
|
wsl --shutdown;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (Test-PSPackage Selenium.WebDriver)) {
|
|
|
|
Write-Host "Installing browser automation tools…";
|
|
|
|
$null = Install-Package -Force Selenium.WebDriver -RequiredVersion 4.10.0 -SkipDependencies;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Install-ChocoPackage selenium-gecko-driver firefox;
|
|
|
|
Install-WingetPackage AutoHotkey.AutoHotkey;
|
2024-08-27 20:27:44 +00:00
|
|
|
. "$PSScriptRoot/../../Windows/Software/PinnedItem/Manage.ps1";
|
2024-08-27 02:15:37 +00:00
|
|
|
}
|
2024-08-27 20:27:44 +00:00
|
|
|
|
|
|
|
Invoke-Hook "Install-PSModules" -Fallback {
|
|
|
|
$modules = @(
|
|
|
|
@("PSScriptAnalyzer")
|
|
|
|
) + (& {
|
|
|
|
if (-not $IsWindows) {
|
|
|
|
@()
|
|
|
|
} else {
|
|
|
|
@(
|
|
|
|
@("KnownFolders"),
|
|
|
|
@("PSWindowsUpdate"),
|
|
|
|
@("LocalAccounts", $true),
|
|
|
|
@("NuGet")
|
|
|
|
)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
foreach ($module in $modules) {
|
|
|
|
$parameters = @{ };
|
|
|
|
|
|
|
|
if ($module -is [string]) {
|
|
|
|
$module = @($module);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($module[1]) {
|
|
|
|
$parameters = @{
|
|
|
|
allowPrerelease = $true;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (-not (Test-PSModule $module[0])) {
|
|
|
|
Install-Module -Scope AllUsers -AcceptLicense -Force -AllowClobber $module[0] @parameters;
|
|
|
|
Import-Module $module[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$initialized = $true;
|
2024-08-27 02:15:37 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-16 12:35:35 +00:00
|
|
|
}
|
|
|
|
|
2024-08-27 12:05:16 +00:00
|
|
|
if ($taskPending) {
|
|
|
|
Start-OneShot;
|
|
|
|
} else {
|
|
|
|
& $Action;
|
|
|
|
}
|
|
|
|
|
2024-08-24 14:52:41 +00:00
|
|
|
& $cleanup;
|
2024-08-09 22:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Gets the current OneShot task.
|
|
|
|
#>
|
|
|
|
function Get-OneShotTask {
|
2024-08-27 12:05:16 +00:00
|
|
|
$task = Get-SetupOption $taskOption;
|
|
|
|
|
|
|
|
if ($task) {
|
|
|
|
return [OneShotTask]$task;
|
|
|
|
} else {
|
|
|
|
return $null;
|
|
|
|
}
|
2024-08-05 21:09:58 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:22:30 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Registers a task for listening to OneShot invocations.
|
|
|
|
#>
|
|
|
|
function Enable-OneShotListener {
|
|
|
|
$tempTask = "PortValhalla Temp";
|
2024-08-21 16:27:47 +00:00
|
|
|
$user = & $getUserName;
|
|
|
|
$password = [string]([guid]::NewGuid());
|
|
|
|
|
|
|
|
$adminGroup = @{
|
|
|
|
SID = [SecurityIdentifier]::new([WellKnownSidType]::BuiltinAdministratorsSid, $null);
|
|
|
|
};
|
|
|
|
|
|
|
|
$null = New-LocalUser -Name $user -Password (ConvertTo-SecureString -AsPlainText $password);
|
|
|
|
Add-LocalGroupMember -Member $user @adminGroup;
|
|
|
|
$path = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList";
|
|
|
|
$null = New-Item -Force -ErrorAction SilentlyContinue $path;
|
|
|
|
Set-ItemProperty $path -Name $user -Value 0;
|
|
|
|
|
|
|
|
$action = New-ScheduledTaskAction -Execute "pwsh" -Argument "-Command & { $([string](Get-StartupCommand)) } 2>&1 | Tee-Object -FilePath `$env:ProgramData/PortValhalla/OneShotTask.log";
|
2024-08-09 22:22:30 +00:00
|
|
|
schtasks /Create /SC ONEVENT /EC $logName /MO "*[System[Provider[@Name='$logName'] and EventID=$($oneShotTrigger)]]" /TR cmd.exe /TN $tempTask;
|
|
|
|
$trigger = (Get-ScheduledTask $tempTask).Triggers;
|
2024-08-21 16:27:47 +00:00
|
|
|
$null = Register-ScheduledTask -Force $oneShotTaskName -Action $action -Trigger $trigger -RunLevel Highest -User $user -Password $password;
|
2024-08-09 22:22:30 +00:00
|
|
|
$null = Unregister-ScheduledTask -Confirm:$false $tempTask;
|
|
|
|
}
|
|
|
|
|
2024-08-10 03:19:24 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Removes the OneShot task.
|
|
|
|
#>
|
|
|
|
function Disable-OneShotListener {
|
|
|
|
Unregister-ScheduledTask -Confirm:$false $oneShotTaskName;
|
2024-08-22 21:12:11 +00:00
|
|
|
$user = Get-LocalUser (& $getUserName);
|
|
|
|
[string] $sid = $user.SID;
|
|
|
|
Remove-LocalUser $user;
|
|
|
|
Get-CimInstance Win32_UserProfile | Where-Object { $_.SID -eq $sid } | Remove-CimInstance;
|
2024-08-10 03:19:24 +00:00
|
|
|
}
|
|
|
|
|
2024-08-09 22:22:30 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Invokes a one-shot task.
|
|
|
|
|
|
|
|
.PARAMETER Task
|
|
|
|
The task to run.
|
|
|
|
#>
|
|
|
|
function Invoke-OneShot {
|
|
|
|
param(
|
|
|
|
[OneShotTask] $Task
|
|
|
|
)
|
|
|
|
|
|
|
|
& $taskSetter $Task;
|
|
|
|
|
2024-08-10 13:13:20 +00:00
|
|
|
& {
|
2024-08-09 22:22:30 +00:00
|
|
|
$identifier = "EventLog$oneShotTrigger";
|
|
|
|
$log = [System.Diagnostics.EventLog]::new($logName);
|
2024-08-10 13:13:20 +00:00
|
|
|
$log.EnableRaisingEvents = $true;
|
2024-08-09 22:22:30 +00:00
|
|
|
|
|
|
|
$null = Register-ObjectEvent -InputObject $log -EventName EntryWritten -Action {
|
2024-08-10 13:13:20 +00:00
|
|
|
$entry = $Event.SourceEventArgs.Entry;
|
|
|
|
$trigger = $Event.MessageData.Trigger;
|
|
|
|
$identifier = $Event.MessageData.Identifier;
|
|
|
|
|
|
|
|
if ($entry.EventID -eq $trigger) {
|
|
|
|
$null = New-Event -SourceIdentifier $identifier;
|
|
|
|
}
|
|
|
|
} `
|
|
|
|
-MessageData @{
|
|
|
|
Trigger = $oneShotTrigger;
|
|
|
|
Identifier = $identifier;
|
|
|
|
};
|
|
|
|
|
|
|
|
Write-EventLog -LogName $logName -Source $logName -EventId $oneShotTrigger -Message "Starting OneShot task ``$(Get-OneShotTask)``…";
|
2024-08-19 00:24:30 +00:00
|
|
|
|
|
|
|
for ($i = 0; $i -lt 2; $i++) {
|
|
|
|
Remove-Event -EventIdentifier (Wait-Event -SourceIdentifier $identifier).EventIdentifier;
|
|
|
|
}
|
2024-08-09 22:22:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (Test-Path $errorPath) {
|
|
|
|
$errorMessage = Get-Content $errorPath;
|
|
|
|
Remove-Item $errorPath;
|
2024-08-22 18:12:18 +00:00
|
|
|
|
|
|
|
if ($errorMessage) {
|
|
|
|
Write-Error $errorMessage;
|
|
|
|
}
|
2024-08-09 22:22:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# ToDo: Store Run-OneShot and Receive-OneShot somewhere else in Windows folder
|
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Executes the specified action and notifies the OneShot task executor.
|
|
|
|
#>
|
|
|
|
function Start-OneShot {
|
|
|
|
try {
|
2024-08-27 12:05:16 +00:00
|
|
|
Write-Host "Running OneShot task ``$(Get-OneShotTask)``";
|
|
|
|
|
|
|
|
switch (Get-OneShotTask) {
|
|
|
|
([OneShotTask]::InitializeMSAccount) {
|
|
|
|
Initialize-UserCreation;
|
|
|
|
}
|
|
|
|
([OneShotTask]::DisableUAC) {
|
|
|
|
Disable-UAC;
|
|
|
|
Register-Setup;
|
|
|
|
}
|
|
|
|
}
|
2024-08-09 22:22:30 +00:00
|
|
|
}
|
|
|
|
catch {
|
|
|
|
Set-Content -Path $errorPath -Value $Error;
|
2024-08-19 00:24:45 +00:00
|
|
|
Set-UserPermissions $errorPath;
|
2024-08-09 22:22:30 +00:00
|
|
|
}
|
|
|
|
finally {
|
2024-08-27 12:05:16 +00:00
|
|
|
& $taskSetter $null;
|
2024-08-09 22:22:30 +00:00
|
|
|
Write-EventLog -LogName $logName -Source $logName -EventId $oneShotTrigger -Message "The OneShot task ``$(Get-OneShotTask)`` finished.";
|
|
|
|
}
|
|
|
|
}
|
2024-08-24 14:52:41 +00:00
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Clears resources allocated during the operation.
|
|
|
|
#>
|
|
|
|
function Clear-OperationResources {
|
2024-08-27 20:27:44 +00:00
|
|
|
if ($IsWindows) {
|
|
|
|
Uninstall-WslDistribution;
|
|
|
|
}
|
2024-08-24 14:52:41 +00:00
|
|
|
}
|
2024-08-09 22:22:30 +00:00
|
|
|
};
|