From fe20802876dcda1d5523c438dee5a1201b4068e1 Mon Sep 17 00:00:00 2001 From: Marterich <47688561+Marterich@users.noreply.github.com> Date: Wed, 11 Sep 2024 22:22:56 +0200 Subject: [PATCH 1/3] Fix the way arguments are passed to the runspace --- functions/public/Invoke-WPFInstall.ps1 | 2 +- functions/public/Invoke-WPFRunspace.ps1 | 14 +++++++++++--- functions/public/Invoke-WPFUnInstall.ps1 | 2 +- functions/public/Invoke-WPFtweaksbutton.ps1 | 13 +++++++++---- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/functions/public/Invoke-WPFInstall.ps1 b/functions/public/Invoke-WPFInstall.ps1 index f67c1353c9..ef03af36da 100644 --- a/functions/public/Invoke-WPFInstall.ps1 +++ b/functions/public/Invoke-WPFInstall.ps1 @@ -20,7 +20,7 @@ function Invoke-WPFInstall { return } $ChocoPreference = $($sync.WPFpreferChocolatey.IsChecked) - Invoke-WPFRunspace -ArgumentList $PackagesToInstall,$ChocoPreference -DebugPreference $DebugPreference -ScriptBlock { + $installHandle = Invoke-WPFRunspace -ParameterList @(("PackagesToInstall", $PackagesToInstall),("ChocoPreference", $ChocoPreference)) -DebugPreference $DebugPreference -ScriptBlock { param($PackagesToInstall, $ChocoPreference, $DebugPreference) if ($PackagesToInstall.count -eq 1) { $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) diff --git a/functions/public/Invoke-WPFRunspace.ps1 b/functions/public/Invoke-WPFRunspace.ps1 index 440a13b7e4..06f227b9ac 100644 --- a/functions/public/Invoke-WPFRunspace.ps1 +++ b/functions/public/Invoke-WPFRunspace.ps1 @@ -10,18 +10,24 @@ function Invoke-WPFRunspace { .PARAMETER ArgumentList A list of arguments to pass to the runspace - + + .PARAMETER ParameterList + A list of named parameters that should be provided. .EXAMPLE Invoke-WPFRunspace ` -ScriptBlock $sync.ScriptsInstallPrograms ` -ArgumentList "Installadvancedip,Installbitwarden" ` + Invoke-WPFRunspace` + -ScriptBlock $sync.ScriptsInstallPrograms ` + -ParameterList @(("PackagesToInstall", @("Installadvancedip,Installbitwarden")),("ChocoPreference", $true)) #> [CmdletBinding()] Param ( $ScriptBlock, $ArgumentList, + $ParameterList, $DebugPreference ) @@ -30,8 +36,10 @@ function Invoke-WPFRunspace { # Add Scriptblock and Arguments to runspace $script:powershell.AddScript($ScriptBlock) - foreach ($Argument in $ArgumentList) { - $script:powershell.AddArgument($Argument) + $script:powershell.AddArgument($ArgumentList) + + foreach ($parameter in $ParameterList){ + $script:powershell.AddParameter($parameter[0], $parameter[1]) } $script:powershell.AddArgument($DebugPreference) # Pass DebugPreference to the script block $script:powershell.RunspacePool = $sync.runspace diff --git a/functions/public/Invoke-WPFUnInstall.ps1 b/functions/public/Invoke-WPFUnInstall.ps1 index a1a670a3c8..7180ecf20e 100644 --- a/functions/public/Invoke-WPFUnInstall.ps1 +++ b/functions/public/Invoke-WPFUnInstall.ps1 @@ -30,7 +30,7 @@ function Invoke-WPFUnInstall { if($confirm -eq "No") {return} $ChocoPreference = $($sync.WPFpreferChocolatey.IsChecked) - Invoke-WPFRunspace -ArgumentList $PackagesToInstall, $ChocoPreference -DebugPreference $DebugPreference -ScriptBlock { + Invoke-WPFRunspace -ArgumentList @(("PackagesToInstall", $PackagesToInstall),("ChocoPreference", $ChocoPreference)) -DebugPreference $DebugPreference -ScriptBlock { param($PackagesToInstall, $ChocoPreference, $DebugPreference) if ($PackagesToInstall.count -eq 1) { $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) diff --git a/functions/public/Invoke-WPFtweaksbutton.ps1 b/functions/public/Invoke-WPFtweaksbutton.ps1 index a4f3819360..d0a5319086 100644 --- a/functions/public/Invoke-WPFtweaksbutton.ps1 +++ b/functions/public/Invoke-WPFtweaksbutton.ps1 @@ -24,8 +24,12 @@ function Invoke-WPFtweaksbutton { Write-Debug "Number of tweaks to process: $($Tweaks.Count)" - Invoke-WPFRunspace -ArgumentList $Tweaks -DebugPreference $DebugPreference -ScriptBlock { - param($Tweaks, $DebugPreference) + # The leading "," in the ParameterList is nessecary because we only provide one argument and powershell cannot be convinced that we want a nested loop with only one argument otherwise + $tweaksHandle = Invoke-WPFRunspace -ParameterList @(,("tweaks",$tweaks)) -DebugPreference $DebugPreference -ScriptBlock { + param( + $tweaks, + $DebugPreference + ) Write-Debug "Inside Number of tweaks to process: $($Tweaks.Count)" $sync.ProcessRunning = $true @@ -38,8 +42,9 @@ function Invoke-WPFtweaksbutton { # Execute other selected tweaks for ($i = 0; $i -lt $Tweaks.Count; $i++) { - Set-WinUtilProgressBar -Label "Applying $($tweaks[$i])" -Percent ($i / $Tweaks.Count * 100) - Invoke-WinUtilTweaks $tweaks[$i]$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value ($i/$Tweaks.Count) }) + Set-WinUtilProgressBar -Label "Applying $($tweaks[$i])" -Percent ($i / $tweaks.Count * 100) + Invoke-WinUtilTweaks $tweaks[$i] + $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value ($i/$Tweaks.Count) }) } Set-WinUtilProgressBar -Label "Tweaks finished" -Percent 100 $sync.ProcessRunning = $false From f727c783e14815cdfe333bf1ff84da7dd6b72cdd Mon Sep 17 00:00:00 2001 From: Marterich <47688561+Marterich@users.noreply.github.com> Date: Wed, 11 Sep 2024 22:39:51 +0200 Subject: [PATCH 2/3] Add $handle return --- functions/public/Invoke-WPFRunspace.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/functions/public/Invoke-WPFRunspace.ps1 b/functions/public/Invoke-WPFRunspace.ps1 index 06f227b9ac..f8c93c6ee0 100644 --- a/functions/public/Invoke-WPFRunspace.ps1 +++ b/functions/public/Invoke-WPFRunspace.ps1 @@ -55,4 +55,6 @@ function Invoke-WPFRunspace { $sync.runspace.Close() [System.GC]::Collect() } + # Return the handle + return $handle } From 020a57c4439291e396342067f383e43905e4db3b Mon Sep 17 00:00:00 2001 From: Marterich <47688561+Marterich@users.noreply.github.com> Date: Wed, 11 Sep 2024 23:03:26 +0200 Subject: [PATCH 3/3] Fix Choco Install logic --- functions/private/Install-WinUtilChoco.ps1 | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/functions/private/Install-WinUtilChoco.ps1 b/functions/private/Install-WinUtilChoco.ps1 index 254feba745..5e56d1ad1e 100644 --- a/functions/private/Install-WinUtilChoco.ps1 +++ b/functions/private/Install-WinUtilChoco.ps1 @@ -13,10 +13,11 @@ function Install-WinUtilChoco { if((Test-WinUtilPackageManager -choco) -eq "installed") { return } - + # Install logic taken from https://chocolatey.org/install#individual Write-Host "Seems Chocolatey is not installed, installing now." - Start-Process -FilePath "powershell" -ArgumentList "Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) -ErrorAction Stop" -Wait -NoNewWindow - Start-Process -FilePath "powershell" -ArgumentList "choco feature enable -n allowGlobalConfirmation" -Wait -NoNewWindow + Set-ExecutionPolicy Bypass -Scope Process -Force; + [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; + Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1')) } catch { Write-Host "===========================================" -Foregroundcolor Red