Repository navigation
Expand file tree
/
Copy pathWindowsUpdatesRemediation.ps1
More file actions
152 lines (129 loc) · 6.14 KB
/
Copy pathWindowsUpdatesRemediation.ps1
File metadata and controls
152 lines (129 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<#
.SYNOPSIS
This script checks for pending Windows Update packages and performs actions based on the presence of stuck updates.
.DESCRIPTION
The script checks if there are pending Windows Update packages. If stuck updates are present, it proceeds to remove related registry entries and files to resolve the pending updates.
.PARAMETER
None required
.EXAMPLE
.\WindowsUpdateRemediation.ps1
.NOTES
Date Created: 2023-12-12
Author: Ditor Sahiti
#>
# Set log file path
$logFilePath = "C:\Windows\Temp\WindowsUpdateRemediation.log"
# Function to write log entries
function Write-Log {
param(
[string]$Message
)
$TimeStamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
$LogEntry = "$TimeStamp - $Message"
Add-Content -Path $logFilePath -Value $LogEntry
}
# Log script start
Write-Log -Message "Script execution started."
$pendingPackages = dism /online /get-packages /format:table | Select-String "Pending" | ForEach-Object { $_.ToString().Split('|')[0].Trim() }
# Check if $pendingPackages is not null
if ($pendingPackages -ne $null) {
Write-Host "PendingPackages is present. Executing the script."
Write-Log -Message "Pending Windows Update packages found. Script execution started."
# Define registry paths to check
$registryPaths = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing"
# Loop through each registry path
foreach ($path in $registryPaths) {
# Check if the path exists
if (Test-Path $path) {
# Get all subkeys and values recursively
$items = Get-ChildItem -Path $path -Recurse
foreach ($item in $items) {
# Check if the current item is a key
if ($item.PSIsContainer) {
# Get all values in the key
$values = Get-ItemProperty -Path $item.PSPath -ErrorAction SilentlyContinue
foreach ($valueName in $values.PSObject.Properties.Name) {
foreach ($package in $pendingPackages) {
# Check if the value name matches the package name
if ($valueName -like "*$package*") {
# Remove the value
Remove-ItemProperty -Path $item.PSPath -Name $valueName -Force -ErrorAction SilentlyContinue
Write-Host "Removed value $valueName in $($item.PSPath)"
Write-Log -Message "Removed value $valueName in $($item.PSPath)"
}
}
}
# Additional check for the key itself
foreach ($package in $pendingPackages) {
if ($item.Name -like "*$package*") {
# Remove the key
Remove-Item -Path $item.PSPath -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Removed key $($item.PSPath)"
Write-Log -Message "Removed key $($item.PSPath)"
}
}
}
}
}
else {
Write-Host "Path $path does not exist."
Write-Log -Message "Path $path does not exist."
}
}
# Define the paths for the registry keys
$registryPaths = @(
"SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending",
"SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\PendedSessionPackages",
"SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\SessionsPending"
)
# Function to safely check and delete subkeys, then delete the main key
function ModifyAndDeleteRegKey {
param(
[string]$regPath
)
try {
$localMachineKey = [Microsoft.Win32.Registry]::LocalMachine
$regKey = $localMachineKey.OpenSubKey($regPath, $true)
if ($regKey -ne $null) {
# Delete subkeys
$subKeys = $regKey.GetSubKeyNames()
foreach ($subKey in $subKeys) {
$subRegKeyPath = "$regPath\$subKey"
$localMachineKey.DeleteSubKeyTree($subRegKeyPath)
Write-Host "Successfully deleted subkey: $subRegKeyPath"
Write-Log -Message "Successfully deleted subkey: $subRegKeyPath"
}
# Delete the main key
$localMachineKey.DeleteSubKeyTree($regPath)
Write-Host "Successfully deleted main registry key: $regPath"
Write-Log -Message "Successfully deleted main registry key: $regPath"
$regKey.Close()
} else {
Write-Host "Registry key does not exist or cannot be accessed: $regPath"
Write-Log -Message "Registry key does not exist or cannot be accessed: $regPath"
}
} catch {
Write-Host "Failed to delete registry key: $regPath. Error: $_"
Write-Log -Message "Failed to delete registry key: $regPath. Error: $_"
}
}
# Process each registry path
foreach ($path in $registryPaths) {
ModifyAndDeleteRegKey -regPath $path
}
#Check for RebootPendingfilePath Key and remove it if present.
$RebootPendingfilePath = "C:\Windows\Winsxs\Pending.xml"
if (Test-Path $RebootPendingfilePath) {
Remove-Item -Path $RebootPendingfilePath -Force -ErrorAction SilentlyContinue
Write-Host "File removed."
Write-Log -Message "File $RebootPendingfilePath removed."
} else {
Write-Host "Pending.XML File does not exist, no action taken." -ForegroundColor green
Write-Log -Message "Pending.XML File does not exist, no action taken."
}
} else {
write-Host "No Pending Windows Updates found. The script will not be executed."
Write-Log -Message "No Pending Windows Updates found. The script will not be executed."
}
# Log script end
Write-Log -Message "Script execution completed."