Install Windows Updates using C# & WUAPI

This document explains how to get the windows updates available for the current machine and selectively install them, using c#.

Overview
This document explains how to get the windows updates available for the current machine
and selectively install them, using c#.

Requirement
Perform Windows Updates customized installation using c# and Windows Update API Library.
This will provide centralized command over customized installation of updates, patches, KB for
Windows.
EX: If particular updates are not to be installed on some machines, in such cases, we can
configure our custom update application to ignore those updates, using this approach.

Benefits
1.  Exclusive command over the updates installed over a network of systems.
2.  Easy to develop and support, using .NET
3.  Simple API approach

References Required
Add reference to the c:\WINDOWS\system32\wuapi.dll file. This is the API library for Window
Update system.
Write the below import statement

using WUApiLib;
using System.Management;


Coding

Now, we need to search the Microsoft updates for this machine;

UpdateSessionClass uSession = new UpdateSessionClass();
IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher();
ISearchResult uResult = uSearcher.Search("IsInstalled=0 and
Type='Software'");

All the updates found will be now populated into the uResult collection object, which can be
accessed using the below foreach loop

foreach (IUpdate update in uResult.Updates)
{
       Console.WriteLine(update.Title);
}

Note that, we have only found the updates available for this machine, but haven’t
downloaded them yet.

You can iterate the available updates to select only the required updates and add then to an
UpdateCollection class which can be assigned to the below UpdateDownloader class so as to
download them

Now we have to create an UpdateDownloader class object to download the updates, as below

UpdateDownloader downloader = uSession.CreateUpdateDownloader();
downloader.Updates = uResult.Updates;
downloader.Download();

Now, as we have completed the download of the required updates, we have to install the
downloaded updates by checking its IsDownloaded property is true.

UpdateCollection updatesToInstall = new UpdateCollection();
foreach (IUpdate update in uResult.Updates)
{
      if (update.IsDownloaded)
            updatesToInstall.Add(update);
}

Now assign the update collection of to be installed updates to the installer class

IUpdateInstaller installer = uSession.CreateUpdateInstaller();
installer.Updates = updatesToInstall;

Now, call the install method of the IUpdateInstaller object and result of which will be stored in
IInstallationResult object installationRes.

IInstallationResult installationRes = installer.Install();

Now as all updates will be installed sequentally by the WUAPI, after which the result will be
stored in installationRes object. We can iterate throught the ICollection implemented object to
find the status of the updates, as mentioned below,

for (int i = 0; i < updatesToInstall.Count; i++)
{
   if (installationRes.GetUpdateResult(i).HResult == 0)
   {
      Console.WriteLine("Installed : " + updatesToInstall[i].Title);
   }
   else
   {
      Console.WriteLine("Failed : " + updatesToInstall[i].Title);
   }
}

Now, all the selected updates are installed on the PC where the application was executed.

Conclusion
The above code can be modified to install filtered updates for a group of PC’s on the network
which can be controlled from a centralized location.
These situations had occurred in our project, wherein some updates were identified to de-
stabilize some of our clients proprietary application and so they should not be installed on the
Production Server as well as some test servers.
Hence, this application could be tweaked to perform windows update on those machines
excluding such problem throwing updates.



By Devil Scorpio   Popularity  (3501 Views)