-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstallUpdates.ps1
More file actions
41 lines (37 loc) · 1.53 KB
/
installUpdates.ps1
File metadata and controls
41 lines (37 loc) · 1.53 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
Write-Host "Checking for Windows updates"
$UpdateCollection = New-Object -ComObject Microsoft.Update.UpdateColl
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$Session = New-Object -ComObject Microsoft.Update.Session
$Searcher = New-Object -ComObject Microsoft.Update.Searcher
$results = $searcher.search("Type='software' AND IsInstalled = 0 AND IsHidden = 0 AND AutoSelectOnWebSites = 1")
# Install Update
if ($results.Updates.Count -eq 0) {
Write-Host "No updates found"
# no updates.
} else {
# setup update collection
foreach ($update in $results.Updates){
$UpdateCollection.Add($update) | out-null
}
# download update items
Write-Host "Downloading updates"
$Downloader = $Session.CreateUpdateDownloader()
$Downloader.Updates = $UpdateCollection
$Downloader.Download()
# install update items
Write-Host "Installing updates"
$Installer = New-Object -ComObject Microsoft.Update.Installer
$Installer.Updates = $UpdateCollection
$InstallationResult = $Installer.Install()
# Check Result
if ($InstallationResult.ResultCode -eq 2){
Write-Host "Updates installed successfully"
} else {
Write-Host "Some updates could not be installed"
}
if ($InstallationResult.RebootRequired){
Write-Host "System needs to reboot"
# uncomment the following line to automatically reboot the system if a reboot is required after the updates are installed
#shutdown -r -f -t 300 -c "Rebooting in 5 minutes to apply Windows updates"
}
}