使用PowerShell,是否有一种更轻松或更好的优化方法将下载链接获取到最新的NVIDIA驱动程序?

发布于 2025-01-26 18:46:54 字数 2300 浏览 1 评论 0原文

大家好,我能够获得此代码,以获取Quadro P1000的NVIDIA驱动程序的最新下载链接。
我想知道,是否有更好的方法可以做到这一点。

这是我的代码:


#Get Nvidia Drivers
            #This next section is JUST TO get the LINK to the LATEST driver from Nvidia
            #WebContent gets the links that are "href" from the nvidia JS request. No idea if there's an easier wey to get this.
            $Webcontent = (Invoke-WebRequest 'https://www.nvidia.com/Download/processFind.aspx?psid=73&pfid=842&osid=57&lid=1&whql=&lang=en-us&ctk=0&qnfslb=10&dtcid=0').links.href 
            #The following line uses Regex regular expressions to MATCH and RETRIEVE ONE single value from the list of values in the previous line.
            $NVIDIALinkToLatestDriver = [regex]::Match($Webcontent, '//(www.nvidia.com/download/driverResults.aspx/[0-9]*/en-us)').Groups[1].Value
            #Link after the previous crap
            $NVIDIADLPage = New-Object -COM "HTMLFile" #Creates a COM Object for easier search of the link.
            [string]$htmlBody = (Invoke-WebRequest -Uri $NVIDIALinkToLatestDriver -UseBasicParsing).Content #Parses the content of the landing page to then look by id
            $NVIDIADLPage.write([ref]$htmlBody)
            $replace = [regex]::Replace($NVIDIADLPage.getElementById('lnkDwnldBtn').href, 'about:', 'www.nvidia.com') #Replaces the "about" with "www.nvidia.com"
            $Webcontent = (Invoke-WebRequest $replace) #Replace Webcontent with the latest link.
            [String]$NvidiaLinkToExe = $Webcontent.links.href -match ".*.exe$" #On this link there's the exe file for Nvidia Drivers
            $NvidiaLinkToExe = $NvidiaLinkToExe -replace "^", "http:" #Replace start of line with the correct Nvidia Link.
            Remove-Variable -Name NVIDIADLPage, Webcontent, replace -Force #cleanup of the previous mess.
            if ($NvidiaLinkToExe -match 'http:\/\/.*[0-9]{2}\/(([0-9]{3}\.[0-9]{2}).*\.exe)') {
                $NVIDIAExeToDownload = [PSCustomObject]@{
                    Url     = $Matches[0];
                    Name    = $Matches[1];
                    Version = $Matches[2]
                }
            }

如您所见,我必须创建三个Indoke-webrequest才能获得一个链接。 而且,我认为我不使用管道,因为我无法使它起作用。

谢谢!

Hello all, I've been able to get this code to get the latest download link for Nvidia Driver for a Quadro P1000.
I was wondering, if there's a better way to do this.

Here's my code:


#Get Nvidia Drivers
            #This next section is JUST TO get the LINK to the LATEST driver from Nvidia
            #WebContent gets the links that are "href" from the nvidia JS request. No idea if there's an easier wey to get this.
            $Webcontent = (Invoke-WebRequest 'https://www.nvidia.com/Download/processFind.aspx?psid=73&pfid=842&osid=57&lid=1&whql=&lang=en-us&ctk=0&qnfslb=10&dtcid=0').links.href 
            #The following line uses Regex regular expressions to MATCH and RETRIEVE ONE single value from the list of values in the previous line.
            $NVIDIALinkToLatestDriver = [regex]::Match($Webcontent, '//(www.nvidia.com/download/driverResults.aspx/[0-9]*/en-us)').Groups[1].Value
            #Link after the previous crap
            $NVIDIADLPage = New-Object -COM "HTMLFile" #Creates a COM Object for easier search of the link.
            [string]$htmlBody = (Invoke-WebRequest -Uri $NVIDIALinkToLatestDriver -UseBasicParsing).Content #Parses the content of the landing page to then look by id
            $NVIDIADLPage.write([ref]$htmlBody)
            $replace = [regex]::Replace($NVIDIADLPage.getElementById('lnkDwnldBtn').href, 'about:', 'www.nvidia.com') #Replaces the "about" with "www.nvidia.com"
            $Webcontent = (Invoke-WebRequest $replace) #Replace Webcontent with the latest link.
            [String]$NvidiaLinkToExe = $Webcontent.links.href -match ".*.exe
quot; #On this link there's the exe file for Nvidia Drivers
            $NvidiaLinkToExe = $NvidiaLinkToExe -replace "^", "http:" #Replace start of line with the correct Nvidia Link.
            Remove-Variable -Name NVIDIADLPage, Webcontent, replace -Force #cleanup of the previous mess.
            if ($NvidiaLinkToExe -match 'http:\/\/.*[0-9]{2}\/(([0-9]{3}\.[0-9]{2}).*\.exe)') {
                $NVIDIAExeToDownload = [PSCustomObject]@{
                    Url     = $Matches[0];
                    Name    = $Matches[1];
                    Version = $Matches[2]
                }
            }

As you can see, I have to create three Invoke-WebRequest just to get one link.
And, I think I made no use of piping, because I was unable to make it work.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2025-02-02 18:46:54

您的代码似乎比我的代码更好。我不得不给Invoke-webrequest打电话3次以获取链接,然后再下载它。

$destination = 'C:\Temp'
$downloadlist = 'https://www.nvidia.com/Download/processFind.aspx?psid=73&pfid=842&osid=57&lid=1&whql=&lang=en-us&ctk=0&qnfslb=10&dtcid=0'

$pattern = "(?s)<tr>.+?href='//(?<URL>.+?)'>.+?<td.+?>(?<Version>.+?)</td.+?td.+?>(?<Date>.+?)</td.+</tr>"
$content = Invoke-WebRequest $downloadlist -UseBasicParsing

$download = if($content.RawContent -match $pattern){
    [PSCustomObject]@{
        URL     = $Matches.URL
        Version = $Matches.Version
        Date    = $Matches.Date
    }
}

$pattern = '(?s)(?<Package>/content/driver[^"]+?{0}/.+?)(?=")' -f ($download.Version -replace '.+\(|\)')
$content = Invoke-WebRequest $download.url -UseBasicParsing

if($content.RawContent -match $pattern){
    $pattern = '//(?<Package>.+?{0}.+exe)' -f ($download.Version -replace '.+\(|\)')
    $content = Invoke-WebRequest -Uri "https://www.nvidia.com$($Matches.Package)" -UseBasicParsing

    if($content.RawContent -match $pattern){
        Invoke-WebRequest "https://$($Matches.Package)" -OutFile (Join-Path $destination $($Matches.package -replace '.+/'))
    }
}

如果您只想要链接和其他信息,则可以放下第四个。

$downloadlist = 'https://www.nvidia.com/Download/processFind.aspx?psid=73&pfid=842&osid=57&lid=1&whql=&lang=en-us&ctk=0&qnfslb=10&dtcid=0'

$content = Invoke-WebRequest $downloadlist -UseBasicParsing

$download = if($content.RawContent -match "(?s)<tr>.+?href='//(?<URL>.+?)'>.+?<td.+?>(?<Version>.+?)</td.+?td.+?>(?<Date>.+?)</td.+</tr>"){
    [PSCustomObject]@{
        URL     = $Matches.URL
        Version = $Matches.Version
        Date    = $Matches.Date
    }
}

$pattern = '(?s)(?<Package>/content/driver[^"]+?{0}/.+?)(?=")' -f ($download.Version -replace '.+\(|\)')
$content = Invoke-WebRequest $download.url -UseBasicParsing

if($content.RawContent -match $pattern){
    $pattern = '//(?<Package>.+?{0}.+exe)' -f ($download.Version -replace '.+\(|\)')
    $content = Invoke-WebRequest -Uri "https://www.nvidia.com$($Matches.Package)" -UseBasicParsing

    $download.URL = "https://$($Matches.Package)"
    $download
}

Your code seems better than mine. I had to call Invoke-WebRequest 3 times to get the link as well and once more to download it.

$destination = 'C:\Temp'
$downloadlist = 'https://www.nvidia.com/Download/processFind.aspx?psid=73&pfid=842&osid=57&lid=1&whql=&lang=en-us&ctk=0&qnfslb=10&dtcid=0'

$pattern = "(?s)<tr>.+?href='//(?<URL>.+?)'>.+?<td.+?>(?<Version>.+?)</td.+?td.+?>(?<Date>.+?)</td.+</tr>"
$content = Invoke-WebRequest $downloadlist -UseBasicParsing

$download = if($content.RawContent -match $pattern){
    [PSCustomObject]@{
        URL     = $Matches.URL
        Version = $Matches.Version
        Date    = $Matches.Date
    }
}

$pattern = '(?s)(?<Package>/content/driver[^"]+?{0}/.+?)(?=")' -f ($download.Version -replace '.+\(|\)')
$content = Invoke-WebRequest $download.url -UseBasicParsing

if($content.RawContent -match $pattern){
    $pattern = '//(?<Package>.+?{0}.+exe)' -f ($download.Version -replace '.+\(|\)')
    $content = Invoke-WebRequest -Uri "https://www.nvidia.com$($Matches.Package)" -UseBasicParsing

    if($content.RawContent -match $pattern){
        Invoke-WebRequest "https://$($Matches.Package)" -OutFile (Join-Path $destination $($Matches.package -replace '.+/'))
    }
}

If you just want the link and other info you can drop the fourth.

$downloadlist = 'https://www.nvidia.com/Download/processFind.aspx?psid=73&pfid=842&osid=57&lid=1&whql=&lang=en-us&ctk=0&qnfslb=10&dtcid=0'

$content = Invoke-WebRequest $downloadlist -UseBasicParsing

$download = if($content.RawContent -match "(?s)<tr>.+?href='//(?<URL>.+?)'>.+?<td.+?>(?<Version>.+?)</td.+?td.+?>(?<Date>.+?)</td.+</tr>"){
    [PSCustomObject]@{
        URL     = $Matches.URL
        Version = $Matches.Version
        Date    = $Matches.Date
    }
}

$pattern = '(?s)(?<Package>/content/driver[^"]+?{0}/.+?)(?=")' -f ($download.Version -replace '.+\(|\)')
$content = Invoke-WebRequest $download.url -UseBasicParsing

if($content.RawContent -match $pattern){
    $pattern = '//(?<Package>.+?{0}.+exe)' -f ($download.Version -replace '.+\(|\)')
    $content = Invoke-WebRequest -Uri "https://www.nvidia.com$($Matches.Package)" -UseBasicParsing

    $download.URL = "https://$($Matches.Package)"
    $download
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文