Skip to content

Commit f8b7f62

Browse files
committed
refractor
- refractor scans into one function - create Invoke-WinUtilInitializeModule Function & use it accordingly - refractor Update Installation process - refractor DataGrid Item-management through Itemsource for better Main Thread performance
1 parent 27a97e4 commit f8b7f62

9 files changed

+280
-199
lines changed

config/tweaks.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2417,13 +2417,7 @@
24172417
Set-ItemProperty -Path \"HKLM:\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\SystemRestore\" -Name \"SystemRestorePointCreationFrequency\" -Value \"0\" -Type DWord -Force -ErrorAction Stop | Out-Null
24182418
}
24192419

2420-
# Attempt to load the required module for Get-ComputerRestorePoint
2421-
try {
2422-
Import-Module Microsoft.PowerShell.Management -ErrorAction Stop
2423-
} catch {
2424-
Write-Host \"Failed to load the Microsoft.PowerShell.Management module: $_\"
2425-
return
2426-
}
2420+
Invoke-WinUtilInitializeModule -module \"Microsoft.PowerShell.Management\"
24272421

24282422
# Get all the restore points for the current day
24292423
try {

functions/public/Invoke-WPFButton.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ function Invoke-WPFButton {
6161
"WPFWinUtilInstallPSProfile" {Invoke-WinUtilInstallPSProfile}
6262
"WPFWinUtilUninstallPSProfile" {Invoke-WinUtilUninstallPSProfile}
6363
"WPFWinUtilSSHServer" {Invoke-WPFSSHServer}
64-
"WPFScanUpdates" {Invoke-WPFUpdatesScan}
64+
"WPFScanUpdates" {Invoke-WPFUpdateScan -type "updates"}
6565
"WPFShowUpdateHistory" { Invoke-WPFUpdateHistoryToggle }
6666
"WPFUpdateSelectedInstall" {Invoke-WPFUpdateMGMT -Selected}
6767
"WPFUpdateAllInstall" {Invoke-WPFUpdateMGMT -All}
68-
"WPFUpdateScanHistory" {Invoke-WPFUpdateScanHistory}
68+
"WPFUpdateScanHistory" {Invoke-WPFUpdateScan -type "history"}
6969
}
7070
}

functions/public/Invoke-WPFUpdateHistoryToggle.ps1

Lines changed: 0 additions & 11 deletions
This file was deleted.

