How to Determine Update Status for all Computers
Applies To: Windows Server Update Services
Use the following procedure to query the WSUS server for its connected clients and their update status. The procedure details the WSUS API calls necessary to retrieve this information. The example that follows the procedure is a complete command-line application that takes this data and outputs it to an XML file for reporting purposes.
To determine update status for all computers
Add the
Microsoft.UpdateServices.Administration
assembly to your project.Insert a
using
statement for theMicrosoft.UpdateServices.Administration
namespace.using Microsoft.UpdateServices.Administration;
Instantiate an
IUpdateServer
object by using the staticAdminProxy.GetUpdateServer
method.IUpdateServer updateServer = AdminProxy.GetUpdateServer();
Instantiate a
ComputerTargetScope
object, and set its properties according to the computers with which you will work.In the following code snippet, the
ComputerTargetScope.IncludeDownstreamComputerTargets
property is set to true. This property specifies whether or not clients of a downstream server, not clients of this server, should be included. After theComputerTargetScope
object has been initialized and its properties set, call theIUpdateServer.GetComputerTargets
method that will return, in the form of aComputerTargetCollection
object, all of the computers that match the criteria specified with theComputerTargetScope
object.// Get the collection of computers on this server. ComputerTargetScope computerTargetScope = new ComputerTargetScope(); computerTargetScope.IncludeDownstreamComputerTargets = true; ComputerTargetCollection computers = updateServer.GetComputerTargets(computerTargetScope);
Iterate over the computers collection, calling the
IComputerTarget.GetUpdateInstallationInfoPerUpdate
method for each computer. This method gets the installation information for all drivers and software updates for this computer and returns aUpdateInstallationInfoCollection
object.foreach (IComputerTarget computer in computers) { // Get the install state of all updates approved for this computer. UpdateInstallationInfoCollection installStatus = computer.GetUpdateInstallationInfoPerUpdate(); // Iterate over the IUpdateInstallationInfo object to access information, such as the name of the updates // and their install state on the current computer. foreach (IUpdateInstallationInfo updateStatus in installStatus) { // Perform your work here, such as generating a report } }
Example
The following is the complete code listing for a console application that write to an XML file the current update status on all computers connected to the WSUS server. The application takes a single parameter, indicating whether or not downstream computers should be included.
/*----------------------------------------------------------------------
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.
----------------------------------------------------------------------*/
namespace WsusSamples
{
using System;
using System.Xml;
using Microsoft.UpdateServices.Administration;
class DetermineUpdateStatusForAllComputers
{
static void Main(string[] args)
{
const string usage = "\r\n" +
"ComputerStatusToXml. Creates an XML file with a list computers on the Update Services server, " +
"and the status of updates approved for each computer." +
"\r\n\r\n" +
"USAGE:" +
"\r\n\r\n" +
" ComputerStatusToXml.exe [IncludeDownstreamComputers]" +
"\r\n\r\n" +
" IncludeDownstreamComputers: Include computer targets rolled up from downstream servers." +
"\r\n";
// evaluate args
bool includeDownstreamComputers = false;
if (args.Length >= 1)
{
if (args.Length > 1 || args[0].ToLower() != "includedownstreamcomputers")
{
Console.WriteLine(usage);
return;
}
if (args[0].ToLower() == "includedownstreamcomputers")
{
includeDownstreamComputers = true;
}
}
XmlTextWriter xml = null;
try
{
Console.WriteLine("Getting computer status...");
// open the XML file
string xmlWriterFile = string.Format("{0}{1}{2}{3}",
Environment.CurrentDirectory.ToString(),
"\\ComputerStatus ",
DateTime.Now.ToLongDateString().ToString(),
".xml");
xml = new XmlTextWriter(xmlWriterFile, System.Text.Encoding.UTF8);
xml.Formatting = Formatting.Indented;
xml.Indentation = 4;
xml.WriteStartDocument(true);
xml.WriteStartElement("ComputerStatus");
// Connect to the local WSUS server
IUpdateServer updateServer = AdminProxy.GetUpdateServer();
// Get the collection of computers on this server
ComputerTargetScope computerTargetScope = new ComputerTargetScope();
computerTargetScope.IncludeDownstreamComputerTargets = includeDownstreamComputers;
ComputerTargetCollection computers = updateServer.GetComputerTargets(computerTargetScope);
// loop through the computer collection
foreach (IComputerTarget computer in computers)
{
// start the element for the computer
xml.WriteStartElement("Computer");
xml.WriteAttributeString("Name", computer.FullDomainName);
xml.WriteAttributeString("LastReportedStatus", computer.LastReportedStatusTime.ToString());
if (includeDownstreamComputers == true)
{
string parentName = "LocalHost";
if (computer.ParentServerId != Guid.Empty)
{
parentName = computer.GetParentServer().FullDomainName;
}
xml.WriteAttributeString("ParentServer", parentName);
}
// start the element for the status of updates on this computer
xml.WriteStartElement("UpdateStatus");
// get the install state of all updates approved for this computer
UpdateInstallationInfoCollection installStatus = computer.GetUpdateInstallationInfoPerUpdate();
// loop through the updates in the install info collection and output the
// name of the update and it's install state on this computer
foreach (IUpdateInstallationInfo updateStatus in installStatus)
{
xml.WriteStartElement("Update");
xml.WriteAttributeString("Title", updateStatus.GetUpdate().Title);
xml.WriteAttributeString("Status", updateStatus.UpdateInstallationState.ToString());
// close the Update element
xml.WriteEndElement();
}
// close the UpdateStatus element
xml.WriteEndElement();
// close the Computer element
xml.WriteEndElement();
} // foreach
} //try
catch (Exception)
{
} // catch
finally
{
Console.WriteLine("Done. Results are written to the ComputerStatus {0}.xml file in the current folder",
DateTime.Now.ToLongDateString());
xml.Close();
} // finally
} // main
}
}