diff --git a/functions/private/Get-WinUtilSelectedPackages.ps1 b/functions/private/Get-WinUtilSelectedPackages.ps1 index b9f1e86d..6f8f825c 100644 --- a/functions/private/Get-WinUtilSelectedPackages.ps1 +++ b/functions/private/Get-WinUtilSelectedPackages.ps1 @@ -15,9 +15,9 @@ function Get-WinUtilSelectedPackages ) if ($PackageList.count -eq 1) { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" } } else { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" } } $packages = [System.Collections.Hashtable]::new() diff --git a/functions/private/Hide-WPFInstallAppBusy.ps1 b/functions/private/Hide-WPFInstallAppBusy.ps1 index 35646498..7cf7b7da 100644 --- a/functions/private/Hide-WPFInstallAppBusy.ps1 +++ b/functions/private/Hide-WPFInstallAppBusy.ps1 @@ -4,9 +4,9 @@ function Hide-WPFInstallAppBusy { Hides the busy overlay in the install app area of the WPF form. This is used to indicate that an install or uninstall has finished. #> - $sync.form.Dispatcher.Invoke([action]{ + Invoke-WPFUIThread -ScriptBlock { $sync.InstallAppAreaOverlay.Visibility = [Windows.Visibility]::Collapsed $sync.InstallAppAreaBorder.IsEnabled = $true $sync.InstallAppAreaScrollViewer.Effect.Radius = 0 - }) + } } diff --git a/functions/private/Install-WinUtilProgramChoco.ps1 b/functions/private/Install-WinUtilProgramChoco.ps1 index 3f9aae40..6b706f05 100644 --- a/functions/private/Install-WinUtilProgramChoco.ps1 +++ b/functions/private/Install-WinUtilProgramChoco.ps1 @@ -113,7 +113,7 @@ function Install-WinUtilProgramChoco { [int]$totalPrograms ) $progressState = if ($currentIndex -eq $totalPrograms) { "Normal" } else { "Error" } - $sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state $progressState -value ($currentIndex / $totalPrograms) }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state $progressState -value ($currentIndex / $totalPrograms) } } function Install-ChocoPackage { @@ -234,7 +234,7 @@ function Install-WinUtilProgramChoco { for ($currentIndex = 0; $currentIndex -lt $totalPrograms; $currentIndex++) { $Program = $Programs[$currentIndex] Set-WinUtilProgressBar -label "$Action $($Program)" -percent ($currentIndex / $totalPrograms * 100) - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value ($currentIndex / $totalPrograms)}) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -value ($currentIndex / $totalPrograms)} switch ($Action) { "Install" { diff --git a/functions/private/Install-WinUtilProgramWinget.ps1 b/functions/private/Install-WinUtilProgramWinget.ps1 index 0e6ca4dd..0ebf414c 100644 --- a/functions/private/Install-WinUtilProgramWinget.ps1 +++ b/functions/private/Install-WinUtilProgramWinget.ps1 @@ -117,7 +117,7 @@ Function Install-WinUtilProgramWinget { $Program = $Programs[$i] $result = $false Set-WinUtilProgressBar -label "$Action $($Program)" -percent ($i / $count * 100) - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value ($i / $count)}) + Invoke-WPFUIThread -ScriptBlock{ Set-WinUtilTaskbaritem -value ($i / $count)} $result = switch ($Action) { "Install" {Invoke-Install -Program $Program} diff --git a/functions/private/Invoke-WinUtilExplorerUpdate.ps1 b/functions/private/Invoke-WinUtilExplorerUpdate.ps1 index fe6975af..d51a8133 100644 --- a/functions/private/Invoke-WinUtilExplorerUpdate.ps1 +++ b/functions/private/Invoke-WinUtilExplorerUpdate.ps1 @@ -8,7 +8,7 @@ function Invoke-WinUtilExplorerUpdate { ) if ($action -eq "refresh") { - Invoke-WPFRunspace -DebugPreference $DebugPreference -ScriptBlock { + Invoke-WPFRunspace -ScriptBlock { # Define the Win32 type only if it doesn't exist if (-not ([System.Management.Automation.PSTypeName]'Win32').Type) { Add-Type -TypeDefinition @" diff --git a/functions/private/Invoke-WinUtilFeatureInstall.ps1 b/functions/private/Invoke-WinUtilFeatureInstall.ps1 index 72a22d9a..335e85a5 100644 --- a/functions/private/Invoke-WinUtilFeatureInstall.ps1 +++ b/functions/private/Invoke-WinUtilFeatureInstall.ps1 @@ -10,46 +10,40 @@ function Invoke-WinUtilFeatureInstall { $CheckBox ) - $x = 0 + if($sync.configs.feature.$CheckBox.feature) { + Foreach( $feature in $sync.configs.feature.$CheckBox.feature ) { + try { + Write-Host "Installing $feature" + Enable-WindowsOptionalFeature -Online -FeatureName $feature -All -NoRestart + } catch { + if ($CheckBox.Exception.Message -like "*requires elevation*") { + Write-Warning "Unable to Install $feature due to permissions. Are you running as admin?" + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" } + } else { - $CheckBox | ForEach-Object { - if($sync.configs.feature.$psitem.feature) { - Foreach( $feature in $sync.configs.feature.$psitem.feature ) { - try { - Write-Host "Installing $feature" - Enable-WindowsOptionalFeature -Online -FeatureName $feature -All -NoRestart - } catch { - if ($psitem.Exception.Message -like "*requires elevation*") { - Write-Warning "Unable to Install $feature due to permissions. Are you running as admin?" - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" }) - } else { - - Write-Warning "Unable to Install $feature due to unhandled exception" - Write-Warning $psitem.Exception.StackTrace - } + Write-Warning "Unable to Install $feature due to unhandled exception" + Write-Warning $CheckBox.Exception.StackTrace } } } - if($sync.configs.feature.$psitem.InvokeScript) { - Foreach( $script in $sync.configs.feature.$psitem.InvokeScript ) { - try { - $Scriptblock = [scriptblock]::Create($script) + } + if($sync.configs.feature.$CheckBox.InvokeScript) { + Foreach( $script in $sync.configs.feature.$CheckBox.InvokeScript ) { + try { + $Scriptblock = [scriptblock]::Create($script) - Write-Host "Running Script for $psitem" - Invoke-Command $scriptblock -ErrorAction stop - } catch { - if ($psitem.Exception.Message -like "*requires elevation*") { - Write-Warning "Unable to Install $feature due to permissions. Are you running as admin?" - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" }) - } else { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" }) - Write-Warning "Unable to Install $feature due to unhandled exception" - Write-Warning $psitem.Exception.StackTrace - } + Write-Host "Running Script for $CheckBox" + Invoke-Command $scriptblock -ErrorAction stop + } catch { + if ($CheckBox.Exception.Message -like "*requires elevation*") { + Write-Warning "Unable to Install $feature due to permissions. Are you running as admin?" + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" } + } else { + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" } + Write-Warning "Unable to Install $feature due to unhandled exception" + Write-Warning $CheckBox.Exception.StackTrace } } } - $X++ - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value ($x/$CheckBox.Count) }) } } diff --git a/functions/private/Set-WinUtilProgressbar.ps1 b/functions/private/Set-WinUtilProgressbar.ps1 index e5f21260..2241e053 100644 --- a/functions/private/Set-WinUtilProgressbar.ps1 +++ b/functions/private/Set-WinUtilProgressbar.ps1 @@ -14,11 +14,15 @@ function Set-WinUtilProgressbar{ [int]$Percent ) - $sync.form.Dispatcher.Invoke([action]{$sync.progressBarTextBlock.Text = $label}) - $sync.form.Dispatcher.Invoke([action]{$sync.progressBarTextBlock.ToolTip = $label}) + if($PARAM_NOUI) { + return; + } + + Invoke-WPFUIThread -ScriptBlock {$sync.progressBarTextBlock.Text = $label} + Invoke-WPFUIThread -ScriptBlock {$sync.progressBarTextBlock.ToolTip = $label} if ($percent -lt 5 ) { $percent = 5 # Ensure the progress bar is not empty, as it looks weird } - $sync.form.Dispatcher.Invoke([action]{ $sync.ProgressBar.Value = $percent}) + Invoke-WPFUIThread -ScriptBlock { $sync.ProgressBar.Value = $percent} } diff --git a/functions/private/Show-WPFInstallAppBusy.ps1 b/functions/private/Show-WPFInstallAppBusy.ps1 index 350e3d68..94d0c0f9 100644 --- a/functions/private/Show-WPFInstallAppBusy.ps1 +++ b/functions/private/Show-WPFInstallAppBusy.ps1 @@ -10,12 +10,12 @@ function Show-WPFInstallAppBusy { param ( $text = "Installing apps..." ) - $sync.form.Dispatcher.Invoke([action]{ + Invoke-WPFUIThread -ScriptBlock { $sync.InstallAppAreaOverlay.Visibility = [Windows.Visibility]::Visible $sync.InstallAppAreaOverlay.Width = $($sync.InstallAppAreaScrollViewer.ActualWidth * 0.4) $sync.InstallAppAreaOverlay.Height = $($sync.InstallAppAreaScrollViewer.ActualWidth * 0.4) $sync.InstallAppAreaOverlayText.Text = $text $sync.InstallAppAreaBorder.IsEnabled = $false $sync.InstallAppAreaScrollViewer.Effect.Radius = 5 - }) } +} diff --git a/functions/public/Invoke-WPFFeatureInstall.ps1 b/functions/public/Invoke-WPFFeatureInstall.ps1 index fb329694..348c7a2e 100644 --- a/functions/public/Invoke-WPFFeatureInstall.ps1 +++ b/functions/public/Invoke-WPFFeatureInstall.ps1 @@ -12,21 +12,25 @@ function Invoke-WPFFeatureInstall { return } - $Features = $sync.selectedFeatures - - Invoke-WPFRunspace -ArgumentList $Features -DebugPreference $DebugPreference -ScriptBlock { - param($Features, $DebugPreference) + $handle = Invoke-WPFRunspace -ScriptBlock { + $Features = $sync.selectedFeatures $sync.ProcessRunning = $true if ($Features.count -eq 1) { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" } } else { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" } } - Invoke-WinUtilFeatureInstall $Features + $x = 0 + + $Features | ForEach-Object { + Invoke-WinUtilFeatureInstall $Feature + $X++ + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -value ($x/$CheckBox.Count) } + } $sync.ProcessRunning = $false - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" } Write-Host "===================================" Write-Host "--- Features are Installed ---" diff --git a/functions/public/Invoke-WPFGetInstalled.ps1 b/functions/public/Invoke-WPFGetInstalled.ps1 index 772315df..34cc28d9 100644 --- a/functions/public/Invoke-WPFGetInstalled.ps1 +++ b/functions/public/Invoke-WPFGetInstalled.ps1 @@ -20,13 +20,13 @@ function Invoke-WPFGetInstalled { } $managerPreference = $sync["ManagerPreference"] - Invoke-WPFRunspace -ParameterList @(("managerPreference", $managerPreference),("checkbox", $checkbox)) -DebugPreference $DebugPreference -ScriptBlock { + Invoke-WPFRunspace -ParameterList @(("managerPreference", $managerPreference),("checkbox", $checkbox)) -ScriptBlock { param ( [string]$checkbox, [PackageManagers]$managerPreference ) $sync.ProcessRunning = $true - $sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "Indeterminate" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Indeterminate" } if ($checkbox -eq "winget") { Write-Host "Getting Installed Programs..." @@ -48,6 +48,6 @@ function Invoke-WPFGetInstalled { Write-Host "Done..." $sync.ProcessRunning = $false - $sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "None" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "None" } } } diff --git a/functions/public/Invoke-WPFImpex.ps1 b/functions/public/Invoke-WPFImpex.ps1 index a5bf3183..d005c2fc 100644 --- a/functions/public/Invoke-WPFImpex.ps1 +++ b/functions/public/Invoke-WPFImpex.ps1 @@ -71,8 +71,10 @@ function Invoke-WPFImpex { # $flattenedJson = $jsonFile.PSObject.Properties.Where({ $_.Name -ne "Install" }).ForEach({ $_.Value }) $flattenedJson = $jsonFile Update-WinUtilSelections -flatJson $flattenedJson - # TODO test with toggles - Reset-WPFCheckBoxes -doToggles $true + + if (!$PARAM_NOUI) { + Reset-WPFCheckBoxes -doToggles $true + } } } catch { Write-Error "An error occurred while importing: $_" diff --git a/functions/public/Invoke-WPFInstall.ps1 b/functions/public/Invoke-WPFInstall.ps1 index 7bb0374e..528af38e 100644 --- a/functions/public/Invoke-WPFInstall.ps1 +++ b/functions/public/Invoke-WPFInstall.ps1 @@ -1,13 +1,12 @@ function Invoke-WPFInstall { - param ( - [Parameter(Mandatory=$false)] - [PSObject[]]$PackagesToInstall = $($sync.selectedApps | Foreach-Object { $sync.configs.applicationsHashtable.$_ }) - ) <# .SYNOPSIS Installs the selected programs using winget, if one or more of the selected programs are already installed on the system, winget will try and perform an upgrade if there's a newer version to install. #> + $PackagesToInstall = $sync.selectedApps | Foreach-Object { $sync.configs.applicationsHashtable.$_ } + + if($sync.ProcessRunning) { $msg = "[Invoke-WPFInstall] An Install process is currently running." [System.Windows.MessageBox]::Show($msg, "Winutil", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Warning) @@ -22,8 +21,8 @@ function Invoke-WPFInstall { $ManagerPreference = $sync["ManagerPreference"] - Invoke-WPFRunspace -ParameterList @(("PackagesToInstall", $PackagesToInstall),("ManagerPreference", $ManagerPreference)) -DebugPreference $DebugPreference -ScriptBlock { - param($PackagesToInstall, $ManagerPreference, $DebugPreference) + $handle = Invoke-WPFRunspace -ParameterList @(("PackagesToInstall", $PackagesToInstall),("ManagerPreference", $ManagerPreference)) -ScriptBlock { + param($PackagesToInstall, $ManagerPreference) $packagesSorted = Get-WinUtilSelectedPackages -PackageList $PackagesToInstall -Preference $ManagerPreference @@ -45,12 +44,12 @@ function Invoke-WPFInstall { Write-Host "===========================================" Write-Host "-- Installs have finished ---" Write-Host "===========================================" - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" } } catch { Write-Host "===========================================" Write-Host "Error: $_" Write-Host "===========================================" - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" -overlay "warning" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" -overlay "warning" } } $sync.ProcessRunning = $False } diff --git a/functions/public/Invoke-WPFRunspace.ps1 b/functions/public/Invoke-WPFRunspace.ps1 index 67c01b8f..bccdd5df 100644 --- a/functions/public/Invoke-WPFRunspace.ps1 +++ b/functions/public/Invoke-WPFRunspace.ps1 @@ -27,8 +27,7 @@ function Invoke-WPFRunspace { Param ( $ScriptBlock, $ArgumentList, - $ParameterList, - $DebugPreference + $ParameterList ) # Create a PowerShell instance @@ -41,7 +40,7 @@ function Invoke-WPFRunspace { 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 # Execute the RunspacePool diff --git a/functions/public/Invoke-WPFSSHServer.ps1 b/functions/public/Invoke-WPFSSHServer.ps1 index 503f8677..0ea6de59 100644 --- a/functions/public/Invoke-WPFSSHServer.ps1 +++ b/functions/public/Invoke-WPFSSHServer.ps1 @@ -6,7 +6,7 @@ function Invoke-WPFSSHServer { #> - Invoke-WPFRunspace -DebugPreference $DebugPreference -ScriptBlock { + Invoke-WPFRunspace -ScriptBlock { Invoke-WinUtilSSHServer diff --git a/functions/public/Invoke-WPFUIThread.ps1 b/functions/public/Invoke-WPFUIThread.ps1 new file mode 100644 index 00000000..d5c1a465 --- /dev/null +++ b/functions/public/Invoke-WPFUIThread.ps1 @@ -0,0 +1,21 @@ +function Invoke-WPFUIThread { + <# + + .SYNOPSIS + Creates and runs a task on Winutil's WPF Forms thread. + + .PARAMETER ScriptBlock + The scriptblock to invoke in the thread + #> + + [CmdletBinding()] + Param ( + $ScriptBlock + ) + + if ($PARAM_NOUI) { + return; + } + + $sync.form.Dispatcher.Invoke([action]$ScriptBlock) +} diff --git a/functions/public/Invoke-WPFUnInstall.ps1 b/functions/public/Invoke-WPFUnInstall.ps1 index 188d7d88..308aadab 100644 --- a/functions/public/Invoke-WPFUnInstall.ps1 +++ b/functions/public/Invoke-WPFUnInstall.ps1 @@ -32,8 +32,8 @@ function Invoke-WPFUnInstall { $ManagerPreference = $sync["ManagerPreference"] - Invoke-WPFRunspace -ParameterList @(("PackagesToUninstall", $PackagesToUninstall),("ManagerPreference", $ManagerPreference)) -DebugPreference $DebugPreference -ScriptBlock { - param($PackagesToUninstall, $ManagerPreference, $DebugPreference) + Invoke-WPFRunspace -ParameterList @(("PackagesToUninstall", $PackagesToUninstall),("ManagerPreference", $ManagerPreference)) -ScriptBlock { + param($PackagesToUninstall, $ManagerPreference) $packagesSorted = Get-WinUtilSelectedPackages -PackageList $PackagesToUninstall -Preference $ManagerPreference $packagesWinget = $packagesSorted[[PackageManagers]::Winget] @@ -54,12 +54,12 @@ function Invoke-WPFUnInstall { Write-Host "===========================================" Write-Host "-- Uninstalls have finished ---" Write-Host "===========================================" - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" } } catch { Write-Host "===========================================" Write-Host "Error: $_" Write-Host "===========================================" - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" -overlay "warning" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" -overlay "warning" } } $sync.ProcessRunning = $False diff --git a/functions/public/Invoke-WPFtweaksbutton.ps1 b/functions/public/Invoke-WPFtweaksbutton.ps1 index e6b6db49..2d80b201 100644 --- a/functions/public/Invoke-WPFtweaksbutton.ps1 +++ b/functions/public/Invoke-WPFtweaksbutton.ps1 @@ -25,30 +25,27 @@ function Invoke-WPFtweaksbutton { Write-Debug "Number of tweaks to process: $($Tweaks.Count)" # The leading "," in the ParameterList is necessary because we only provide one argument and powershell cannot be convinced that we want a nested loop with only one argument otherwise - Invoke-WPFRunspace -ParameterList @(,("tweaks",$tweaks)) -DebugPreference $DebugPreference -ScriptBlock { - param( - $tweaks, - $DebugPreference - ) + $handle = Invoke-WPFRunspace -ParameterList @(,("tweaks",$tweaks)) -ScriptBlock { + param($tweaks) Write-Debug "Inside Number of tweaks to process: $($Tweaks.Count)" $sync.ProcessRunning = $true if ($Tweaks.count -eq 1) { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" } } else { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" }) + Invoke-WPFUIThread -ScriptBlock{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" } } # 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) }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -value ($i/$Tweaks.Count) } } Set-WinUtilProgressBar -Label "Tweaks finished" -Percent 100 $sync.ProcessRunning = $false - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" } Write-Host "=================================" Write-Host "-- Tweaks are Finished ---" Write-Host "=================================" diff --git a/functions/public/Invoke-WPFundoall.ps1 b/functions/public/Invoke-WPFundoall.ps1 index 829483e2..6bde6d13 100644 --- a/functions/public/Invoke-WPFundoall.ps1 +++ b/functions/public/Invoke-WPFundoall.ps1 @@ -20,26 +20,26 @@ function Invoke-WPFundoall { return } - Invoke-WPFRunspace -ArgumentList $tweaks -DebugPreference $DebugPreference -ScriptBlock { - param($tweaks, $DebugPreference) + Invoke-WPFRunspace -ArgumentList $tweaks -ScriptBlock { + param($tweaks) $sync.ProcessRunning = $true if ($tweaks.count -eq 1) { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" } } else { - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" } } for ($i = 0; $i -lt $tweaks.Count; $i++) { Set-WinUtilProgressBar -Label "Undoing $($tweaks[$i])" -Percent ($i / $tweaks.Count * 100) Invoke-WinUtiltweaks $tweaks[$i] -undo $true - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value ($i/$tweaks.Count) }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -value ($i/$tweaks.Count) } } Set-WinUtilProgressBar -Label "Undo Tweaks Finished" -Percent 100 $sync.ProcessRunning = $false - $sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" }) + Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" } Write-Host "==================================" Write-Host "--- Undo Tweaks are Finished ---" Write-Host "==================================" diff --git a/functions/public/Invoke-WinUtilAutoRun.ps1 b/functions/public/Invoke-WinUtilAutoRun.ps1 new file mode 100644 index 00000000..022dae22 --- /dev/null +++ b/functions/public/Invoke-WinUtilAutoRun.ps1 @@ -0,0 +1,48 @@ +function Invoke-WinUtilAutoRun { + <# + + .SYNOPSIS + Runs Install, Tweaks, and Features with optional UI invocation. + #> + + function BusyWait { + Start-Sleep -Seconds 5 + while ($sync.ProcessRunning) { + Start-Sleep -Seconds 5 + } + } + + BusyWait + + Write-Host "Applying tweaks..." + Invoke-WPFtweaksbutton + BusyWait + + Write-Host "Applying toggles..." + $handle = Invoke-WPFRunspace -ScriptBlock { + $Toggles = $sync.selectedToggles + Write-Debug "Inside Number of toggles to process: $($Toggles.Count)" + + $sync.ProcessRunning = $true + + for ($i = 0; $i -lt $Tweaks.Count; $i++) { + Invoke-WinUtilTweaks $Toggles[$i] + } + + $sync.ProcessRunning = $false + Write-Host "=================================" + Write-Host "-- Toggles are Finished ---" + Write-Host "=================================" + } + BusyWait + + Write-Host "Applying features..." + Invoke-WPFFeatureInstall + BusyWait + + Write-Host "Installing applications..." + Invoke-WPFInstall + BusyWait + + Write-Host "Done." +} diff --git a/scripts/main.ps1 b/scripts/main.ps1 index 1a253f58..6359933e 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -13,10 +13,14 @@ $maxthreads = [int]$env:NUMBER_OF_PROCESSORS # Create a new session state for parsing variables into our runspace $hashVars = New-object System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList 'sync',$sync,$Null +$debugVar = New-object System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList 'DebugPreference',$DebugPreference,$Null +$uiVar = New-object System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList 'PARAM_NOUI',$PARAM_NOUI,$Null $InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault() # Add the variable to the session state $InitialSessionState.Variables.Add($hashVars) +$InitialSessionState.Variables.Add($debugVar) +$InitialSessionState.Variables.Add($uiVar) # Get every private function and add them to the session state $functions = Get-ChildItem function:\ | Where-Object { $_.Name -imatch 'winutil|WPF' } @@ -55,6 +59,42 @@ class GenericException : Exception { GenericException($Message) : base($Message) {} } +# Load the configuration files + +$sync.configs.applicationsHashtable = @{} +$sync.configs.applications.PSObject.Properties | ForEach-Object { + $sync.configs.applicationsHashtable[$_.Name] = $_.Value +} + +Set-PackageManagerPreference + +if ($PARAM_NOUI) { + Show-CTTLogo + if ($PARAM_CONFIG -and -not [string]::IsNullOrWhiteSpace($PARAM_CONFIG)) { + Write-Host "Running config file tasks..." + Invoke-WPFImpex -type "import" -Config $PARAM_CONFIG + if ($PARAM_RUN) { + Invoke-WinUtilAutoRun + } + else { + Write-Host "Did you forget to add '--Run'?"; + } + $sync.runspace.Dispose() + $sync.runspace.Close() + [System.GC]::Collect() + Stop-Transcript + exit 1 + } + else { + Write-Host "Cannot automatically run without a config file provided." + $sync.runspace.Dispose() + $sync.runspace.Close() + [System.GC]::Collect() + Stop-Transcript + exit 1 + } +} + $inputXML = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^