Automatize drivers installation script

Hello everyone.

In my department, we deploy a lot of pc’s every day so I want to create a script to automatize drivers installation as we spent more than 20 minutes doing that.

Unfortunately, I have no much experience in PowerShell scrips so I searched it on the internet. Finally, I found a script to do that so I used it.

** It required to install the next module: https://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc **

Code below:

# Set Window Title
Write-Host('Driver Updater by Sergio Rolando') -Fore Green
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

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

# Get all available Drivers
$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

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

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

I executed it and it worked well, I received a list of all drivers pending to be updated and some of those drivers were installed properly till I reboot the machine due it was requested "Write-Host(‘Reboot required! please reboot now…’) -Fore Red "

Unfortunately, after rebooting the machine I checked that some drivers still pending to update so I executed the script again and I received next error:

Driver Updater by Sergio Rolando

VirtualKeyCode             Character                  ControlKeyState         KeyDown
--------------                      ---------                        ---------------             -------
32                                                                                                0                 True

Searching Driver-Updates...
Downloading Drivers...
Exception from HRESULT: 0x80240024
At D:\Automatization\update.ps1:38 char:1
+ $Downloader.Download()
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Installing Drivers...
Exception from HRESULT: 0x80240024
At D:\Automatization\update.ps1:47 char:1
+ $InstallationResult = $Installer.Install()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Done..
Press any key to exit ..."

So just one driver displayed on “check” phase although I know that are more than one pending to update and I received next errors during downloading/installation phases…

Any idea/suggestion about why it happens and how to fix it?

Are all the machines the same model (or a small number of models)?

Are you imaging these devices? What is the deployment process being utilized?