diff --git a/functions/private/Set-WinUtilTaskbarItem.ps1 b/functions/private/Set-WinUtilTaskbarItem.ps1 index 465ca4c3..a3b88d8a 100644 --- a/functions/private/Set-WinUtilTaskbarItem.ps1 +++ b/functions/private/Set-WinUtilTaskbarItem.ps1 @@ -43,44 +43,48 @@ function Set-WinUtilTaskbaritem { [string]$description ) - if ($value) { - $sync["Form"].taskbarItemInfo.ProgressValue = $value - } - - if ($state) { - switch ($state) { - 'None' { $sync["Form"].taskbarItemInfo.ProgressState = "None" } - 'Indeterminate' { $sync["Form"].taskbarItemInfo.ProgressState = "Indeterminate" } - 'Normal' { $sync["Form"].taskbarItemInfo.ProgressState = "Normal" } - 'Error' { $sync["Form"].taskbarItemInfo.ProgressState = "Error" } - 'Paused' { $sync["Form"].taskbarItemInfo.ProgressState = "Paused" } - default { throw "[Set-WinUtilTaskbarItem] Invalid state" } + try { + if ($value) { + $sync["Form"].taskbarItemInfo.ProgressValue = $value } - } - if ($overlay) { - switch ($overlay) { - 'logo' { - $sync["Form"].taskbarItemInfo.Overlay = "$env:LOCALAPPDATA\winutil\cttlogo.png" + if ($state) { + switch ($state) { + 'None' { $sync["Form"].taskbarItemInfo.ProgressState = "None" } + 'Indeterminate' { $sync["Form"].taskbarItemInfo.ProgressState = "Indeterminate" } + 'Normal' { $sync["Form"].taskbarItemInfo.ProgressState = "Normal" } + 'Error' { $sync["Form"].taskbarItemInfo.ProgressState = "Error" } + 'Paused' { $sync["Form"].taskbarItemInfo.ProgressState = "Paused" } + default { throw "[Set-WinUtilTaskbarItem] Invalid state" } } - 'checkmark' { - $sync["Form"].taskbarItemInfo.Overlay = "$env:LOCALAPPDATA\winutil\checkmark.png" - } - 'warning' { - $sync["Form"].taskbarItemInfo.Overlay = "$env:LOCALAPPDATA\winutil\warning.png" - } - 'None' { - $sync["Form"].taskbarItemInfo.Overlay = $null - } - default { - if (Test-Path $overlay) { - $sync["Form"].taskbarItemInfo.Overlay = $overlay + } + + if ($overlay) { + switch ($overlay) { + 'logo' { + $sync["Form"].taskbarItemInfo.Overlay = "$env:LOCALAPPDATA\winutil\cttlogo.png" + } + 'checkmark' { + $sync["Form"].taskbarItemInfo.Overlay = "$env:LOCALAPPDATA\winutil\checkmark.png" + } + 'warning' { + $sync["Form"].taskbarItemInfo.Overlay = "$env:LOCALAPPDATA\winutil\warning.png" + } + 'None' { + $sync["Form"].taskbarItemInfo.Overlay = $null + } + default { + if (Test-Path $overlay) { + $sync["Form"].taskbarItemInfo.Overlay = $overlay + } } } } - } - if ($description) { - $sync["Form"].taskbarItemInfo.Description = $description + if ($description) { + $sync["Form"].taskbarItemInfo.Description = $description + } + } catch { + #nothing } } diff --git a/functions/public/Invoke-WPFRunspace.ps1 b/functions/public/Invoke-WPFRunspace.ps1 index dfb3a6cd..a462586c 100644 --- a/functions/public/Invoke-WPFRunspace.ps1 +++ b/functions/public/Invoke-WPFRunspace.ps1 @@ -25,24 +25,28 @@ function Invoke-WPFRunspace { $DebugPreference ) - # Create a PowerShell instance - $script:powershell = [powershell]::Create() + if ($PARAM_RUN) { + & $ScriptBlock @ArgumentList @DebugPreference + } else { + # Create a PowerShell instance + $script:powershell = [powershell]::Create() - # Add Scriptblock and Arguments to runspace - $script:powershell.AddScript($ScriptBlock) - $script:powershell.AddArgument($ArgumentList) - $script:powershell.AddArgument($DebugPreference) # Pass DebugPreference to the script block - $script:powershell.RunspacePool = $sync.runspace + # Add Scriptblock and Arguments to runspace + $script:powershell.AddScript($ScriptBlock) + $script:powershell.AddArgument($ArgumentList) + $script:powershell.AddArgument($DebugPreference) # Pass DebugPreference to the script block + $script:powershell.RunspacePool = $sync.runspace - # Execute the RunspacePool - $script:handle = $script:powershell.BeginInvoke() + # Execute the RunspacePool + $script:handle = $script:powershell.BeginInvoke() - # Clean up the RunspacePool threads when they are complete, and invoke the garbage collector to clean up the memory - if ($script:handle.IsCompleted) { - $script:powershell.EndInvoke($script:handle) - $script:powershell.Dispose() - $sync.runspace.Dispose() - $sync.runspace.Close() - [System.GC]::Collect() + # Clean up the RunspacePool threads when they are complete, and invoke the garbage collector to clean up the memory + if ($script:handle.IsCompleted) { + $script:powershell.EndInvoke($script:handle) + $script:powershell.Dispose() + $sync.runspace.Dispose() + $sync.runspace.Close() + [System.GC]::Collect() + } } } diff --git a/scripts/main.ps1 b/scripts/main.ps1 index bfc05c52..e92e1a68 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -146,12 +146,60 @@ Invoke-WPFRunspace -ScriptBlock { } | Out-Null #=========================================================================== -# Setup and Show the Form +# Print Logo into Console #=========================================================================== -# Print the logo Invoke-WPFFormVariables +#=========================================================================== +# Run Automation +#=========================================================================== + +if ($PARAM_CONFIG) { + if ($PARAM_RUN) { + $installConfig = Get-Content $PARAM_CONFIG -Raw | ConvertFrom-Json + if ($installConfig.WPFInstall) { + write-host "Installing Programs" + + # Create a new array to hold the combined install configurations + $combinedInstallConfig = @() + + # Iterate over each WPFInstall entry + for ($i = 0; $i -lt $installConfig.WPFInstall.Count; $i++) { + $wpfInstallEntry = $installConfig.WPFInstall[$i] + $installEntry = $installConfig.Install[$i] + + # Create a new object with the combined values + $combinedEntry = @{ + Name = $wpfInstallEntry + Winget = $installEntry.winget + Choco = $installEntry.choco + } + + # Add the combined entry to the array + $combinedInstallConfig += $combinedEntry + } + + # Invoke the WPFInstall function with the combined configuration + Invoke-WPFInstall -InstallConfig $combinedInstallConfig + } + if ($installConfig.WPFTweaks) { + write-host "Running Tweaks" + Invoke-WPFtweaksbutton -TweaksConfig $installConfig.WPFTweaks + } + if ($installConfig.WPFFeature) { + write-host "Installing Features" + Invoke-WPFFeatureInstall -FeatureConfig $installConfig.WPFFeature + } + } else { + Invoke-WPFImpex -type "import" -Config $PARAM_CONFIG + } +} + +#=========================================================================== +# Setup and Show the Form +#=========================================================================== + # Progress bar in taskbaritem > Set-WinUtilProgressbar $sync["Form"].TaskbarItemInfo = New-Object System.Windows.Shell.TaskbarItemInfo Set-WinUtilTaskbaritem -state "None" @@ -318,49 +366,6 @@ Add-Type @" Invoke-WPFTab "WPFTab1BT" $sync["Form"].Focus() - - # maybe this is not the best place to load and execute config file? - # maybe community can help? - if ($PARAM_CONFIG) { - if ($PARAM_RUN) { - $installConfig = Get-Content $PARAM_CONFIG -Raw | ConvertFrom-Json - if ($installConfig.WPFTweaks) { - write-host "Running Tweaks" - Invoke-WPFtweaksbutton -TweaksConfig $installConfig.WPFTweaks - } - if ($installConfig.WPFFeature) { - write-host "Installing Features" - Invoke-WPFFeatureInstall -FeatureConfig $installConfig.WPFFeature - } - if ($installConfig.WPFInstall) { - write-host "Installing Programs" - - # Create a new array to hold the combined install configurations - $combinedInstallConfig = @() - - # Iterate over each WPFInstall entry - for ($i = 0; $i -lt $installConfig.WPFInstall.Count; $i++) { - $wpfInstallEntry = $installConfig.WPFInstall[$i] - $installEntry = $installConfig.Install[$i] - - # Create a new object with the combined values - $combinedEntry = @{ - Name = $wpfInstallEntry - Winget = $installEntry.winget - Choco = $installEntry.choco - } - - # Add the combined entry to the array - $combinedInstallConfig += $combinedEntry - } - - # Invoke the WPFInstall function with the combined configuration - Invoke-WPFInstall -InstallConfig $combinedInstallConfig - } - } else { - Invoke-WPFImpex -type "import" -Config $PARAM_CONFIG - } - } }) # Load Checkboxes and Labels outside of the Filter function only once on startup for performance reasons @@ -535,5 +540,8 @@ $sync["SponsorMenuItem"].Add_Click({ $Height = $sync.configs.themes.$ctttheme.CustomDialogHeight Show-CustomDialog -Message $authorInfo -Width $Width -Height $Height -FontSize $FontSize -HeaderFontSize $HeaderFontSize -IconSize $IconSize -EnableScroll $true }) -$sync["Form"].ShowDialog() | out-null -Stop-Transcript + +if (!$PARAM_RUN) { + $sync["Form"].ShowDialog() | out-null + Stop-Transcript +}