Fix non-functioning OneShot script

This commit is contained in:
Manuel Thalmann 2024-08-10 15:13:20 +02:00
parent 270854f435
commit f20523c43b

View file

@ -79,26 +79,30 @@ $null = New-Module {
Set-Stage ([SetupStage]::OneShot); Set-Stage ([SetupStage]::OneShot);
& $taskSetter $Task; & $taskSetter $Task;
$job = & { & {
$identifier = "EventLog$oneShotTrigger"; $identifier = "EventLog$oneShotTrigger";
$log = [System.Diagnostics.EventLog]::new($logName); $log = [System.Diagnostics.EventLog]::new($logName);
$log.EnableRaisingEvents = $true;
$null = Register-ObjectEvent -InputObject $log -EventName EntryWritten -Action { $null = Register-ObjectEvent -InputObject $log -EventName EntryWritten -Action {
$entry = $event.SourceEventArgs.Entry; $entry = $Event.SourceEventArgs.Entry;
$trigger = $Event.MessageData.Trigger;
$identifier = $Event.MessageData.Identifier;
if ($entry.EventID -eq $oneShotTrigger) { if ($entry.EventID -eq $trigger) {
$null = New-Event -SourceIdentifier $identifier; $null = New-Event -SourceIdentifier $identifier;
} }
}; } `
-MessageData @{
Trigger = $oneShotTrigger;
Identifier = $identifier;
};
Start-Job { Write-EventLog -LogName $logName -Source $logName -EventId $oneShotTrigger -Message "Starting OneShot task ``$(Get-OneShotTask)``";
Wait-Event -SourceIdentifier $identifier; Remove-Event -EventIdentifier (Wait-Event -SourceIdentifier $identifier).EventIdentifier;
Wait-Event -SourceIdentifier $identifier; $null = 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; Set-Stage $currentStage;
if (Test-Path $errorPath) { if (Test-Path $errorPath) {