How to Approve Updates for Optional Install

 

Applies To: Windows Server Update Services

Use the following procedure to approve specified updates for optional install. When approved, the updates will appear in Programs and Features (PAF) for Windows Vista and above or in Add/Remove Programs (ARP) for versions of Windows before Windows Vista (for example, Windows XP).

The procedure demonstrates the WSUS API calls required to perform this task. The example that follows gives a working command-line application where the UpdateId, RevisionNumber, and TargetGroupName properties can all be passed in as parameters.

To approve updates for optional install

  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 UpdateRevisionId object and initialize its UpdateId and RevisionNumber properties. The UpdateId property is a GUID that identifies the update, while the RevisionNumber property is a numeric value representing the revision number to be approved. To specify that you want the latest revision, set the RevisionNumber property to 0.

    UpdateRevisionId updateRevisionId = new UpdateRevisionId();  
    
    // Replace UpdateGuid with actual value.   
    updateRevisionId.UpdateId = new Guid( /* Your GUID here */ );   
    
    // 0 represents the latest revision.   
    updateRevisionId.RevisionNumber = 0;  
    
    
  4. Instantiate an IUpdateServer object using the static AdminProxy.GetUpdateServer method.

    IUpdateServer updateServer = AdminProxy.GetUpdateServer();  
    
    
  5. Determine the target group. The following code snippet sets the target group to ComputerTargetGroupId.AllComputers.

    IComputerTargetGroup targetGroup = updateServer.GetComputerTargetGroup(ComputerTargetGroupId.AllComputers);  
    
    

    If you want a specific target group, you’ll need to get the IComputerTargetGroup interface for that group. The following code snippet includes a helper method (FindTargetGroup) that takes two parameters—the WSUS server (updateServer) and the name of desired target group (targetGroupName)—and returns the IComputerTargetGroup interface for that target group.

    IComputerTargetGroup targetGroup = FindTargetGroupByName(updateServer, args[3]);  
    
    ...  
    
    IComputerTargetGroup FindTargetGroupByName(IUpdateServer updateServer, string targetGroupName)  
    {  
       ComputerTargetGroupCollection targetGroupsCollection = updateServer.GetComputerTargetGroups();  
       foreach (IComputerTargetGroup targetGroup in targetGroupsCollection)  
       {  
          if (targetGroup.Name == targetGroupName)   
          {  
             return targetGroup;  
          }  
       }  
       // If you get this far, throw an exception, because it means that the specified target group could not be located.     
       throw new ArgumentException("Could not find target group: {0}", targetGroupName);  
    }  
    
    
  6. Use the IUpdateServer.GetUpdate method to get the update to approve.

    // Get the update to approve.  
    IUpdate update = updateServer.GetUpdate(updateRevisionId);  
    
    
  7. After you have the IUpdate and IComputerTargetGroup interfaces, you can approve the update via the IUpdateServer.ApproveForOptionalInstall method. The update will then show up in the PAF or ARP, depending on the Windows version.

    // Approve the update.  
    update.ApproveForOptionalInstall(targetGroup);  
    
    

The following is the complete code listing for a console application that approves the specified update for optional install.

Syntax: ApproveForOptionalInstall UpdateID RevisionNumber [TargetGroupName]

UpdateIDIdentifies - The update with its GUID

RevisionNumberRevision - Number to be approved (0 = latest revision).

[TargetGroupName] (Optional) - Group to which updates must be approved. Defaults to "All Computers"

/*----------------------------------------------------------------------  
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.  
----------------------------------------------------------------------*/  
  
/*   
 * This application approves the specified update for optional install.    
 * i.e. it will be shown as an optional install in the Add/Remove Programs (ARP)   
 * or Programs And Features (PAF) in case of Windows Vista  or above.  
 *   
 * This application takes the following command line parameters  
 *    UpdateID  
 *    TargetGroupName (if not specified, then update is approved for "All Computers" target group)  
*/  
  
using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
using System.Xml;  
  
using Microsoft.UpdateServices.Administration;  
  
namespace WsusSamples  
{  
  class ApproveUpdatesForOptionalInstall  
  {  
    static void Main(string[] args)  
    {  
      try  
      {  
        const String usage = "\r\nApproveForOptionalInstall. Approves a specified update for optional install.\r\n\r\n" +  
                             "USAGE:\r\n\r\n" +  
                             "ApproveForOptionalInstall UpdateID RevisionNumber [TargetGroupName]\r\n\r\n" +  
                             "\tUpdateID          Identifies the update with its GUID.\r\n" +  
                             "\tRevisionNumber    Revision number to be approved. (0 = latest revision)\r\n" +  
                             "\t[TargetGroupName] Group to which updates must be approved. (Default = All Computers)\r\n";  
  
        // There are two mandatory params to be passed with an optional third param  
        // Therefore, verify that the correct number of params are being passed.  
        // If not, show syntax  
        if ((args.Length < 2) || (args.Length > 3))  
        {  
          Console.WriteLine(usage);  
          return;  
        }  
  
        UpdateRevisionId updateRevisionId = new UpdateRevisionId();  
        updateRevisionId.UpdateId = new Guid(args[0]);  
        updateRevisionId.RevisionNumber = Convert.ToInt32(args[1]);  
  
       //connect to the local server  
        IUpdateServer updateServer = AdminProxy.GetUpdateServer();  
  
        // determine the target group to approve for  
        IComputerTargetGroup targetGroup = null;  
        if (args.Length == 4)  
        {  
          //target group was specified on the command line  
          targetGroup = FindTargetGroupByName(updateServer, args[2]);  
        }  
        else  
        {  
          //target group was not specified; default to the All Computers group  
          targetGroup = updateServer.GetComputerTargetGroup(ComputerTargetGroupId.AllComputers);  
        }  
  
        //get the update to approve  
        IUpdate update = updateServer.GetUpdate(updateRevisionId);  
  
        //approve the update  
        update.ApproveForOptionalInstall(targetGroup);  
  
        Console.WriteLine("Created Approval for optional Install to {0}", targetGroup.Name);  
      }  
      catch (Exception ex)  
      {  
        Console.WriteLine("An error occurred:\r\n" +  
                          "Error text: {0}\r\n" +  
                          "Stack trace: {1}\r\n",  
                          ex.Message, ex.StackTrace);  
      }  
    }  
  
    static IComputerTargetGroup FindTargetGroupByName(IUpdateServer updateServer, string targetGroupName)  
    {  
        ComputerTargetGroupCollection targetGroupsCollection = updateServer.GetComputerTargetGroups();  
        foreach (IComputerTargetGroup targetGroup in targetGroupsCollection)  
        {  
          if (targetGroup.Name == targetGroupName)   
          {  
            return targetGroup;  
          }  
        }  
  
        throw new ArgumentException("Could not find target group: {0}", targetGroupName);  
    }  
  }  
}  

Community Additions

ADD
Show: