-
Notifications
You must be signed in to change notification settings - Fork 0
/
downloadandinstallcriticalupdates.ps1
111 lines (54 loc) · 3.91 KB
/
downloadandinstallcriticalupdates.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
# Thanks to eHow for providing the script framework
# http://www.ehow.com/how_8724332_use-powershell-run-windows-updates.html
function Get-WIAStatusValue($value)
{
switch -exact ($value)
{
0 {"NotStarted"}
1 {"InProgress"}
2 {"Succeeded"}
3 {"SucceededWithErrors"}
4 {"Failed"}
5 {"Aborted"}
}
}
$needsReboot = $false
$UpdateSession = New-Object -ComObject Microsoft.Update.Session
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
Write-Host " - Searching for Updates"
$SearchResult = $updateSession.createupdatesearcher().search("isInstalled=0 and Type='Software'").Updates | Where-Object { $_.MsrcSeverity -eq "Critical" }
$noUpdateCounter = $SearchResult | Measure-Object | Where {$_.Count -eq 0}
if ($noUpdateCounter.count -eq 0){
Write-Host " - There are no applicable updates."
}
else{
Write-Host " - Found [$($SearchResult.count)] Updates to Download and install"
#Write-Host
foreach($Update in $SearchResult)
{
# Add Update to Collection
$UpdatesCollection = New-Object -ComObject Microsoft.Update.UpdateColl
if ( $Update.EulaAccepted -eq 0 ) { $Update.AcceptEula() }
$UpdatesCollection.Add($Update)
# Download
Write-Host " + Downloading Update $($Update.Title)"
$UpdatesDownloader = $UpdateSession.CreateUpdateDownloader()
$UpdatesDownloader.Updates = $UpdatesCollection
$DownloadResult = $UpdatesDownloader.Download()
$Message = " - Download {0}" -f (Get-WIAStatusValue $DownloadResult.ResultCode)
Write-Host $message
# Install
Write-Host " - Installing Update"
$UpdatesInstaller = $UpdateSession.CreateUpdateInstaller()
$UpdatesInstaller.Updates = $UpdatesCollection
$InstallResult = $UpdatesInstaller.Install()
$Message = " - Install {0}" -f (Get-WIAStatusValue $DownloadResult.ResultCode)
Write-Host $message
Write-Host
$needsReboot = $installResult.rebootRequired
}
#if($needsReboot)
#{
#restart-computer
#}
}