Store the artifact path in an individual script

This commit is contained in:
Manuel Thalmann 2024-09-23 00:33:54 +02:00
parent 5bbe197ec6
commit 50b4a30256
3 changed files with 19 additions and 4 deletions

View file

@ -4,8 +4,10 @@ using namespace System.Management.Automation.Host;
$null = New-Module { $null = New-Module {
. "$PSScriptRoot/Config.ps1"; . "$PSScriptRoot/Config.ps1";
. "$PSScriptRoot/Scripting.ps1";
. "$PSScriptRoot/../Scripts/SoftwareManagement.ps1"; . "$PSScriptRoot/../Scripts/SoftwareManagement.ps1";
. "$PSScriptRoot/../Types/OneShotTask.ps1"; . "$PSScriptRoot/../Types/OneShotTask.ps1";
. "$PSScriptRoot/../../Windows/Scripts/Constants.ps1";
. "$PSScriptRoot/../../Windows/Scripts/Hooks.ps1"; . "$PSScriptRoot/../../Windows/Scripts/Hooks.ps1";
. "$PSScriptRoot/../../Windows/Scripts/PowerManagement.ps1"; . "$PSScriptRoot/../../Windows/Scripts/PowerManagement.ps1";
. "$PSScriptRoot/../../Windows/Scripts/Registry.ps1"; . "$PSScriptRoot/../../Windows/Scripts/Registry.ps1";
@ -15,8 +17,10 @@ $null = New-Module {
$logName = "Application"; $logName = "Application";
$oneShotTrigger = 1337; $oneShotTrigger = 1337;
$taskOption = "OneShotTask"; $taskOption = "OneShotTask";
# ToDo: Store "ProgramData/PortValhalla" path somewhere as const
$errorPath = "$env:ProgramData/PortValhalla/error.txt"; $getErrorPath = {
Join-Path (Get-ArtifactRoot) "error.txt";
};
$getUserName = { $getUserName = {
"$(Get-SetupUser)OneShot"; "$(Get-SetupUser)OneShot";
@ -314,11 +318,12 @@ $null = New-Module {
$null = New-LocalUser -Name $user -Password (ConvertTo-SecureString -AsPlainText $password); $null = New-LocalUser -Name $user -Password (ConvertTo-SecureString -AsPlainText $password);
Add-LocalGroupMember -Member $user @adminGroup; Add-LocalGroupMember -Member $user @adminGroup;
$logPath = Join-Path (Get-ArtifactRoot) "OneShotTask.log";
$path = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"; $path = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList";
$null = New-Item -Force -ErrorAction SilentlyContinue $path; $null = New-Item -Force -ErrorAction SilentlyContinue $path;
Set-ItemProperty $path -Name $user -Value 0; 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"; $action = New-ScheduledTaskAction -Execute "pwsh" -Argument "-Command & { $([string](Get-StartupCommand)) } 2>&1 | Tee-Object -FilePath $(ConvertTo-Injection $logPath)";
schtasks /Create /SC ONEVENT /EC $logName /MO "*[System[Provider[@Name='$logName'] and EventID=$($oneShotTrigger)]]" /TR cmd.exe /TN $tempTask; schtasks /Create /SC ONEVENT /EC $logName /MO "*[System[Provider[@Name='$logName'] and EventID=$($oneShotTrigger)]]" /TR cmd.exe /TN $tempTask;
$trigger = (Get-ScheduledTask $tempTask).Triggers; $trigger = (Get-ScheduledTask $tempTask).Triggers;
$null = Register-ScheduledTask -Force $oneShotTaskName -Action $action -Trigger $trigger -RunLevel Highest -User $user -Password $password; $null = Register-ScheduledTask -Force $oneShotTaskName -Action $action -Trigger $trigger -RunLevel Highest -User $user -Password $password;
@ -349,6 +354,7 @@ $null = New-Module {
[OneShotTask] $Task [OneShotTask] $Task
) )
$errorPath = & $getErrorPath;
& $taskSetter $Task; & $taskSetter $Task;
& { & {
@ -408,6 +414,7 @@ $null = New-Module {
} }
} }
catch { catch {
$errorPath = & $getErrorPath;
Set-Content -Path $errorPath -Value $Error; Set-Content -Path $errorPath -Value $Error;
Set-UserPermissions $errorPath; Set-UserPermissions $errorPath;
} }

View file

@ -0,0 +1,7 @@
<#
.SYNOPSIS
Gets the path to a directory to store setup artifacts.
#>
function Get-ArtifactRoot {
Join-Path "$env:ProgramData" "PortValhalla";
}

View file

@ -1,3 +1,4 @@
. "$PSScriptRoot/Constants.ps1";
. "$PSScriptRoot/../Scripts/Security.ps1"; . "$PSScriptRoot/../Scripts/Security.ps1";
<# <#
@ -13,7 +14,7 @@ function Get-WslDistributionName {
Gets the path to the directory containing the WSL distribution. Gets the path to the directory containing the WSL distribution.
#> #>
function Get-WslDistributionPath { function Get-WslDistributionPath {
return "$env:ProgramData/PortValhalla/$(Get-WslDistributionName)"; Join-Path (Get-ArtifactRoot) (Get-WslDistributionName);
} }
<# <#