2024-08-09 22:22:30 +00:00
|
|
|
. "$PSScriptRoot/Config.ps1";
|
|
|
|
. "$PSScriptRoot/../Types/OneShotTask.ps1";
|
|
|
|
. "$PSScriptRoot/../../Windows/Scripts/PowerManagement.ps1";
|
2024-08-11 23:27:50 +00:00
|
|
|
. "$PSScriptRoot/../../Windows/Scripts/Registry.ps1";
|
2024-08-05 21:09:58 +00:00
|
|
|
|
2024-08-09 22:22:30 +00:00
|
|
|
$null = New-Module {
|
|
|
|
. "$PSScriptRoot/../Types/OneShotTask.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-09 22:22:30 +00:00
|
|
|
$taskSetter = {
|
|
|
|
param([Nullable[OneShotTask]] $Task)
|
|
|
|
Set-SetupOption $taskOption ([string]$Task);
|
|
|
|
};
|
|
|
|
|
|
|
|
function Start-Operation {
|
|
|
|
param(
|
|
|
|
[scriptblock] $Action
|
|
|
|
)
|
|
|
|
|
|
|
|
$Global:ErrorActionPreference = 'Inquire';
|
|
|
|
$env:WSLENV = "CONFIG_MODULE/p";
|
|
|
|
|
|
|
|
if ($env:CONFIG_MODULE) {
|
|
|
|
$env:CONFIG_MODULE = Resolve-Path $env:CONFIG_MODULE;
|
|
|
|
}
|
|
|
|
|
2024-08-16 12:35:35 +00:00
|
|
|
if (Test-Admin) {
|
|
|
|
Disable-WindowsUpdateAutoRestart;
|
|
|
|
}
|
|
|
|
|
2024-08-09 22:22:46 +00:00
|
|
|
New-Alias -Force "sudo" gsudo;
|
2024-08-09 22:22:30 +00:00
|
|
|
& $Action;
|
|
|
|
}
|
|
|
|
|
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Gets the current OneShot task.
|
|
|
|
#>
|
|
|
|
function Get-OneShotTask {
|
|
|
|
[OneShotTask](Get-SetupOption $taskOption);
|
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-16 21:03:19 +00:00
|
|
|
$action = New-ScheduledTaskAction -Execute "pwsh" -Argument ([string](Get-StartupArguments));
|
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-10 04:11:55 +00:00
|
|
|
$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest;
|
2024-08-09 22:22:30 +00:00
|
|
|
$task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger;
|
2024-08-10 03:19:24 +00:00
|
|
|
$null = Register-ScheduledTask -Force $oneShotTaskName -InputObject $task;
|
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-09 22:22:30 +00:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Invokes a one-shot task.
|
|
|
|
|
|
|
|
.PARAMETER Task
|
|
|
|
The task to run.
|
|
|
|
#>
|
|
|
|
function Invoke-OneShot {
|
|
|
|
param(
|
|
|
|
[OneShotTask] $Task
|
|
|
|
)
|
|
|
|
|
|
|
|
$currentStage = Get-Stage;
|
|
|
|
Set-Stage ([SetupStage]::OneShot);
|
|
|
|
& $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)``…";
|
|
|
|
Remove-Event -EventIdentifier (Wait-Event -SourceIdentifier $identifier).EventIdentifier;
|
|
|
|
$null = Wait-Event -SourceIdentifier $identifier;
|
2024-08-09 22:22:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Set-Stage $currentStage;
|
|
|
|
|
|
|
|
if (Test-Path $errorPath) {
|
|
|
|
$errorMessage = Get-Content $errorPath;
|
|
|
|
Remove-Item $errorPath;
|
|
|
|
throw $errorMessage;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# 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 {
|
|
|
|
param(
|
|
|
|
[scriptblock] $Action
|
|
|
|
)
|
|
|
|
|
|
|
|
try {
|
|
|
|
Start-Operation @PSBoundParameters;
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
Set-Content -Path $errorPath -Value $Error;
|
|
|
|
}
|
|
|
|
finally {
|
|
|
|
Set-Stage ([SetupStage]::Idle);
|
|
|
|
Write-EventLog -LogName $logName -Source $logName -EventId $oneShotTrigger -Message "The OneShot task ``$(Get-OneShotTask)`` finished.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|