Discussion:
Approvals Gropu WUS
(too old to reply)
Support Technique
2005-12-06 17:35:56 UTC
Permalink
I wish to transfer approvals from a group towards another group.
How to make?
Thank you..
Torgeir Bakken (MVP)
2005-12-06 19:08:32 UTC
Permalink
Post by Support Technique
I wish to transfer approvals from a group towards another group.
How to make?
Thank you..
Hi,

Nothing builtin for this, but you can create a command line utility
yourself that does the job (source code and build command line supplied
below). WSUS expose .NET API's that can be called from VB.NET, C#.NET,
or other .NET languages.

The easiest way to develop .NET programs is to use Visual Studio, but VS is
not required. The .NET Framework ships with all the basic tools necessary
for building .NET programs.

To get a tool that copies update approvals from one computer group to
another, do the following on the WSUS server (C# below is previously
posted to the newsgroup by Yasufumi Shiraishi [MSFT]):

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<----------------------
--
torgeir, Microsoft MVP Scripting, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx
maximillianx
2005-12-06 19:35:35 UTC
Permalink
Nice post Torgeir!

This is information I wasn't aware of...

I'll be using this extensively.

Rob
Post by Torgeir Bakken (MVP)
Post by Support Technique
I wish to transfer approvals from a group towards another group.
How to make?
Thank you..
Hi,
Nothing builtin for this, but you can create a command line utility
yourself that does the job (source code and build command line supplied
below). WSUS expose .NET API's that can be called from VB.NET, C#.NET,
or other .NET languages.
The easiest way to develop .NET programs is to use Visual Studio, but VS is
not required. The .NET Framework ships with all the basic tools necessary
for building .NET programs.
To get a tool that copies update approvals from one computer group to
another, do the following on the WSUS server (C# below is previously
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
%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
http://download.microsoft.com/download/7/4/5/7458e392-11de-4543-936c-b5248e344487/readme.htm
)
--------------------8<----------------------
using System;
using Microsoft.UpdateServices.Administration;
// 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<----------------------
--
torgeir, Microsoft MVP Scripting, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
http://www.microsoft.com/technet/scriptcenter/default.mspx
The Tech Guy
2012-07-30 22:33:59 UTC
Permalink
I did this the CopyApprovalsBetweenGroups.exe tool was created in the directory but when I run it with my from and to groups I get a pop up error

"Windows can't open this file:

File: CopyApprovalsBetweenGroups.cs

What did I miss?

Let me know

thank you

Jon

Loading...