Execute OneShot tasks in any operation
This commit is contained in:
parent
a9a90a64f9
commit
b4e0ed7db8
3 changed files with 438 additions and 445 deletions
scripts/Common/Scripts
|
@ -3,8 +3,6 @@ using namespace System.Security.AccessControl;
|
|||
using namespace System.Security.Principal;
|
||||
|
||||
enum SetupStage {
|
||||
Idle
|
||||
OneShot
|
||||
Configure
|
||||
Install
|
||||
CreateUser
|
||||
|
@ -389,7 +387,7 @@ $null = New-Module {
|
|||
Gets a value indicating whether the setup has finished.
|
||||
#>
|
||||
function Get-IsFinished {
|
||||
return [bool] (((Get-Stage) -eq ([SetupStage]::Idle)) -or (Get-SetupOption $finishedOption));
|
||||
return [bool](Get-SetupOption $finishedOption);
|
||||
}
|
||||
|
||||
<#
|
||||
|
|
|
@ -29,18 +29,24 @@ $null = New-Module {
|
|||
|
||||
function Start-Operation {
|
||||
param(
|
||||
[switch] $NonInteractive = ((Get-Stage) -eq ([SetupStage]::OneShot)),
|
||||
[switch] $NonInteractive,
|
||||
[switch] $NoImplicitCleanup,
|
||||
[scriptblock] $Action
|
||||
)
|
||||
|
||||
$cleanup = { };
|
||||
$taskPending = $false;
|
||||
|
||||
if (-not $Global:InOperation) {
|
||||
if ($env:DEBUG) {
|
||||
Set-PSDebug -Trace 1;
|
||||
}
|
||||
|
||||
if ($null -ne (Get-OneShotTask)) {
|
||||
$taskPending = $true;
|
||||
[switch] $NonInteractive = $true;
|
||||
}
|
||||
|
||||
$Global:InOperation = $true;
|
||||
$Global:ErrorActionPreference = $NonInteractive.IsPresent ? 'Continue' : 'Inquire';
|
||||
|
||||
|
@ -229,7 +235,12 @@ $null = New-Module {
|
|||
}
|
||||
}
|
||||
|
||||
& $Action;
|
||||
if ($taskPending) {
|
||||
Start-OneShot;
|
||||
} else {
|
||||
& $Action;
|
||||
}
|
||||
|
||||
& $cleanup;
|
||||
}
|
||||
|
||||
|
@ -238,7 +249,13 @@ $null = New-Module {
|
|||
Gets the current OneShot task.
|
||||
#>
|
||||
function Get-OneShotTask {
|
||||
[OneShotTask](Get-SetupOption $taskOption);
|
||||
$task = Get-SetupOption $taskOption;
|
||||
|
||||
if ($task) {
|
||||
return [OneShotTask]$task;
|
||||
} else {
|
||||
return $null;
|
||||
}
|
||||
}
|
||||
|
||||
<#
|
||||
|
@ -291,8 +308,6 @@ $null = New-Module {
|
|||
[OneShotTask] $Task
|
||||
)
|
||||
|
||||
$currentStage = Get-Stage;
|
||||
Set-Stage ([SetupStage]::OneShot);
|
||||
& $taskSetter $Task;
|
||||
|
||||
& {
|
||||
|
@ -321,8 +336,6 @@ $null = New-Module {
|
|||
}
|
||||
};
|
||||
|
||||
Set-Stage $currentStage;
|
||||
|
||||
if (Test-Path $errorPath) {
|
||||
$errorMessage = Get-Content $errorPath;
|
||||
Remove-Item $errorPath;
|
||||
|
@ -340,23 +353,27 @@ $null = New-Module {
|
|||
Executes the specified action and notifies the OneShot task executor.
|
||||
#>
|
||||
function Start-OneShot {
|
||||
param(
|
||||
[scriptblock] $Action
|
||||
)
|
||||
|
||||
try {
|
||||
Start-Operation -NonInteractive @PSBoundParameters;
|
||||
Write-Host "Running OneShot task ``$(Get-OneShotTask)``";
|
||||
|
||||
switch (Get-OneShotTask) {
|
||||
([OneShotTask]::InitializeMSAccount) {
|
||||
Initialize-UserCreation;
|
||||
}
|
||||
([OneShotTask]::DisableUAC) {
|
||||
Disable-UAC;
|
||||
Register-Setup;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch {
|
||||
Set-Content -Path $errorPath -Value $Error;
|
||||
Set-UserPermissions $errorPath;
|
||||
}
|
||||
finally {
|
||||
Set-Stage ([SetupStage]::Idle);
|
||||
& $taskSetter $null;
|
||||
Write-EventLog -LogName $logName -Source $logName -EventId $oneShotTrigger -Message "The OneShot task ``$(Get-OneShotTask)`` finished.";
|
||||
}
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
<#
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue