-
Notifications
You must be signed in to change notification settings - Fork 0
/
nvidiadrivercheck.psm1
279 lines (225 loc) · 9.69 KB
/
nvidiadrivercheck.psm1
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#Requires -Module AngleParse
# To get AngleParse - see https://github.com/kamome283/AngleParse
# Example usage:
#
# $ProductType = "GeForce"
# $ProductSeries = "GeForce RTX 30 Series"
# $Product = "GeForce RTX 3080"
# $OperatingSystem = "Windows 10 64-bit"
# $DownloadType = "Game Ready Driver (GRD)"
# $Language = "English (US)"
# Import-Module -Name ".\nvidiadrivercheck.psm1" -ArgumentList "NVIDIA GeForce RTX 3080", $ProductType, $ProductSeries, $Product, $OperatingSystem, $DownloadType, $Language
param(
# Supply the Devicename of your NVIDIA graphic card as reported by
# Get-CimInstance win32_pnpSignedDriver | Select-Object Devicename
[parameter(Position=0,Mandatory=$true)][string]$NvidiaDeviceName,
# Supply the string-values for the all the options for your card in the dropdown
# at https://www.nvidia.com/Download/index.aspx
[parameter(Position=1,Mandatory=$true)][string]$ProductType,
[parameter(Position=2,Mandatory=$true)][string]$ProductSeries,
[parameter(Position=3,Mandatory=$true)][string]$Product,
[parameter(Position=4,Mandatory=$true)][string]$OperatingSystem,
[parameter(Position=5,Mandatory=$true)][string]$DownloadType,
[parameter(Position=6,Mandatory=$true)][string]$Language
)
function Get-SelectOptionValue() {
param (
# HTML to look for select in
[Parameter(Mandatory)]
[String]
$Html,
# HTML id of select
[Parameter(Mandatory)]
[String]
$SelectId,
# text value of select option to return value for
[Parameter(Mandatory)]
[String]
$OptionText
)
return (($HTML | Select-HtmlContent "#$SelectId", ([AngleParse.Attr]::Element)).Options | Where-Object { $_.Text -eq $OptionText }).Value
}
function Get-ApiLookupValue(){
param (
[Parameter(Mandatory)]
[String]
$Page,
[Parameter(Mandatory)]
[String]
$Name
)
return ($Page | Select-Xml -XPath "//LookupValue[Name='$Name']").Node.Value
}
$NvidiaDownloadPage = "https://www.nvidia.com/Download/index.aspx"
$NvidiaSearchDriverUrlFormatString = "https://www.nvidia.com/Download/processDriver.aspx?psid={0}&pfid={1}&rpf=1&osid={2}&lid={3}&lang=en-us&ctk=0&dtid={4}&dtcid=1"
$ApiLookupPage = "https://www.nvidia.com/Download/API/lookupValueSearch.aspx?TypeId={0}"
function Find-NvidiaDriverSearchPage()
{
[OutputType([String])]
Param()
$ptPage = Invoke-WebRequest -Uri ($ApiLookupPage -f 1)
$pt = Get-ApiLookupValue -Page $ptPage -Name $ProductType
Write-Debug "Found value $pt for $ProductType"
if(!$pt)
{
Throw "Found no value for $ProductType"
}
$psPage = Invoke-WebRequest -Uri ("$ApiLookupPage&ParentID=$pt" -f 2)
$ps = Get-ApiLookupValue -Page $psPage -Name $ProductSeries
Write-Debug "Found value $ps for $ProductSeries"
if(!$ps)
{
Throw "Found no value for $ProductSeries"
}
$pPage = Invoke-WebRequest -Uri ("$ApiLookupPage&ParentID=$ps" -f 3)
$p = Get-ApiLookupValue -Page $pPage -Name $Product
Write-Debug "Found value $p for $Product"
if(!$p)
{
Throw "Found no value for $Product"
}
$osPage = Invoke-WebRequest -Uri ("$ApiLookupPage&ParentID=$ps" -f 4)
$os = Get-ApiLookupValue -Page $osPage -Name $OperatingSystem
Write-Debug "Found value $os for $OperatingSystem"
if(!$os)
{
Throw "Found no value for $OperatingSystem"
}
$downloadPage = Invoke-WebRequest -Uri $NvidiaDownloadPage
$dt = Get-SelectOptionValue -Html $downloadPage -SelectId "ddlDownloadTypeCrdGrd" -OptionText $DownloadType
Write-Debug "Found value $dt for $DownloadType"
if(!$dt)
{
Throw "Found no value for $DownloadType"
}
$lPage = Invoke-WebRequest -Uri ("$ApiLookupPage&ParentID=$ps" -f 5)
$l = Get-ApiLookupValue -Page $lPage -Name $Language
Write-Debug "Found value $l for $Language"
if(!$l)
{
Throw "Found no value for $Language"
}
$completeSearchString = $NvidiaSearchDriverUrlFormatString -f $ps,$p,$os,$dt,$l
Write-Debug "Constructed NVIDIA driver search URI: $completeSearchString"
return $completeSearchString
}
function Get-NvidiaInstalledVersion{
[OutputType([int])]
Param()
$installedDriverVersion = (Get-CimInstance win32_pnpSignedDriver | Select-Object Devicename, driverversion | Where-Object { $_.devicename -like $NvidiaDeviceName }).driverversion;
# results in smth like
# 21.21.13.7557
# now we pick out the last five digits, which match the ones reported by the driver-version in Windows
$idvr = ($installedDriverVersion -replace "\.");
$installedString = $idvr.Substring($idvr.Length - 5);
$installed = $installedString -as [int]
if($installed -is [int]){
return $installed
}
$ErrorMessage = New-Object System.InvalidOperationException "Driver version parsing failed; best effort found '$installedString', which could not be parsed as a version number."
Throw $ErrorMessage
}
function Get-NvidiaAvailableVersion{
[OutputType([int])]
Param ([string] $driverPage)
$page = Invoke-WebRequest $driverPage
# following results in smth like
# 375.57 <B><SUP>WHQL</SUP></B>
$avaitrimmed = ($page | Select-HtmlContent "#tdVersion").trim()
# now we pick the five digits
$avainumber = ($avaitrimmed -replace "\.").substring(0,5)
$available = $avainumber -as [int]
if($available -is [int]){
return $available
}
$ErrorMessage = New-Object System.InvalidOperationException "Screen-scraping for available version failed. Best effort found '$avaitrimmed', which could not be parsed as a version-number."
Throw $ErrorMessage
}
function Get-DirectDownloadLink{
[OutputType([string])]
Param ([string] $driverDownloadPage)
Write-Debug "Accessing $driverDownloadPage to get link to confirmation page"
$downloadPage = Invoke-WebRequest $driverDownloadPage
#get confirmation page link
$confirmationUrl = "https://www.nvidia.com/" + ($downloadPage | Select-HtmlContent "#lnkDwnldBtn", ([AngleParse.Attr]::Href))
Write-Debug "Found URL for confirmation page: $confirmationUrl"
$confirmationPage = Invoke-WebRequest $confirmationUrl
$href = $confirmationPage | Select-HtmlContent "#mainContent > table > tbody > tr > td > a", ([AngleParse.Attr]::Href)
$fixupHref = "https:$href"
Write-Debug "Found URL for drivers at: $fixupHref"
return $fixupHref
}
function Get-Driver{
[OutputType([string])]
param (
[Parameter(Mandatory=$true)]
[string]
$DownloadUrl,
[Parameter(Mandatory=$true)]
[string]
$DownloadDirectory
)
$filename = $downloadUrl.Split("/")[-1]
$downloadPath = Join-Path -Path $downloadDirectory -ChildPath $filename
Start-BitsTransfer $downloadUrl $downloadPath
return $downloadPath
}
<#
.Synopsis
Checks local version of NVIDIA display driver version (for a graphics driver given as argument for the module)
against the version available on NVIDIAs website (against a driver search URI given as argument for the module).
WARNING - relies on undocumented APIs and screenscraping, FTW!
.Description
Checks local version of NVIDIA display driver version (for a graphics driver given as argument for the module)
against the version available on NVIDIAs website (against a driver search URI given as argument for the module).
If a possible new version is found, by default the download URL for the drivers are copied to the
clipboard for manual download and installation. If the -Download switch is given, the drivers are downloaded
directly to your Downloads folder.
WARNING - relies on undocumented APIs and screenscraping, FTW!
.Example
Test-NvidiaDriver
Checks driver version with minimal output. If new drivers are found, the download URL for the drivers are copied to the
clipboard for manual download and installation.
.Example
Test-NvidiaDriver -Debug
Checks driver version with debug output. If new drivers are found, the download URL for the drivers are copied to the
clipboard for manual download and installation.
.Example
Test-NvidiaDriver -Download
Checks driver version and downloads drivers to Downloads folder, if new are found.
#>
function Test-NvidiaDriver{
[CmdletBinding()]
param (
# If given, downloads the drivers directly to your Downloads folder
[Parameter()]
[Switch]
$Download = $false
)
$driverSearchPage = Find-NvidiaDriverSearchPage
$driverPage = (Invoke-WebRequest -Uri $driverSearchPage).Content
$avai = Get-NvidiaAvailableVersion -driverPage $driverPage
Write-Debug "Found available version: $avai"
$inst = Get-NvidiaInstalledVersion
Write-Debug "Found install version: $inst"
if($avai -gt $inst){
Write-Host "It looks like there's a more recent driver available."
$driverDownloadUrl = Get-DirectDownloadLink $driverPage
if($Download){
$downloadsDirectory = (New-Object -ComObject Shell.Application).NameSpace('shell:Downloads').Self.Path
Write-Host "Downloading to your Downloads folder at $downloadsDirectory"
$downloadPath = Get-Driver -DownloadUrl $driverDownloadUrl -DownloadDirectory $downloadsDirectory
Write-Host "File downloaded to $downloadPath"
Set-Clipboard $downloadPath
Write-Host "File path copied to clipboard."
}
else{
Set-Clipboard $driverDownloadUrl
Write-Host "Download url copied to clipboard; enter in browser to download manually."
}
}
else {
Write-Host "It looks like your drivers are up-to-date."
}
}
Export-ModuleMember -function Test-NvidiaDriver