Powershell 在dell上使用microsoft update作为源安装音频驱动程序脚本失败

Powershell 在dell上使用microsoft update作为源安装音频驱动程序脚本失败,powershell,Powershell,我试图运行以下代码在戴尔笔记本电脑上安装realtek音频驱动程序,但出现以下错误。是否可以对所有型号的笔记本电脑使用此脚本来安装或更新丢失的音频驱动程序?任何帮助都将不胜感激 错误: RegistrationState ServiceID IsPendingRegistrationWithAU Service ----------------- ---------

我试图运行以下代码在戴尔笔记本电脑上安装realtek音频驱动程序,但出现以下错误。是否可以对所有型号的笔记本电脑使用此脚本来安装或更新丢失的音频驱动程序?任何帮助都将不胜感激

错误:

RegistrationState ServiceID                            IsPendingRegistrationWithAU Service           
----------------- ---------                            --------------------------- -------           
                3 7971f918-a847-4430-9279-4a52d1efe18d                       False System.__ComObject
Exception from HRESULT: 0x80240024
+ $Downloader.Download()
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
 
Installing Drivers...
Exception from HRESULT: 0x80240024
+ $InstallationResult = $Installer.Install()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException


 
**CODE:** 

$UpdateSvc = New-Object -ComObject Microsoft.Update.ServiceManager            
$UpdateSvc.AddService2("7971f918-a847-4430-9279-4a52d1efe18d",7,"")     

$Session = New-Object -ComObject Microsoft.Update.Session           
$Searcher = $Session.CreateUpdateSearcher() 

$Searcher.ServiceID = '7971f918-a847-4430-9279-4a52d1efe18d'
$Searcher.SearchScope =  1 # MachineOnly
$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 | Where-Object { $_.DriverManufacturer -like 'Realtek' }
    
#Show available Drivers...

$Updates | select Title, DriverModel, DriverVerDate, Driverclass, DriverManufacturer | fl

#Download the Drivers from Microsoft

$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()

#Check if the Drivers are all downloaded and trigger the Installation

$UpdatesToInstall = New-Object -Com Microsoft.Update.UpdateColl
$updates | % { if($_.IsDownloaded) { $UpdatesToInstall.Add($_) | out-null } }

Write-Host('Installing Drivers...')  -Fore Green  
$Installer = $UpdateSession.CreateUpdateInstaller()
$Installer.Updates = $UpdatesToInstall
$InstallationResult = $Installer.Install()
if($InstallationResult.RebootRequired) {  
Write-Host('Reboot required! please reboot now..') -Fore Red  
} else { Write-Host('Done..') -Fore Green }  ```

在您的代码中有几处我需要更改。
首先,我认为您正在(不必要地)创建一个新的
Microsoft.Update.UpdateColl
对象两次。然后,您没有检查是否确实有要安装的更新,此时代码应该退出

在您的代码中,驱动程序制造商的测试是
-类似于'Realtek'
,但没有通配符(
*
),这与
-eq'Realtek'
相同,因此您可能会错过一对

# check if the Windows Update service is running
if ((Get-Service -Name wuauserv).Status -ne "Running") { 
    Set-Service -Name wuauserv -StartupType Automatic
    Start-Service -Name wuauserv 
} 
# check if there are updates available
$UpdateSession  = New-Object -Com Microsoft.Update.Session
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
Write-Host 'Searching Driver-Updates...' -ForegroundColor Green     
$SearchResult   = $UpdateSearcher.Search("IsInstalled=0 and Type='Driver' and IsHidden=0")

# collect the updates
$UpdateCollection = New-Object -Com Microsoft.Update.UpdateColl
$Updates = for ($i = 0; $i -lt $SearchResult.Updates.Count; $i++) {
    $Update = $SearchResult.Updates.Item($i)
    # we are only interested in RealTek Audio driver updates
    # if you need to add more manufacturers, change the first part in the 'if' to for instance
    # $Update.DriverManufacturer -match 'Realtek|Conexant|Intel'
    if ($Update.DriverManufacturer -like '*Realtek*' -and
       ($Update.Title -like '*audio*' -or $Update.Description -like '*audio*')) {
        if (-not $Update.EulaAccepted) { $Update.AcceptEula() | Out-Null }
        [void]$UpdateCollection.Add($Update)
        # output a PsCustomObject for display purposes only
        $Update | Select-Object Title, DriverModel, DriverVerDate, Driverclass, DriverManufacturer
    }
}

# no updates found; exit the script
if ($null -eq $Updates -or $Updates.Count -eq 0) {
     Write-Host 'No Driver-Updates available...' -ForegroundColor Cyan
}
else {
    # Show available driver updates...
    $Updates | Format-List
   
    # download the updates
    Write-Host 'Downloading driver updates...' -ForegroundColor Green  
    $Downloader = $UpdateSession.CreateUpdateDownloader()
    $Downloader.Updates = $UpdateCollection
    $Downloader.Priority = 3  # high
    [void]$Downloader.Download()

    # install the updates
    Write-Host 'Installing Drivers...' -ForegroundColor Green
    $Installer = $UpdateSession.CreateUpdateInstaller()
    # accept all Critical and Security bulletins.
    $Installer.ForceQuiet = $true
    $Installer.Updates    = $UpdateCollection
    $InstallationResult   = $Installer.Install()

    $ResultCode = $InstallationResult.ResultCode

    # test if the computer needs rebooting
    if ($InstallationResult.RebootRequired) {
        Write-Host 'Reboot required! please reboot now..' -ForegroundColor Red
    }
    else {
        Write-Host 'Done..' -ForegroundColor Green
    }
}

# Clean-up COM objects
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($UpdateSession)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($UpdateSearcher)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($UpdateCollection)
$null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($SearchResult)
if ($Downloader) { $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Downloader)}
if ($Installer)  { $null = [System.Runtime.Interopservices.Marshal]::ReleaseComObject($Installer)}
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()

非常感谢西奥,让我来测试一下。我们可以将此脚本用于所有型号的笔记本电脑吗?如果未安装音频,那么我们可以更改代码以安装驱动程序吗?@PGT该代码专门尝试查找Realtek设备的驱动程序(不一定仅限于音频)。如果其他笔记本电脑有来自不同制造商的设备,你也需要检查这些设备。首先进行测试,如果需要,我可以更新以添加其他制造商。我测试了@Theo,但它没有像我预期的那样工作,我只需要为每个型号安装音频驱动程序,无论是更新还是缺少驱动程序。是否有任何方法可以隐藏其他更新并只安装音频。@PGT我添加了仅针对音频驱动程序的测试和COM对象的清理代码。希望这能达到您的期望。@THEOI将使用不同型号的旧版本音频进行测试,并让您知道。