diff --git a/scripts/Common/Scripts/Operations.ps1 b/scripts/Common/Scripts/Operations.ps1 index d59e700b..3abee6e4 100644 --- a/scripts/Common/Scripts/Operations.ps1 +++ b/scripts/Common/Scripts/Operations.ps1 @@ -4,8 +4,10 @@ using namespace System.Management.Automation.Host; $null = New-Module { . "$PSScriptRoot/Config.ps1"; + . "$PSScriptRoot/Scripting.ps1"; . "$PSScriptRoot/../Scripts/SoftwareManagement.ps1"; . "$PSScriptRoot/../Types/OneShotTask.ps1"; + . "$PSScriptRoot/../../Windows/Scripts/Constants.ps1"; . "$PSScriptRoot/../../Windows/Scripts/Hooks.ps1"; . "$PSScriptRoot/../../Windows/Scripts/PowerManagement.ps1"; . "$PSScriptRoot/../../Windows/Scripts/Registry.ps1"; @@ -15,8 +17,10 @@ $null = New-Module { $logName = "Application"; $oneShotTrigger = 1337; $taskOption = "OneShotTask"; - # ToDo: Store "ProgramData/PortValhalla" path somewhere as const - $errorPath = "$env:ProgramData/PortValhalla/error.txt"; + + $getErrorPath = { + Join-Path (Get-ArtifactRoot) "error.txt"; + }; $getUserName = { "$(Get-SetupUser)OneShot"; @@ -314,11 +318,12 @@ $null = New-Module { $null = New-LocalUser -Name $user -Password (ConvertTo-SecureString -AsPlainText $password); Add-LocalGroupMember -Member $user @adminGroup; + $logPath = Join-Path (Get-ArtifactRoot) "OneShotTask.log"; $path = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList"; $null = New-Item -Force -ErrorAction SilentlyContinue $path; 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; $trigger = (Get-ScheduledTask $tempTask).Triggers; $null = Register-ScheduledTask -Force $oneShotTaskName -Action $action -Trigger $trigger -RunLevel Highest -User $user -Password $password; @@ -349,6 +354,7 @@ $null = New-Module { [OneShotTask] $Task ) + $errorPath = & $getErrorPath; & $taskSetter $Task; & { @@ -408,6 +414,7 @@ $null = New-Module { } } catch { + $errorPath = & $getErrorPath; Set-Content -Path $errorPath -Value $Error; Set-UserPermissions $errorPath; } diff --git a/scripts/Windows/Scripts/Constants.ps1 b/scripts/Windows/Scripts/Constants.ps1 new file mode 100644 index 00000000..0b5e88c0 --- /dev/null +++ b/scripts/Windows/Scripts/Constants.ps1 @@ -0,0 +1,7 @@ +<# + .SYNOPSIS + Gets the path to a directory to store setup artifacts. +#> +function Get-ArtifactRoot { + Join-Path "$env:ProgramData" "PortValhalla"; +} diff --git a/scripts/Windows/Scripts/WSL.ps1 b/scripts/Windows/Scripts/WSL.ps1 index fec3fa71..eb84e838 100644 --- a/scripts/Windows/Scripts/WSL.ps1 +++ b/scripts/Windows/Scripts/WSL.ps1 @@ -1,3 +1,4 @@ +. "$PSScriptRoot/Constants.ps1"; . "$PSScriptRoot/../Scripts/Security.ps1"; <# @@ -13,7 +14,7 @@ function Get-WslDistributionName { Gets the path to the directory containing the WSL distribution. #> function Get-WslDistributionPath { - return "$env:ProgramData/PortValhalla/$(Get-WslDistributionName)"; + Join-Path (Get-ArtifactRoot) (Get-WslDistributionName); } <#