I have several Windows 7 PCs that need to be fixed with a specific Windows update using the Windows Update API in the C # console application. The API should look for installed updates and report if it is already installed and complete the installation if not.
During testing on a virtual PC (Windows 7 Professional Hyper-v client), I have a situation similar to the target PC (Windows 7 Embedded), where the following code returns (very quickly and without any exceptions) 0 updates. Which I know is wrong. In fact, it even returns this after installing the .msu update.
code:
UpdateSession uSession = new UpdateSession(); IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher(); uSearcher.Online = false; try { ISearchResult sResult = uSearcher.Search("IsInstalled=1 And IsHidden=0"); Console.WriteLine("Found " + sResult.Updates.Count + " updates"); foreach (IUpdate update in sResult.Updates) { Console.WriteLine(update.Title); if (update.Title.ToLower().Contains("kb123456")) { //Update is not required ReportInstalled(); return; } } //If we get here, the update is not installed InstallUpdate(); } catch (Exception ex) { Console.WriteLine("Something went wrong: " + ex.Message); }
Now for the fun part. If I open Windows Update from the control panel and click "Check for Updates", it will turn off for a while and return with a set of updates for installation. At this point, if I run the above code, it works as expected and reports 200 installed updates.
It seems that the manual update search process starts / restarts some services and / or other processes, however I am struggling to figure out what I need to do to get the system back into the correct state. I expect the answer to be a simple case of starting service x or process y with a set of arguments, but which?
Some (not all) things I tried but did not change the behavior:
- Started the BITS service, restarted the Windows Update service.
- Test run wuauclt.exe with various switches (documented here in the comments)
With the machine in a state where the code is working correctly (after starting WU manually), I noticed that the wuauclt.exe process appears when the above code is launched. When it is in the target state (before starting WU manually), wuauclt.exe does not start, and I cannot start it manually, I suspect that this is a big key.
Another key is the status of Windows Update before starting it manually. The update in the control panel window is as follows:

After starting WU and installing updates using this method, and the machine is in a state where the code is working, as expected, WU looks like this:

To summarize, I need this process to automate the installation of the update. If I find 0 installed updates, I know that the machine is in a certain state, so I will need to start / restart some processes and services (programmatically) so that the machine is in the correct state before running my code. Knowing what to start / restart is the essence of this problem.