2

I started testing around if i could create a powershell script, which would update all device drivers automatically.

After searching for a while, i stumbled across this thread: How to Automatically update all devices in device manager

The User "HarryMc" pretty much provided me with all the answers needed. My problem now is, that i run into an error on the "Downloading Drivers" and "Installing Drivers" Part.

Powershell throws an HRESULT 0x80240024 exception. The past couple of days i was trying to figure out what might be causing this, but no answer could help me out.

Now i was hoping that maybe here i might be able to get some ideas or answers that could fix the issue.

The Code that i used is:

#search and list all missing Drivers
set Window Title $host.ui.RawUI.WindowTitle = "HarryMc Driver Updater"

$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' and ISHidden=0"
Write-Host('Searching Driver-Updates...') -Fore Green  
$SearchResult = $Searcher.Search($Criteria)          
$Updates = $SearchResult.Updates

#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 }

Write-Host Write-Host('Press any key to exit ...') -Fore Yellow   $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

EDIT: The script fails at line 28 in which it tries to download the drivers, and therefore fails to install them at line 38. Script is being used primarily on win 10 systems.

Thx guys!

Suishido
  • 21
  • 3
  • So what line throws the exception? – DavidPostill Sep 03 '20 at 21:10
  • Does `$updates` contain anything?, I get this exception when there are no updates, when removing the search criteria it sucessfully downloads for me the updates that are found. – CraftyB Sep 03 '20 at 21:21
  • @DavidPostill so, the script manages to find all the drivers available, but fails at line 28 and line 38. – Suishido Sep 04 '20 at 05:46
  • @CraftyB i just checked and yes, the variable does contain 2 updates. It fails to download the drivers in line 28, and therefore fails to install at line 38. Strange... – Suishido Sep 04 '20 at 06:04
  • I would suggest outputting the values of eula accepted: `$Updates | Foreach {$_.EulaAccepted}` to check that these are all true. If they are not use the following: `$Updates | Foreach {$_.AcceptEula()}` then check they have updated to true using the 1st command. Other things to consider, is it a VM you are installing these to?, is the time/date correct on the system? – CraftyB Sep 04 '20 at 07:56
  • @CraftyB thank you for your quick reply :) i just checked the values of eula accepted and they are all set to "true". I´m using the script on my local computer (it´s my work computer though). Time and Date are correct, but something else might be blocking it then i guess... but i´m really not sure what it could be. – Suishido Sep 04 '20 at 09:38
  • Is your work computer in a domain? and do you use WSUS? – CraftyB Sep 04 '20 at 12:42
  • @CraftyB sorry, late reply! Yes it actually is and we do use WSUS ._.) – Suishido Sep 07 '20 at 05:36
  • Try the following: locate `HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU` in the registry and change the value of `UseWUServer (REG_DWORD)` to `0`, this should be enough to detach your system from WSUS, you will need to revert this after running your script (Or you could incorporate it as part of your script.). Also could you please confirm that you are running the script as administrator? – CraftyB Sep 07 '20 at 06:48
  • @CraftyB you´re a legend! I can´t believe i tried everything BUT running it as administrator... oh god. But it works... it finally works!! What a relief :) Thank you very much!! – Suishido Sep 07 '20 at 15:02
  • Is this something you will want to run against numerous machines. I will write up an answer in the next day or so, if explained correctly could you please mark the answer as correct . Thanks – CraftyB Sep 07 '20 at 23:04
  • @CraftyB yes, this will run on numerous machines,but on all of them i will have admin permissions. And sure can! Only thing is, i don´t really know how. I noticed my question has been marked as "closed" because somehow "details and clarity" to the question are missing...is that gonna be a problem? – Suishido Sep 08 '20 at 06:04

0 Answers0