Allow execution of OneShot tasks
This commit is contained in:
parent
9e7ed6e85e
commit
ecb34288cc
3 changed files with 125 additions and 12 deletions
|
@ -3,7 +3,9 @@ using namespace System.Security.AccessControl;
|
|||
using namespace System.Security.Principal;
|
||||
|
||||
enum SetupStage {
|
||||
Idle
|
||||
Initialize
|
||||
OneShot
|
||||
Configure
|
||||
Install
|
||||
CreateUser
|
||||
|
@ -244,7 +246,7 @@ $null = New-Module {
|
|||
Gets a value indicating whether the setup has finished.
|
||||
#>
|
||||
function Get-IsFinished {
|
||||
return [bool] (Get-SetupOption $finishedOption);
|
||||
return [bool] (((Get-Stage) -eq ([SetupStage]::Idle)) -or (Get-SetupOption $finishedOption));
|
||||
}
|
||||
|
||||
<#
|
||||
|
|
|
@ -1,4 +1,21 @@
|
|||
function Start-Operation {
|
||||
. "$PSScriptRoot/Config.ps1";
|
||||
. "$PSScriptRoot/../Types/OneShotTask.ps1";
|
||||
. "$PSScriptRoot/../../Windows/Scripts/PowerManagement.ps1";
|
||||
|
||||
$null = New-Module {
|
||||
. "$PSScriptRoot/../Types/OneShotTask.ps1";
|
||||
$logName = "Application";
|
||||
$oneShotTrigger = 1337;
|
||||
$taskOption = "OneShotTask";
|
||||
# ToDo: Store "ProgramData/PortValhalla" path somewhere as const
|
||||
$errorPath = "$env:ProgramData/PortValhalla/error.txt";
|
||||
|
||||
$taskSetter = {
|
||||
param([Nullable[OneShotTask]] $Task)
|
||||
Set-SetupOption $taskOption ([string]$Task);
|
||||
};
|
||||
|
||||
function Start-Operation {
|
||||
param(
|
||||
[scriptblock] $Action
|
||||
)
|
||||
|
@ -12,4 +29,95 @@ function Start-Operation {
|
|||
|
||||
New-Alias "sudo" gsudo;
|
||||
& $Action;
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Gets the current OneShot task.
|
||||
#>
|
||||
function Get-OneShotTask {
|
||||
[OneShotTask](Get-SetupOption $taskOption);
|
||||
}
|
||||
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Registers a task for listening to OneShot invocations.
|
||||
#>
|
||||
function Enable-OneShotListener {
|
||||
$tempTask = "PortValhalla Temp";
|
||||
$action = New-ScheduledTaskAction -Execute "pwsh" -Argument (Get-StartupArguments);
|
||||
schtasks /Create /SC ONEVENT /EC $logName /MO "*[System[Provider[@Name='$logName'] and EventID=$($oneShotTrigger)]]" /TR cmd.exe /TN $tempTask;
|
||||
$trigger = (Get-ScheduledTask $tempTask).Triggers;
|
||||
$principal = New-ScheduledTaskPrincipal -UserId (Get-SetupUser) -RunLevel Highest;
|
||||
$task = New-ScheduledTask -Action $action -Principal $principal -Trigger $trigger;
|
||||
$null = Register-ScheduledTask -Force "PortValhalla OneShot" -InputObject $task;
|
||||
$null = Unregister-ScheduledTask -Confirm:$false $tempTask;
|
||||
}
|
||||
|
||||
<#
|
||||
.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;
|
||||
|
||||
$job = & {
|
||||
$identifier = "EventLog$oneShotTrigger";
|
||||
$log = [System.Diagnostics.EventLog]::new($logName);
|
||||
|
||||
$null = Register-ObjectEvent -InputObject $log -EventName EntryWritten -Action {
|
||||
$entry = $event.SourceEventArgs.Entry;
|
||||
|
||||
if ($entry.EventID -eq $oneShotTrigger) {
|
||||
$null = New-Event -SourceIdentifier $identifier;
|
||||
}
|
||||
};
|
||||
|
||||
Start-Job {
|
||||
Wait-Event -SourceIdentifier $identifier;
|
||||
};
|
||||
};
|
||||
|
||||
Write-EventLog -LogName $logName -Source $logName -EventId $oneShotTrigger -Message "Starting OneShot task ``$(Get-OneShotTask)``…";
|
||||
$null = Wait-Job $job;
|
||||
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.";
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
3
scripts/Common/Types/OneShotTask.ps1
Normal file
3
scripts/Common/Types/OneShotTask.ps1
Normal file
|
@ -0,0 +1,3 @@
|
|||
enum OneShotTask {
|
||||
DisableUAC
|
||||
}
|
Loading…
Reference in a new issue