functions/public/Invoke-WPFUpdateMGMGT.ps1

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
2+
function Invoke-WinUtilUpdateInstall {
3+
4+
<#
5+
.SYNOPSIS
6+
Installs Windows updates using the Initialize-WindowsUpdateModule and Install-WindowsUpdate cmdlets.
7+
8+
.PARAMETER Params
9+
A hashtable containing the parameters for the Install-WindowsUpdate cmdlet.
10+
11+
#>
12+
13+
param (
14+
[Parameter(Mandatory=$true)]
15+
[hashtable]$Params
16+
)
17+
18+
try {
19+
Initialize-WindowsUpdateModule
20+
Install-WindowsUpdate @Params
21+
Set-WinUtilTaskbaritem -state "None" -overlay "checkmark"
22+
}
23+
catch {
24+
Write-Host "Error installing updates: $_" -ForegroundColor Red
25+
Set-WinUtilTaskbaritem -state "Error" -overlay "warning"
26+
}
27+
}
28+
29+
function Invoke-WPFUpdateMGMT {
30+
31+
<#
32+
.SYNOPSIS
33+
Manages Windows Update Installation
34+
35+
.PARAMETER Selected
36+
A switch parameter that indicates whether to install only selected updates.
37+
38+
.PARAMETER All
39+
A switch parameter that indicates whether to install all available updates.
40+
41+
#>
42+
43+
param (
44+
[switch]$Selected,
45+
[switch]$All
46+
)
47+
48+
# Prepare common installation parameters
49+
$params = @{
50+
Confirm = $false
51+
IgnoreReboot = $true
52+
IgnoreRebootRequired = $true
53+
}
54+
55+
if ($sync["WPFUpdateVerbose"].IsChecked) {
56+
$params['Verbose'] = $true
57+
}
58+
59+
try {
60+
if ($All) {
61+
Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo"
62+
Invoke-WinUtilUpdateControls -state $false
63+
Invoke-WPFRunspace -ArgumentList $params -DebugPreference $DebugPreference -ScriptBlock {
64+
param ($params)
65+
66+
try {
67+
Write-Host "Installing all available updates..."
68+
Invoke-WinUtilUpdateInstall -Params $params
69+
Write-Host "All available updates installed successfully"
70+
$sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" })
71+
} catch {
72+
Write-Host "Error installing updates: $_" -ForegroundColor Red
73+
$sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "Error" -overlay "warning" })
74+
}
75+
}
76+
Invoke-WinUtilUpdateControls -state $true
77+
} elseif ($Selected -and $sync["WPFUpdatesList"].SelectedItems.Count -gt 0) {
78+
Write-Host "Installing selected updates..."
79+
80+
# Get selected updates
81+
$selectedUpdates = $sync["WPFUpdatesList"].SelectedItems | ForEach-Object {
82+
[PSCustomObject]@{
83+
ComputerName = $_.ComputerName
84+
Title = $_.LongTitle
85+
KB = $_.KB
86+
}
87+
}
88+
89+
# Install selected updates
90+
Invoke-WPFRunspace -ParameterList @(("selectedUpdates", $selectedUpdates),("params", $params)) -DebugPreference $DebugPreference -ScriptBlock {
91+
param ($selectedUpdates, $params)
92+
93+
$sync.form.Dispatcher.Invoke([action] {
94+
Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo"
95+
Invoke-WinUtilUpdateControls -state $false
96+
})
97+
98+
foreach ($update in $selectedUpdates) {
99+
Write-Host "Installing update $($update.Title) on $($update.ComputerName)"
100+
101+
# Prepare update-specific parameters
102+
$updateParams = $params.Clone()
103+
$updateParams['ComputerName'] = $update.ComputerName
104+
105+
# Install update based on KB or Title
106+
if ($update.KB) {
107+
Get-WindowsUpdate -KBArticleID $update.KB -Install @updateParams
108+
} else {
109+
Get-WindowsUpdate -Title $update.Title -Install @updateParams
110+
}
111+
}
112+
113+
$sync.form.Dispatcher.Invoke([action] {
114+
Set-WinUtilTaskbaritem -state "None" -overlay "checkmark"
115+
Invoke-WinUtilUpdateControls -state $true
116+
})
117+
Write-Host "Selected updates installed successfully"
118+
}
119+
} else {
120+
Write-Host "No updates selected"
121+
}
122+
123+
} catch {
124+
Write-Host "Error installing updates: $_" -ForegroundColor Red
125+
Set-WinUtilTaskbaritem -state "Error" -overlay "warning"
126+
}
127+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
function Invoke-WPFUpdateHistoryToggle {
2+
<#
3+
.SYNOPSIS
4+
Toggles the visibility of the Windows update history and available updates.
5+
#>
6+
7+
$showHistory = $sync["WPFShowUpdateHistory"].Content -eq "Show History"
8+
9+
$sync["WPFShowUpdateHistory"].Content = if ($showHistory) { "Show available Updates" } else { "Show History" }
10+
$sync["HistoryGrid"].Visibility = if ($showHistory) { "Visible" } else { "Collapsed" }
11+
$sync["UpdatesGrid"].Visibility = if ($showHistory) { "Collapsed" } else { "Visible" }
12+
}
13+
14+
function Invoke-WinUtilUpdateControls {
15+
<#
16+
.SYNOPSIS
17+
Disables or enables the update controls based on the specified state.
18+
19+
.PARAMETER state
20+
The state to set the controls to.
21+
#>
22+
23+
param (
24+
[boolean]$state
25+
)
26+
27+
$sync["WPFScanUpdates"].IsEnabled = $state
28+
$sync["WPFUpdateScanHistory"].IsEnabled = $state
29+
$sync["WPFUpdateSelectedInstall"].IsEnabled = $state
30+
$sync["WPFUpdateAllInstall"].IsEnabled = $state
31+
}
32+
33+
34+
function Invoke-WPFUpdateScan {
35+
<#
36+
.SYNOPSIS
37+
Scans for Windows updates and history and builds the DataGrids for the UI.
38+
39+
.PARAMETER type
40+
The type of scan to perform.
41+
42+
#>
43+
44+
param (
45+
[string]$type
46+
)
47+
48+
Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo"
49+
Invoke-WinUtilUpdateControls -state $false
50+
51+
Invoke-WPFRunspace -ArgumentList $type -DebugPreference $DebugPreference -ScriptBlock {
52+
param ($type)
53+
try {
54+
Invoke-WinUtilInitializeModule -module "PSWindowsUpdate"
55+
switch ($type) {
56+
"updates" {
57+
$sync.form.Dispatcher.Invoke([action] { $sync["WPFUpdatesList"].ItemsSource = $null })
58+
Write-Host "Scanning for Windows updates..."
59+
$updates = Get-WindowsUpdate -ErrorAction SilentlyContinue
60+
Write-Host "Found $($updates.Count) updates."
61+
62+
# Build the list of items first
63+
$items = foreach ($update in $updates) {
64+
[PSCustomObject]@{
65+
LongTitle = $update.Title
66+
ComputerName = $update.ComputerName
67+
KB = $update.KB
68+
Size = $update.Size
69+
Title = $update.Title -replace '\s*\(KB\d+\)', '' -replace '\s*KB\d+\b', ''
70+
Status = "Not Installed"
71+
}
72+
}
73+
74+
$Computers = $updates.ComputerName | Select-Object -Unique
75+
76+
# Update the DataGrid at once
77+
$sync.form.Dispatcher.Invoke([action] {
78+
$sync["WPFUpdatesList"].ItemsSource = $items
79+
$sync["WPFUpdatesList"].Columns[0].Visibility = if ($Computers.Count -gt 1) { "Visible" } else { "Collapsed" }
80+
})
81+
}
82+
"history" {
83+
$sync.form.Dispatcher.Invoke([action] { $sync["WPFUpdateHistory"].ItemsSource = $null })
84+
Write-Host "Scanning for Windows update history..."
85+
$history = Get-WUHistory -Last 50 -ErrorAction Stop
86+
if (!$history) {
87+
Write-Host "No update history available."
88+
return
89+
}
90+
91+
# Build the list of history items first
92+
$items = foreach ($update in $history) {
93+
[PSCustomObject]@{
94+
ComputerName = $update.ComputerName
95+
Result = $update.Result
96+
Title = $update.Title -replace '\s*\(KB\d+\)', '' -replace '\s*KB\d+\b', ''
97+
KB = $update.KB
98+
Date = $update.Date
99+
}
100+
}
101+
102+
$Computers = $history.ComputerName | Select-Object -Unique
103+
104+
# Update the DataGrid at once
105+
$sync.form.Dispatcher.Invoke([action] {
106+
$sync["WPFUpdateHistory"].ItemsSource = $items
107+
$sync["WPFUpdateHistory"].Columns[0].Visibility = if ($Computers.Count -gt 1) { "Visible" } else { "Collapsed" }
108+
})
109+
Write-Host "Scanning for Windows update history completed"
110+
}
111+
}
112+
113+
$sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" })
114+
}
115+
catch {
116+
$sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "Error" -overlay "warning" })
117+
Write-Host "Error during scan: $_" -ForegroundColor Red
118+
} finally {
119+
$sync.form.Dispatcher.Invoke([action] { Invoke-WinUtilUpdateControls -state $true })
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)