共用方式為


Determining the Progress or Status of the Synchronization Process

 

Applies To: Windows Server Update Services

The WSUS API exposes the status, progress, and results of the synchronization process. To retrieve the status of the synchronization process, call ISubscription.GetSynchronizationStatus. The synchronization status indicates if the synchronization process is currently running.

While the synchronization process is running, you can call ISubscription.GetSynchronizationProgress to retrieve its progress. If the server is a replica server, the synchronization process occurs in two phases: the first phase synchronizes the update metadata, and the second phase synchronizes the deployments. The progress information reflects only on the progress of the current phase, not both phases. For non-replica servers, the server synchronizes only the update metadata.

To determine if the synchronization process ended successfully, call ISubscription.GetLastSynchronizationInfo. Then, call ISynchronizationInfo.Result and compare the value with SynchronizationResult.Succeeded.

WSUS downloads the update files and required license agreements in parallel with synchronizing the update metadata. If IUpdateServerConfiguration.HostBinariesOnMicrosoftUpdate is true, WSUS downloads only the license agreements. To check the download progress of the update files and license agreements, call IUpdateServer.GetContentDownloadProgress.

The synchronization process can take a long time to complete depending on how long it has been since you last synchronized the WSUS server.

The following example shows how to check the status and progress of the synchronization process. The server variable that is used in the example is an instance of IUpdateServer (for an example that retrieves an IUpdateServer instance and sets the preferred culture, see Using the Windows Server Update Services 3.0 Class Library).

ISubscription subscription = null;

//These two objects contain the properties and methods for 
//retrieving the synchronization status information.
subscription = server.GetSubscription();

//If the synchronization process is currently running, print its progress.
while (SynchronizationStatus.Running == subscription.GetSynchronizationStatus())
{
  SynchronizationProgress progress = subscription.GetSynchronizationProgress();

  Console.WriteLine("{0}% of {1} synchronized.", 
      100*progress.ProcessedItems/progress.TotalItems,
      (SynchronizationPhase.Updates) ? "updates" : "approvals");

  System.Threading.Thread.Sleep(500);
};

//If the synchronization process is not running, check if it has succeeded.
if (SynchronizationStatus.NotProcessing == subscription.GetSynchronizationStatus())
{
  ISynchronizationInfo lastSyncInfo = subscription.GetLastSynchronizationInfo();

  //Check the result to see if the synchronization process has succeeded.
  switch (lastSyncInfo.Result)
  {
    case SynchronizationResult.Succeeded :
      Console.WriteLine("Synchronization completed on {0}.", 
                        lastSyncInfo.EndTime.ToLocalTime().ToString());
      break;

    case SynchronizationResult.Failed :
      Console.WriteLine("Synchronization failed with error, {0}, on {1}.", 
                        lastSyncInfo.Error, lastSyncInfo.EndTime.ToLocalTime().ToString());
      Console.WriteLine(lastSyncInfo.ErrorText);

      //The synchronization process could also fail with an import error, 
      //so also check UpdateErrors.
      foreach (SynchronizationUpdateErrorInfo importError in lastSyncInfo.UpdateErrors)
      {
        Console.WriteLine("Failed to import update, {0}\nReason: {1}\n",
                          server.GetUpdate(importError.UpdateId).Title,
                          importError.ErrorText);
      }

      break;

    case SynchronizationResult.Canceled :
    {
      Console.WriteLine("The synchronization process was canceled.");
      break;
    }

    case SynchronizationResult.NeverRun : 
      Console.WriteLine("Synchronization has never been run on this WSUS server.");
      break;

    case SynchronizationResult.Unknown :
      Console.WriteLine("Synchronization ended for an unknown reason.");
      break;

    default :
      Console.WriteLine("Unknown synchronization result code, {0}.", lastSyncInfo.Result);
      break;
  }

  if (true == subscription.SynchronizeAutomatically)
  {
    Console.WriteLine("The next synchronization process is scheduled to occur at {0}", 
                      subscription.GetNextSynchronizationTime().ToLocalTime().ToString());
  }
}
else
{
  Console.WriteLine("The synchronization process is stopping. Try again later.");
}