How to Determine Status for all Updates

 

Applies To: Windows Server Update Services

Use the following procedure to generate an XML file with a list of updates and the update status for each computer client. The example that follows is a fully-functioning application that creates an XML file with a list of updates, with update status for each computer client.

To determine update status

  1. Add the Microsoft.UpdateServices.Administration assembly to your project.

  2. Insert a using statement for the Microsoft.UpdateServices.Administration namespace.

    using Microsoft.UpdateServices.Administration;  
    
  3. Instantiate an IUpdateServer object by using the static AdminProxy.GetUpdateServer method.

    IUpdateServer server = AdminProxy.GetUpdateServer();  
    
  4. Using the IUpdateServer.GetUpdates method, get an UpdateCollection object that includes the latest revisions of all available drivers and software updates.

    UpdateCollection UpdateCollection = server.GetUpdates();  
    
  5. Use the IUpdateServer.GetComputerTargetGroup method to get the "All Computers" computer group.

    IComputerTargetGroup allComputersGroup = server.GetComputerTargetGroup(ComputerTargetGroupId.AllComputers);  
    
  6. Iterate over the IUpdate objects within the UpdateCollection object. For each IUpdate object, first determine whether the update was declined. If not, call the IComputerTargetGroup.GetUpdateInstallationInfoPerComputersTarget method to retrieve the status, via the UpdateInstallationInfoCollection object for that update. After you acquire the UpdateInstallationInfoCollection object, you can enumerate that object’s IUpdateInstallationInfo object for each update and process as needed.

    // Get the status for each update and all the computers.  
    foreach (IUpdate Update in UpdateCollection)  
    {  
       // Print status for this update if it is not declined.  
       if (!Update.IsDeclined)  
       {  
          // Get the status of all the computers for this update.  
          UpdateInstallationInfoCollection InstallInfo = allComputersGroup.GetUpdateInstallationInfoPerComputerTarget(Update);  
    
          // Output the status and computer name for all computers and this update.  
          foreach (IUpdateInstallationInfo info in InstallInfo)  
          {  
          }   
       }  
    }  
    

Example

The following is the complete code listing for a console application that creates an XML file with a list of updates, with update status for each computer client.

/*----------------------------------------------------------------------  
This file is part of the Microsoft Windows Server Update Services  
API Code Samples.  
  
DISCLAIMER OF WARRANTY: THIS CODE AND INFORMATION ARE PROVIDED "AS-IS."    
YOU BEAR THE RISK OF USING IT.  MICROSOFT GIVES NO EXPRESS WARRANTIES,   
GUARANTEES OR CONDITIONS.  YOU MAY HAVE ADDITIONAL CONSUMER RIGHTS   
UNDER YOUR LOCAL LAWS WHICH THIS AGREEMENT CANNOT CHANGE.    
TO THE EXTENT PERMITTED UNDER YOUR LOCAL LAWS, MICROSOFT EXCLUDES   
THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR   
PURPOSE AND NON-INFRINGEMENT.  
----------------------------------------------------------------------*/  
  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Xml;  
  
using Microsoft.UpdateServices.Administration;  
  
namespace WsusSamples  
{  
  class DetermineStatusForAllUpdates  
  {  
    static void Main(string[] args)  
    {  
      try  
      {  
        Console.WriteLine("Getting update status...");  
  
        // Open the XML file  
        StringBuilder xmlPath = new StringBuilder();  
        xmlPath.AppendFormat("{0}\\UpdateStatus {1}.xml",   
                              Environment.CurrentDirectory,  
                              DateTime.Now.ToLongDateString());  
        XmlTextWriter xml = new XmlTextWriter(xmlPath.ToString(), System.Text.Encoding.UTF8);  
        xml.Formatting = Formatting.Indented;  
        xml.Indentation = 4;  
  
        xml.WriteStartDocument(true);  
  
        // Write the <updateStatus> element  
        xml.WriteStartElement("updateStatus");  
  
        // connect to the local server  
        IUpdateServer server = AdminProxy.GetUpdateServer();  
  
        // get all updates  
        UpdateCollection UpdateCollection = server.GetUpdates();  
  
        // get the all computers group  
        IComputerTargetGroup allComputersGroup = server.GetComputerTargetGroup(ComputerTargetGroupId.AllComputers);  
  
        // get the status for each update and all the computers  
        foreach (IUpdate Update in UpdateCollection)  
        {  
          // print status for this update if it is not declined  
          if (!Update.IsDeclined)  
          {  
            // Open the <update> element  
            xml.WriteStartElement("update");  
  
            // Output the update title  
            xml.WriteAttributeString("updateTitle", Update.Title);  
  
            // Open the <computerStatus> element  
            xml.WriteStartElement("computerStatus");  
  
            // Get the status of all the computers for this update  
            UpdateInstallationInfoCollection InstallInfo = allComputersGroup.GetUpdateInstallationInfoPerComputerTarget(Update);  
  
            // Output the status and computer name for all computers and this update  
            foreach (IUpdateInstallationInfo info in InstallInfo)  
            {  
              // Open the <computer> element  
              xml.WriteStartElement("computer");  
  
              // Output the computer name  
              xml.WriteAttributeString("computerName", info.GetComputerTarget().FullDomainName);  
  
              // Output the status of this update  
              xml.WriteAttributeString("status", info.UpdateInstallationState.ToString());  
  
              // Close the <computer> element  
              xml.WriteEndElement();  
            } // foreach IUpdateInstallationInfo  
  
            // Close the <computerStatus> element  
            xml.WriteEndElement();  
  
            // Close the <update> element  
            xml.WriteEndElement();  
          } // if (!Update.IsDeclined)  
        } // foreach IUpdate  
  
        // Close the <updateStatus> element  
        xml.WriteEndElement();  
  
        xml.Close();  
  
        Console.WriteLine("Done. Results are written to the updateStatus {0}.xml file in the current folder",  
                          DateTime.Now.ToLongDateString());  
  
      } // try  
      catch (Exception ex)  
      {  
        Console.WriteLine("An error occurred: {0}", ex.ToString());  
      } // catch  
    } // main  
  } // class  
} // namespace