-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWindows Updates.ps1
92 lines (85 loc) · 3.75 KB
/
Windows Updates.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
$ErrorActionPreference = "SilentlyContinue"
If ($Error) {
$Error.Clear()
}
$Today = Get-Date
Set-Service wuauserv -startupType Manual
$UpdateCollection = New-Object -ComObject Microsoft.Update.UpdateColl
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$Session = New-Object -ComObject Microsoft.Update.Session
Write-Host
Write-Host "`t Initialising and Checking for Applicable Updates. Please wait ..." -ForeGroundColor "Yellow"
$Result = $Searcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
If ($Result.Updates.Count -EQ 0) {
Write-Host "`t There are no applicable updates for this computer."
}
Else {
$ReportFile = $Env:ComputerName + "_Report.txt"
If (Test-Path $ReportFile) {
Remove-Item $ReportFile
}
New-Item $ReportFile -Type File -Force -Value "Windows Update Report For Computer: $Env:ComputerName`r`n" | Out-Null
Add-Content $ReportFile "Report Created On: $Today`r"
Add-Content $ReportFile "==============================================================================`r`n"
Write-Host "`t Preparing List of Applicable Updates For This Computer ..." -ForeGroundColor "Yellow"
Add-Content $ReportFile "List of Applicable Updates For This Computer`r"
Add-Content $ReportFile "------------------------------------------------`r"
For ($Counter = 0; $Counter -LT $Result.Updates.Count; $Counter++) {
$DisplayCount = $Counter + 1
$Update = $Result.Updates.Item($Counter)
$UpdateTitle = $Update.Title
Add-Content $ReportFile "`t $DisplayCount -- $UpdateTitle"
}
$Counter = 0
$DisplayCount = 0
Add-Content $ReportFile "`r`n"
Write-Host "`t Initialising Download of Applicable Updates ..." -ForegroundColor "Yellow"
Add-Content $ReportFile "Initialising Download of Applicable Updates"
Add-Content $ReportFile "------------------------------------------------`r"
$Downloader = $Session.CreateUpdateDownloader()
$UpdatesList = $Result.Updates
For ($Counter = 0; $Counter -LT $Result.Updates.Count; $Counter++) {
$UpdateCollection.Add($UpdatesList.Item($Counter)) | Out-Null
$ShowThis = $UpdatesList.Item($Counter).Title
$DisplayCount = $Counter + 1
Add-Content $ReportFile "`t $DisplayCount -- Downloading Update $ShowThis `r"
$Downloader.Updates = $UpdateCollection
$Track = $Downloader.Download()
If (($Track.HResult -EQ 0) -AND ($Track.ResultCode -EQ 2)) {
Add-Content $ReportFile "`t Download Status: SUCCESS"
}
Else {
Add-Content $ReportFile "`t Download Status: FAILED With Error -- $Error()"
$Error.Clear()
Add-content $ReportFile "`r"
}
}
$Counter = 0
$DisplayCount = 0
Write-Host "`t Starting Installation of Downloaded Updates ..." -ForegroundColor "Yellow"
Add-Content $ReportFile "`r`n"
Add-Content $ReportFile "Installation of Downloaded Updates"
Add-Content $ReportFile "------------------------------------------------`r"
$Installer = New-Object -ComObject Microsoft.Update.Installer
For ($Counter = 0; $Counter -LT $UpdateCollection.Count; $Counter++) {
$Track = $Null
$DisplayCount = $Counter + 1
$WriteThis = $UpdateCollection.Item($Counter).Title
Add-Content $ReportFile "`t $DisplayCount -- Installing Update: $WriteThis"
$Installer.Updates = $UpdateCollection
Try {
$Track = $Installer.Install()
Add-Content $ReportFile "`t Update Installation Status: SUCCESS"
}
Catch {
[System.Exception]
Add-Content $ReportFile "`t Update Installation Status: FAILED With Error -- $Error()"
$Error.Clear()
Add-content $ReportFile "`r"
}
}
}
Set-Service wuauserv -startupType Disabled
stop-service wuauserv
Get-ScheduledTask -TaskPath "\Microsoft\Windows\WindowsUpdate\" | Disable-ScheduledTask
Get-ScheduledTask -TaskPath "\Microsoft\Windows\UpdateOrchestrator\" | Disable-ScheduledTask