WSUS TOOL: Copying Approvals Between Target Groups

You can use the WSUS API to copy update approvals from one WSUS group to another. Yasufumi Shiraishi [MSFT] has posted the code and the procedure to get a tool that copies update approvals from one computer group to another,


Some additional notes about the utility from Torgeir Bakken (MVP) on microsoft.public.windows.server.update_services:

1. The utility is a command line based utility, you need to specify the “from” and “to” group names on the command line.

2. If a group name contains spaces, you need to surround it with quotes, like this: “Test group”.

3. The naming of the groups is case sensitive so “Servers” is not equal to “servers”.

4. The utility will mirror the approval status, so any additional approved installations in the target group will be unapproved.


Copying the Code and Procedure from the original post;


1) Copy the code between the two —————-8<—————–
below and save it to a text file named “CopyApprovalsBetweenGroups.cs

2) Open a command prompt, and navigate to the directory containing
CopyApprovalsBetweenGroups.cs.

3) Run the following command line (all one one line, you will need to
unwrap the line before running it!):

%WINDIR%\Microsoft.NET\Framework\v1.1.4322\csc.exe
  /r:”%PROGRAMFILES%\Update Services\service\bin
\Microsoft.UpdateServices.Administration.dll” /target:exe
  /out:CopyApprovalsBetweenGroups.exe CopyApprovalsBetweenGroups.cs

This will create a tool called CopyApprovalsBetweenGroups.exe.

(Note if you use Visual Studio to compile the code instead of using the command line above, you need to add a reference to the file microsoft.updateservices.administration.dll in your project, see
bottom of this Web page for more on this:
http://download.microsoft.com/download/7/4/5/7458e392-11de-4543-936c-b5248e344487/readme.htm)


Content of CopyApprovalsBetweenGroups.cs:
——————–8<———————-

using System;
using Microsoft.UpdateServices.Administration;

// Usage:
// copyapprovals <name of group to copy from> <name of group to copy to>
class Program
{
  static IComputerTargetGroup FindComputerTargetGroup(
   ComputerTargetGroupCollection groups,
   string name)
  {
   foreach (IComputerTargetGroup group in groups)
   {
    if (group.Name == name)
    {
     return group;
    }
   }

   throw new ApplicationException(string.Format(“Computer group {0} not found.”, name));
  }

  static void Main(string[] args)
  {
   try
   {
    if (args.Length != 2)
    {
     //System.Windows.Forms.MessageBox.Show(“TEST”);
     Console.WriteLine(“Incorrect number of arguments.”);
     Console.WriteLine(“usage: copyapprovals <name of group to copy from>” +
      ” <name of group to copy to>”);
     Console.ReadLine();
     return;
    }

    IUpdateServer server = AdminProxy.GetUpdateServer();
    ComputerTargetGroupCollection groups = server.GetComputerTargetGroups();

    // get IComputerTargetGroup references for the source and destination groups
    IComputerTargetGroup sourceGroup = FindComputerTargetGroup(groups, args[0]);
    IComputerTargetGroup destinationGroup = FindComputerTargetGroup(groups, args[1]);

    Console.WriteLine(“Copying update approvals from group {0} to group {1}.”, args[0],
     args[1]);

    // loop over all updates, copying approvals from the source group to the destination
    // group as necessary
    UpdateCollection updates = server.GetUpdates();

    foreach (IUpdate update in updates)
    {
     UpdateApprovalCollection sourceApprovals = update.GetUpdateApprovals(sourceGroup);
     UpdateApprovalCollection destinationApprovals =
       update.GetUpdateApprovals(destinationGroup);

     // for simplicity, this program assumes that an update has
     // at most one approval to a given group
     if (sourceApprovals.Count > 1)
     {
      Console.WriteLine(
       “Update {0} had multiple approvals to group {1}; skipping.”,
       update.Title,
       sourceGroup.Name);
      continue;
     }
     if (destinationApprovals.Count > 1)
     {
      Console.WriteLine(
       “Update {0} had multiple approvals to group {1}; skipping.”,
       update.Title,
       destinationGroup.Name);
      continue;
     }

     IUpdateApproval sourceApproval = null;
     IUpdateApproval destinationApproval = null;

     if (sourceApprovals.Count > 0)
     {
      sourceApproval = sourceApprovals[0];
     }
     if (destinationApprovals.Count > 0)
     {
      destinationApproval = destinationApprovals[0];
     }

     if (sourceApproval == null)
     {
      // the update is not approved to the source group

      if (destinationApproval != null)
      {
       // the update is not approved to the source group, but it is approved
       // to the destination group
       // unapprove the update for the destination group to match the source
       Console.WriteLine(
        “Unapproving update {0} to group {1}.”,
        update.Title,
        destinationGroup.Name);
       destinationApproval.Delete();
      }
      else
      {
       // neither the source group nor the destination group have an approval;
       // do nothing
      }
     }
     else
     {
      // the source group has an approval

      if (destinationApproval != null)
      {
       // destination group has an approval; check to see if we need to overwrite it
       if (destinationApproval.Action !=sourceApproval.Action)
       {
        // the approvals are different; overwrite
        Console.WriteLine(
         “Changing approval for update {0} from {1} to {2} for group {3}.”,
         update.Title,
         destinationApproval.Action,
         sourceApproval.Action,
         destinationGroup.Name);
        update.Approve(sourceApproval.Action, destinationGroup);
       }
      }
      else
      {
       // destination group does not have an approval; approve
       Console.WriteLine(
        “Approving update {0} for {1} for group {2}.”,
        update.Title,
        sourceApproval.Action,
        destinationGroup.Name);
       update.Approve(sourceApproval.Action, destinationGroup);
      }
     }
    }
   }
   catch (Exception e)
   {
    Console.WriteLine(e.Message);
   }
  }
}

——————–8<———————-

2 thoughts on “WSUS TOOL: Copying Approvals Between Target Groups

Leave a Reply

Your email address will not be published. Required fields are marked *