Anyone have this issue, I install updates using this class. Updates are downloaded, installed correctly but still show in the WU GUI as not installed. Pressing install now on the GUI refreshes the fact they're installed.
class WindowsUpdate:IDownloadProgressChangedCallback,IDownloadCompletedCallback, IInstallationCompletedCallback, IInstallationProgressChangedCallback
{
public static UpdateSession session = new UpdateSession();
public UpdateCollection neededUpdates = new UpdateCollection();
UpdateDownloader downloader = session.CreateUpdateDownloader();
IUpdateInstaller installer = session.CreateUpdateInstaller();
public Status currentStatus;
bool enableDebugLogging = false;
bool enableInstall = true;
bool enableDownload = true;
public bool rebootPending = false;
public List<string> messages = new List<string>();
public void checkForUpdates()
{
debugLog("New WU: Checking for Updates");
currentStatus = Status.Starting;
IUpdateSearcher searcher = session.CreateUpdateSearcher();
ISearchResult results = searcher.Search("IsInstalled=0 AND IsHidden=0");
debugLog(string.Format("{0} Updates Found", results.Updates.Count));
downloader.Updates = new UpdateCollection();
for (int x = 0; x < results.Updates.Count; x++)
{
IUpdate u = results.Updates[x];
if (!u.EulaAccepted)
{
u.AcceptEula();
}
neededUpdates.Add(u);
debugLog(string.Format("Update {0} Needed", u.Title));
if (!u.IsDownloaded)
{
debugLog(string.Format("Adding {0} To the WU Download Queue", u.Title), false);
downloader.Updates.Add(u);
debugLog(string.Format("Added {0} To the WU Download Queue", u.Title));
}
}
debugLog("New WU: Search Complete");
if (downloader.Updates.Count > 0 && enableDownload)
{
downloadUpdates();
}else if(downloader.Updates.Count == 0 && results.Updates.Count > 0)
{
debugLog(string.Format("{0} Updates, None pending download", downloader.Updates.Count));
if (enableInstall)
{
debugLog("Queing Install");
}
installUpdates();
}else if(!enableDownload && downloader.Updates.Count > 0)
{
debugLog(string.Format("{0} Updates Pending Download, Download Not Enabled", downloader.Updates.Count));
}else if(neededUpdates.Count == 0)
{
currentStatus = Status.Ready;
}
}
//void Dispose()
//{
// //TODO DISPOSE LOGIC
// //UNSURE IF OBJECT DISPOSAL NEEDED
//}
void installUpdates()
{
debugLog(string.Format("Beginning Installation of {0} Updates", neededUpdates.Count));
installer.Updates = neededUpdates;
installer.BeginInstall(this, this, null);
}
void downloadUpdates()
{
//https://csharp.hotexamples.com/examples/-/UpdateSessionClass/CreateUpdateSearcher/php-updatesessionclass-createupdatesearcher-method-examples.html UpdateDownloader downloader = session.CreateUpdateDownloader();
//updateDownloader.IsForced = true;
//updateDownloader.Priority = DownloadPriority.dpHigh;
//https://learn.microsoft.com/en-us/windows/win32/api/wuapi/nf-wuapi-iupdatedownloader-begindownload
debugLog(string.Format("Beginning Download of {0} Updates", downloader.Updates.Count));
IDownloadJob job = downloader.BeginDownload(this, this, null);
}
void IDownloadCompletedCallback.Invoke(IDownloadJob downloadJob, IDownloadCompletedCallbackArgs callbackArgs)
{
debugLog("Download Completed Callback");
foreach (IUpdate u in downloadJob.Updates)
{
debugLog(string.Format("{0} Download Completed", u.Title));
}
debugLog("All Updates Downloaded, Beginning Install");
currentStatus = Status.Installing;
installUpdates();
}
void IDownloadProgressChangedCallback.Invoke(IDownloadJob downloadJob, IDownloadProgressChangedCallbackArgs callbackArgs)
{
debugLog("Download Status Callback", false);
currentStatus = Status.Downloading;
IDownloadProgress progress = callbackArgs.Progress;
debugLog(string.Format("WUA_DownloadingProgress:{0}|{1}|{2}",
progress.CurrentUpdateIndex + 1,
progress.CurrentUpdatePercentComplete,
progress.PercentComplete), false);
}
void IInstallationCompletedCallback.Invoke(IInstallationJob installationJob, IInstallationCompletedCallbackArgs callbackArgs)
{
debugLog("Install Complete Callback");
foreach(IUpdate u in installationJob.Updates)
{
debugLog(string.Format("Installation Complete: {0}", u.Title));
}
debugLog("Ending WU Install", false);
IInstallationResult installationResult = installer.EndInstall(installationJob);
debugLog("After Installer Ended", false);
if (installationResult.RebootRequired)
{
debugLog("Reboot Required, Setting Flag", false);
rebootPending = true;
}
debugLog("Starting Cleanup");
//installationJob.CleanUp();
debugLog("Ending Cleanup");
currentStatus = Status.Installed;
}
void IInstallationProgressChangedCallback.Invoke(IInstallationJob installationJob, IInstallationProgressChangedCallbackArgs callbackArgs)
{
debugLog("Install Progress Callback", false);
currentStatus = Status.Installing;
IInstallationProgress p = callbackArgs.Progress;
debugLog(string.Format("Installation Progress: {0} {1} {2}",
p.CurrentUpdateIndex + 1,
p.CurrentUpdatePercentComplete,
p.PercentComplete), false);
}
void debugLog(string message, bool showInResults = true)
{
if (enableDebugLogging)
{
Logging.WriteToTextLog(message);
}
if (showInResults)
{
messages.Add(message);
}
}
public enum Status
{
Starting = 0,
Ready = 1,
Downloading = 2,
Installing = 3,
Installed = 4
}
public WindowsUpdate(bool logging, bool install)
{
enableDebugLogging = logging;
enableInstall = install;
currentStatus = Status.Starting;
debugLog("WU Instance Called");
}
public string getStatus()
{
StringBuilder sb = new StringBuilder();
foreach(string s in messages)
{
sb.Append(s);
sb.Append(Environment.NewLine);
}
return sb.ToString();
}
}
Tried debugging, per the log file updates are installed correctly. I'm wondering if I need to use a different version of IUpdateInstaller.