From bd2dc9046350642cf153a40ddc24eb14fc4c2ecc Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Fri, 9 Aug 2024 23:23:29 +0200 Subject: [PATCH] Add dedicated functions for creating startup scripts --- scripts/Windows/Scripts/PowerManagement.ps1 | 41 ++++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/scripts/Windows/Scripts/PowerManagement.ps1 b/scripts/Windows/Scripts/PowerManagement.ps1 index 6c361b34..70c7fb8f 100644 --- a/scripts/Windows/Scripts/PowerManagement.ps1 +++ b/scripts/Windows/Scripts/PowerManagement.ps1 @@ -38,6 +38,32 @@ $null = New-Module { } } + <# + .SYNOPSIS + Generates a script for executing the installer. + #> + function Get-StartupScript { + "pwsh " + (Get-StartupArguments); + } + + <# + .SYNOPSIS + Generates arguments for running the installer using `pwsh`. + #> + function Get-StartupArguments { + "-Command " + + (& { + if ($env:PWSH_PATH) { + "`$env:PWSH_PATH = $(ConvertTo-Injection $env:PWSH_PATH);" + } else { + "" + } + }) + + "`$env:INSTALLER_SCRIPT = $(ConvertTo-Injection (Resolve-Path $env:INSTALLER_SCRIPT));" + + "`$env:CONFIG_MODULE = $(ConvertTo-Injection ($env:CONFIG_MODULE));" + + "& `$env:INSTALLER_SCRIPT;"; + } + <# .SYNOPSIS Registers a task to run the setup once after the next reboot. @@ -65,20 +91,7 @@ $null = New-Module { $key = Get-RunOnceKey; } - Set-ItemProperty -Path $key.PSPath -Name $runOnceName -Type "ExpandString" -Value ( - "pwsh -Command " + - (& { - if ($env:PWSH_PATH) { - "`$env:PWSH_PATH = $(ConvertTo-Injection $env:PWSH_PATH);" - } else { - "" - } - }) + - "`$env:INSTALLER_SCRIPT = $(ConvertTo-Injection (Resolve-Path $env:INSTALLER_SCRIPT));" + - "`$env:CONFIG_MODULE = $(ConvertTo-Injection $env:CONFIG_MODULE);" + - "& `$env:INSTALLER_SCRIPT;" - ); - + Set-ItemProperty -Path $key.PSPath -Name $runOnceName -Type "ExpandString" -Value (Get-StartupScript); $key.Handle.Close(); }