-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindows - Microsoft Updates (Drivers) Manual.ps1
84 lines (69 loc) · 2.91 KB
/
Windows - Microsoft Updates (Drivers) Manual.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
<#
.NOTES
===========================================================================
Created on: 2022-04-27
Created by: Brian Thorp
===========================================================================
.Description
Adds Microsoft Update from Update Server List
Searches for Updated Drivers
Downloads them if there are any
Installs them if there are any
Checks for reboot needed status
Removes Microsoft Update from Update Server List
#>
# Register Microsoft Update as Source
$UpdateSvc = New-Object -ComObject Microsoft.Update.ServiceManager
$UpdateSvc.AddService2("7971f918-a847-4430-9279-4a52d1efe18d",7,"") | Out-Null
# Get all available Drivers
Write-Host 'Searching for drivers...'
$Session = New-Object -ComObject Microsoft.Update.Session
$Searcher = $Session.CreateUpdateSearcher()
$Searcher.ServiceID = '7971f918-a847-4430-9279-4a52d1efe18d'
<#
https://devblogs.microsoft.com/scripting/hey-scripting-guy-how-can-i-tell-which-software-updates-are-available-via-windows-update/
0 Default = Machine Only
1 Machine Only
#>
$Searcher.SearchScope = 1 # MachineOnly
<#
https://docs.microsoft.com/en-us/windows/win32/wua_sdk/searching--downloading--and-installing-updates
Server Selection
1 WSUS
2 Windows Update
3 Microsoft Update, Store, Other
#>
$Searcher.ServerSelection = 3 # Third Party
$Criteria = "IsInstalled=0 and Type='Driver'"
# Write-Host('Searching Driver-Updates...') -Fore Green
$SearchResult = $Searcher.Search($Criteria)
$Updates = $SearchResult.Updates
#Show available Drivers...
$Updates | Select-Object Title, DriverModel, DriverVerDate, Driverclass, DriverManufacturer | Out-File "C:\ContosoTemp\driverupdates.txt"
Write-Host 'Downloading Drivers...'
# Download Drivers
$UpdatesToDownload = New-Object -Com Microsoft.Update.UpdateColl
$updates | % { $UpdatesToDownload.Add($_) | out-null }
# Write-Host('Downloading Drivers...') -Fore Green
$UpdateSession = New-Object -Com Microsoft.Update.Session
$Downloader = $UpdateSession.CreateUpdateDownloader()
$Downloader.Updates = $UpdatesToDownload
$Downloader.Download()
Write-Host 'Installing Drivers...'
# Install Drivers
$UpdatesToInstall = New-Object -Com Microsoft.Update.UpdateColl
$updates | ForEach-Object { if ($_.IsDownloaded) { $UpdatesToInstall.Add($_) | out-null } }
# Write-Host('Installing Drivers...') -Fore Green
$Installer = $UpdateSession.CreateUpdateInstaller()
$Installer.Updates = $UpdatesToInstall
$InstallationResult = $Installer.Install()
if($InstallationResult.RebootRequired) { $rebootRequired = $True }
else { $rebootRequired = $False }
Write-Host 'Cleaning up...'
# Cleanup
$updateSvc.Services | Where-Object { $_.IsDefaultAUService -eq $false -and $_.ServiceID -eq "7971f918-a847-4430-9279-4a52d1efe18d" } | ForEach-Object { $UpdateSvc.RemoveService($_.ServiceID) }
# Report
if ($rebootRequired)
{
Write-Host -ForegroundColor DarkYellow "Please Reboot"
}