-
Notifications
You must be signed in to change notification settings - Fork 2
/
Get-WindowsUpdateError.ps1
158 lines (93 loc) · 5.42 KB
/
Get-WindowsUpdateError.ps1
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
153
154
155
156
157
158
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force -Confirm:$False
Write-Output "[*] Building update log file, please wait as this may take a up to a minute to complete."
$Job = Start-Job -ScriptBlock { Get-WindowsUpdateLog -ErrorVariable ErrorVariable }
$Job | Wait-Job | Remove-Job
Write-Output "[*] Verifying log was updated today"
If ($env:OneDrive) {
$UpdateLog = "$env:OneDrive\Desktop\WindowsUpdate.log"
} Else {
$UpdateLog = "$env:USERPROFILE\Desktop\WindowsUpdate.log"
} # End If Else
Write-Output "[*] Verifying update log was last written too today"
[datetime]$Today = Get-Date
$FileProperties = Get-ChildItem -Path $UpdateLog
If ((Test-Path -Path $UpdateLog) -and ($FileProperties.LastWriteTime.ToShortDateString() -eq ($Today).ToShortDateString())) {
Write-Output "[*] Successfully created Windows Update log file"
$Pattern = 'ERROR'
$ErrorLog = $UpdateLog.Replace("WindowsUpdate.log","WindowsError.log")
Get-Content -Path $UpdateLog | Select-String -Pattern $Pattern | Out-File -FilePath $ErrorLog
$ErrorLogContents = Get-Content -Path $ErrorLog
Do {
$Answer = Read-Host -Prompt "Would you like to view errors from the last 24 hours or the Week? [t/w]"
If ($Answer -like "t*") {
$WriteLine = @()
ForEach ($Line in $ErrorLogContents) {
If ($Line -NotLike "*succeeded with errors = 0*" -and $Line -NotLike "*and error 0") {
Write-Verbose "Checking for entires in the last 24 hours"
Try {
[datetime]$CheckDate = ($Line.ToCharArray() | Select-Object -First 19 ) -Join ''
If (($Null -ne $CheckDate) -and ($CheckDate -gt $Today.AddHours(-24))) {
$WriteLine += $Line
} # End If
} Catch {
Continue
} # End Try Catch
} # End If
} # End ForEach
If (!$WriteLine) {
Write-Host "SUCCESS! No Windows Update errors over the last 24 hours" -ForegroundColor Green
} Else {
$WriteLine
$Answer = Read-Host -Prompt "Based on the error messages above, should we run the Windows Update Troubleshooter? [y/N]"
If ($Answer -like "y*") {
Get-TroubleshootingPack -Path "C:\Windows\Diagnostics\System\WindowsUpdate" | Invoke-TroubleshootingPack -Result "C:\DiagResult"
Write-Output "[*] Troubleshooter results saved to C:\DiagResult"
} # End If
$RunUpdate = Read-Host -Prompt "Would you like to try updating Windows again now? [y/N]"
If ($RunUpdate -like "y*") {
Write-Output "[*] Running Windows Update"
$Updates = Start-WUScan -SearchCriteria "Type='Software' AND IsInstalled=0"
If ($Updates) {
Install-WUUpdates -Updates $Updates
} Else {
Write-Host "Hooray! No more Windows Updates to install" -ForegroundColor Green
} # End Else
} # End If
} # End If Else
} ElseIf ($Answer -like "w*") {
$WriteLine = @()
ForEach ($Line in $ErrorLogContents) {
If ($Line -NotLike "*succeeded with errors = 0*" -and $Line -NotLike "*and error 0") {
Write-Verbose "Checking all the Windows Update error log entries"
$WriteLine += $Line
} # End If
} # End ForEach
If (!$WriteLine) {
Write-Host "SUCCESS! No Windows Update errors over the last 24 hours" -ForegroundColor Green
} Else {
$WriteLine + "`n"
$Answer = Read-Host -Prompt "Based on the error messages above, should we run the Windows Update Troubleshooter? [y/N]"
If ($Answer -like "y*") {
Get-TroubleshootingPack -Path "C:\Windows\Diagnostics\System\WindowsUpdate" | Invoke-TroubleshootingPack -Result "C:\DiagResult"
Write-Output "[*] Troubleshooter results saved to C:\DiagResult"
} # End If
$RunUpdate = Read-Host -Prompt "Would you like to try updating Windows again now? [y/N]"
If ($RunUpdate -like "y*") {
Write-Output "[*] Running Windows Update"
$Updates = Start-WUScan -SearchCriteria "Type='Software' AND IsInstalled=0"
If ($Updates) {
Install-WUUpdates -Updates $Updates
} Else {
Write-Host "Hooray! No more Windows Updates to install" -ForegroundColor Green
} # End Else
} # End If
Write-Output "[*] Script Execution Complete"
} # End If Else
} Else {
Write-Output "You just had to be difficult :) Lets try again"
} # End If ElseIf Else
} Until ($Answer -like "t*" -or $Answer -like "w*") # End Do Until
} Else {
Write-Output "[x] Failed to create Windows Update log file"
Throw "$ErrorVariable"
} # End If Else