Windows Update UI stuck on Error Encountered even when WindowsUpdateService is up and running

334 Views Asked by At

I need to implement disabling/enabling windows update from CMD/PowerShell. for that, I'm using the following commands to disable the WindowsUpdate:

sc config wuauserv start= disabled
sc stop wuauserv

And for enabling I'm using the following commands:

sc config wuauserv start=auto
net start wuauserv

The problem is that after the WindowsUpdate Service restarts the UI is still stuck (also after reboot) and Windows doesn't scan or install the new updates until I manually click the "Retry" button enter image description here

Is there any CMD/PowerShell that will change the windows update state back to normal? (without clicking on the retry button) a.k.a:

enter image description here

1

There are 1 best solutions below

0
On

I'm fairly sure that making the Windows Update Agent search for updates should clear that error in the UI. This should do that for you:

$WUSess = new-object -ComObject Microsoft.Update.Session
$Searcher=$WUSess.CreateUpdateSearcher()
$SearchResults = Try{$Searcher.Search("IsInstalled=0 and Type='Software'")}Catch{[pscustomobject]@{'Updates'=@()}}

That's part of a larger script I use to install updates via Windows Update without having to click through the UI. The entire script is here:

#region UACElevation
# Elevate UAC if not already running As Administrator
# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
 
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
 
# Check to see if we are currently running "as Administrator"
if (!$myWindowsPrincipal.IsInRole($adminRole))
{
    # We are not running "as Administrator" - so relaunch as administrator
    # Create an encoded string to re-launch the script bypassing execution policy
    $Code = ". '$($myInvocation.MyCommand.Definition)'"
    $Encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code))
   
    # Indicate that the process should be elevated
    Start-Process PowerShell.exe -Verb RunAs -ArgumentList "-EncodedCommand",$Encoded
   
    # Exit from the current, unelevated, process
    exit
}
#endregion UACElevation

#region Functions
Function Invoke-Pause ($text){
    
    [reflection.assembly]::LoadWithPartialName('Windows.Forms')|out-null
    If($psISE){
        [Windows.Forms.MessageBox]::Show("$Text", "Script Paused", [Windows.Forms.MessageBoxButtons]"OK", [Windows.Forms.MessageBoxIcon]"Information") | ?{(!($_ -eq "OK"))}
    }Else{
        Write-Host $Text
        Write-Host "Press any key to continue ..."
        $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }
}

Function Show-MsgBox ($Text,$Title="",[Windows.Forms.MessageBoxButtons]$Button = "OK",[Windows.Forms.MessageBoxIcon]$Icon="Information"){
[Windows.Forms.MessageBox]::Show("$Text", "$Title", [Windows.Forms.MessageBoxButtons]::$Button, $Icon) | ?{(!($_ -eq "OK"))}
}
#endregion Functions

$WUSess = new-object -ComObject Microsoft.Update.Session
$Searcher=$WUSess.CreateUpdateSearcher()
$SearchResults = Try{$Searcher.Search("IsInstalled=0 and Type='Software'")}Catch{[pscustomobject]@{'Updates'=@()}}
If($SearchResults.Updates.Count -eq 0){Invoke-Pause "No updates found at this time.`nScript will now exit.";Return}
If($($SearchResults.updates).Where({!($_.isdownloaded)})){
    Write-Host "Downloading $($($SearchResults.updates).Where({!($_.isdownloaded)}).Count) updates to install."
    $ToDownload = New-Object -ComObject Microsoft.Update.UpdateColl
    $SearchResults.updates|?{!($_.isdownloaded)}|%{[void]$ToDownload.Add($_)}
    $Downloader = $WUSess.CreateUpdateDownloader()
    $Downloader.Updates = $ToDownload
    [void]$Downloader.Download()
}
$SearchResults = $Searcher.Search("IsInstalled=0 and Type='Software'")
$ToInstall = New-Object -ComObject Microsoft.Update.UpdateColl
$SearchResults.Updates|?{$_.isDownloaded -and $_.EULAAccepted}|%{[void]$ToInstall.Add($_)}
$Installer = $WUSess.CreateUpdateInstaller()
$Installer.Updates = $ToInstall
Write-Host "Installing $($ToInstall.Count) udpates."
$InstResults = $Installer.Install()
If($InstResults.RebootRequired){Write-Warning "One or more of the updates installed require the system to be rebooted to complete its installation.`nPlease reboot now"}
$ResultCodes = @{ 
  0 = 'Not Started'
  1 = 'In Progress'
  2 = 'Succeeded'
  3 = 'Succeeded With Errors'
  4 = 'Failed'
  5 = 'Aborted'
}
If($InstResults.ResultCode -gt 3){Write-Warning "One or more update failed to install.`nIf this is the first time you have seen this message please reboot and try again"}
$(For($i=0;$i -lt $ToInstall.Count;$i++){[pscustomobject]@{'Result'=$ResultCodes[$InstResults.GetUpdateResult($i).ResultCode];'Update Title'=$ToInstall.Item($i).Title}})|FT -AutoSize
If(($InstResults.ResultCode -gt 3 -or $InstResults.RebootRequired) -and (Show-MsgBox -Text 'Would you like to reboot now?' -Title 'Reboot?' -Button YesNo -Icon Question ) -eq 'Yes'){Restart-Computer -Force}
Invoke-Pause -Text 'Finished installing updates, the script will now exit.'