-
Notifications
You must be signed in to change notification settings - Fork 14
/
Update-Drivers.ps1
238 lines (161 loc) · 8.41 KB
/
Update-Drivers.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<#
.SYNOPSIS
This cmdlet is used to quickly and easily update all drivers in "Device Manager". By default this cmdlet is looking to update all your drivers. This does have the functionality to list all available driver updates which in turn then the allows you to install one more of them. For ease of use I have also added a switch parameter to exclude Firmware driver upgrades to prevent issues on devices whose firmware refuses to upgrade without damaging the device.
.PARAMETER Name
# NOT AVAILABLE JUST YET I AM STILL WORKING ON THIS
Specifies an array of names of driver updates to download
.PARAMETER ListAll
Indicates that you want to get a list of all available driver updates.
.PARAMETER SkipFirmware
Indicates you wish to install all available driver updates excluding Firmware
.DESCRIPTION
Rather than opening Device Manager (Ctrl + x, M) and going through each individual driver manually to check for upgrades, this cmdlet does it automatically. You can list available updates, install one or more of the listed updates or install all updates excluding firmware updates.
.EXAMPLE
Update-Drivers
# This example downloads and install all available driver updates
.EXAMPLE
Update-Drivers -ListAll
# This example lists all available driver updates in a table
.EXAMPLE
Update-Drivers -ListAll -ExcludeFirmware
# This example lists all available driver updates in a table and excludes the firmware drivers.
.EXAMPLE
Update-Drivers -ExcludeFirmware
# This example installs all available drivers excluding firmware drivers
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: rosborne@osbornepro.com
.LINK
https://osbornepro.com
https://writeups.osbornepro.com
https://btpssecpack.osbornepro.com
https://github.com/tobor88
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
https://www.hackthebox.eu/profile/52286
https://rzander.azurewebsites.net/script-to-install-or-update-drivers-directly-from-microsoft-catalog/
.INPUTS
None
.OUTPUTS
None, Microsoft.PowerShell.Commands.Internal.Format
By default, this cmdlet does not return an object. If you use the -ListAll switch parameter a Microsoft.PowerShell.Commands.Internal.Format object will be returned
#>
Function Update-Drivers {
[CmdletBinding(DefaultParameterSetName="UpdateAll")]
param(
[Parameter(
ParameterSetName="Install",
Position=0,
Mandatory=$True,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True,
HelpMessage="`n[H] After using the -ListAll switch parameter you can use the Title value to choose an array of updates to install. Separate multiple values with a comma.`n[E] Example: '<I had not driver updates needed at the writing of this module so I don't have an example yet>'")] # End Parameter
[ValidateNotNullOrEmpty()]
[Alias('Title','KB')]
[String[]]$Name,
[Parameter(
ParameterSetName='List',
Mandatory=$False)] # End Parameter
[Switch][Bool]$ListAll,
[Parameter(
ParameterSetName="List",
Mandatory=$False)] # End Parameter
[Parameter(
ParameterSetName="UpdateAll",
Mandatory=$False)] # End Parameter
[Switch][Bool]$SkipFirmware
) # End param
BEGIN {
Write-Verbose "Verifying permissions"
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
If ($IsAdmin) {
Write-Verbose "Permissions verified, continuing execution"
} # End If
Else {
Throw "Insufficient permissions detected. Run this cmdlet in an adminsitrative prompt."
} # End Else
Write-Verbose "Adding source to Microsoft Update"
$UpdateSvc = New-Object -ComObject Microsoft.Update.ServiceManager
$UpdateSvc.AddService2("7971f918-a847-4430-9279-4a52d1efe18d",7,"")
Write-Verbose "Building searcher for driver updates"
$Session = New-Object -ComObject Microsoft.Update.Session
$Searcher = $Session.CreateUpdateSearcher()
# (New-Object -ComObject Microsoft.Update.ServiceManager).Services | Select-Object -Property ServiceID
$Searcher.ServiceID = "7971f918-a847-4430-9279-4a52d1efe18d"
$Searcher.SearchScope = 1 # MachineOnly
$Searcher.ServerSelection = 3 # Third Party
$Criteria = "IsInstalled=0 and Type='Driver'"
} # End BEGIN
PROCESS {
Write-Output "[*] Searching Driver-Updates..."
$SearchResult = $Searcher.Search($Criteria)
$Updates = $SearchResult.Updates
If ($Updates.Count -eq 0) {
Write-Output "[*] All drivers are up to date"
} # End If
ElseIf (($Updates.Count -gt 0) -and ($SearchResult.Updates.Where({$_.Filter -like $Name}))) {
Write-Verbose "Searching for $Name in available updates"
} # End Else
ElseIf ($Updates.Count -gt 0) {
$UpdateDriverList = Select-Object -InputObject $Updates -Property "Title","DriverModel","DriverVerDate","Driverclass","DriverManufacturer" | Format-Table -AutoSize -Wrap
} # End If
Else {
Write-Output "[*] All drivers are up to date"
Write-Output "[*] Returning Microsoft Update registered sources to their original states"
$ReferenceObj = $UpdateSvc.Services | Where-Object { $_.IsDefaultAUService -eq $False -and $_.ServiceID -eq "7971f918-a847-4430-9279-4a52d1efe18d" }
$ReferenceObj | ForEach-Object -Process { $UpdateSvc.RemoveService($_.ServiceID) }
Exit 0
} # End Else
If ($ListAll.IsPresent) {
Write-Output "[*] The below table lists available driver updates"
$UpdateDriverList
} # End If
Else {
If ($PSCmdlet.ParameterSetName -eq "Install") {
Write-Verbose "[*] Downloading $Name"
Write-Verbose "[*] Installing $Name"
} # End If
Else {
Write-Output "[*] Downloading Drivers..."
$UpdatesToDownload = New-Object -ComObject Microsoft.Update.UpdateColl
ForEach-Object -InputObject $Updates -Process { $UpdatesToDownload.Add($_) | Out-Null }
Write-Verbose "Starting download"
$UpdateSession = New-Object -Com Microsoft.Update.Session
$Downloader = $UpdateSession.CreateUpdateDownloader()
$Downloader.Updates = $UpdatesToDownload
$Downloader.Download()
Write-Output "[*] Installing Drivers..."
$UpdatesToInstall = New-Object -Com Microsoft.Update.UpdateColl
$Updates | ForEach-Object { If ($_.IsDownloaded) { $UpdatesToInstall.Add($_) | Out-Null } }
Write-Output "Starting Install..."
$Installer = $UpdateSession.CreateUpdateInstaller()
$Installer.Updates = $UpdatesToInstall
$InstallationResult = $Installer.Install()
If ($InstallationResult.RebootRequired) {
Write-Output "[*] Reboot required to finish updating"
$Selection = Read-Host -Prompt "[!] Would you like to restart the computer now? [y|N]"
If (($Selection -like "y") -or ($Selection -like "yes")) {
Write-Output "[*] Returning Microsoft Update registered sources to their original states"
$ReferenceObj = $UpdateSvc.Services | Where-Object { $_.IsDefaultAUService -eq $False -and $_.ServiceID -eq "7971f918-a847-4430-9279-4a52d1efe18d" }
$ReferenceObj | ForEach-Object -Process { $UpdateSvc.RemoveService($_.ServiceID) }
Restart-Computer -Force
} # End If
Else {
Write-Output "[*] To finish installing updates you still need to restart the device"
} # End Else
} # End If
Else {
Write-Output "[*] All drivers are now up to date"
} # End Else
} # End Else
} # End Else
} # End PROCESS
END {
Write-Output "[*] Returning Microsoft Update registered sources to their original states"
$ReferenceObj = $UpdateSvc.Services | Where-Object { $_.IsDefaultAUService -eq $False -and $_.ServiceID -eq "7971f918-a847-4430-9279-4a52d1efe18d" }
$ReferenceObj | ForEach-Object -Process { $UpdateSvc.RemoveService($_.ServiceID) }
} # End END
} # End Function Update-Drivers