question

RakeshG-5216 avatar image
0 Votes"
RakeshG-5216 asked RakeshG-5216 edited

Powershell driver update script not working as expected

I am running below script to install/update only sound driver but it's not detecting driver update or missing driver , It's only giving no driver update available. Is there anything missing in the code that's stopping the script to work?

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

>> 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 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 -match 'Realtek|Conexant|Intel' -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()

windows-10-generalwindows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Any one have any suggestions? Any help would be really appreciated.

0 Votes 0 ·

1 Answer

AravinthMathan-3183 avatar image
0 Votes"
AravinthMathan-3183 answered RakeshG-5216 edited

Does it show updates if you manually check for update?
There are GPOs which can block update over internet, have you checked on those.

Regards
Aravinth

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AravinthMathan-3183 Any suggestions about the policy which is blocking?

I confirmed below policy
Computer config>Administrative templates>windows component>windows update>do not include drivers with windows update >not configured

0 Votes 0 ·