Script Center > Gallery > Windows Update > WSUS Move Approvals from Out-of-Date Revisions
TechNet Script Center logo

Welcome to the TechNet Script Center Gallery!

Each contribution is licensed to you under a License Agreement by its owner, not Microsoft. Microsoft does not guarantee the contribution or purport to grant rights to it.

WSUS Move Approvals from Out-of-Date Revisions

(Microsoft)
VERIFIED AND TESTED BY THE SCRIPT CENTER TEAM
Rate it:
 
 
 
 
 
Script Code
Windows PowerShell

$doRecommendedAction = $false
if ($args[0] -ne $null)
{
  if (($args[0].ToUpper() -eq "-Q") -or ($args[0].ToUpper() -eq "-QUIET"))
  {
    $doRecommendedAction = $true
  }
  else
  {
    write-host "Usage: manage-approvals.ps1 [<-quiet>/<-q>]"
    exit
  }
}

# Load administration
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.UpdateServices.Administration') | out-null


# Create update server
write-host "<<< Connecting to WSUS server >>>" -foregroundcolor "yellow"
$updateServer = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer()
write-host ""

# Get all updates and loop through them to find out if there are any unapproved ones 
# with older revisions that are approved
$updates = $updateServer.GetUpdates()
$count = 1
$didNothing = $true

foreach ($update in $updates)
{
    if (($update.IsApproved -eq $false) -and ($update.HasEarlierRevision -eq $true))
    {
        $revisions = $update.GetRelatedUpdates([Microsoft.UpdateServices.Administration.UpdateRelationship]::AllRevisionsOfThisUpdate)
        
        # Find the first revision that has any approval on it
        foreach ($revision in $revisions)
        {
            if ($revision.IsApproved)
            {
                $didNothing = $false
                $approvals = $revision.GetUpdateApprovals()
                foreach ($approval in $approvals)
                {
                  $targetGroup = $approval.GetComputerTargetGroup()
                    write-host "Update #" $count
                    write-host "----------"
                    write-host " Update ID         :" $update.Id.UpdateId.ToString()
                    write-host " Update title      :"$update.Title
                    write-host " Current state     : An older revision is approved for" $approval.Action.ToString() "for target group" $targetGroup.Name
    
                    $action = "s" # skip by default
                    
                    if ($update.PublicationState -eq [Microsoft.UpdateServices.Administration.PublicationState]::Expired)
                    {
            if ($doRecommendedAction -eq $true)
            {
              # Recommended action: Decline update
              $action = "d"
            }
            else
            {
              # Recommended action: Decline update
              write-host " Recommended action: Decline this update" -foregroundcolor "blue" -backgroundcolor "yellow"
              write-host ""
              $action = read-host " Decline (d)/Skip (s or Enter)"
            }
                    }
                    else
                    {
            if ($doRecommendedAction -eq $true)
            {
              # Recommended action: Move approval to the latest revision
              $action = "m"
            }
            else
            {
              # Recommended action: Move approval to the latest revision
              write-host " Recommended action: Move this approval to the latest revision" -foregroundcolor "blue" -backgroundcolor "yellow"
              write-host ""
              $action = read-host " Move (m)/Skip (s or Enter)"
            }
                    }

                    switch ($action)
                    {
                        "d" # Decline
                        {
                            write-host " Declining update ..."
                            $approval.Delete()
                            $update.Decline()
                            write-host " Done!"
                        }
                        
                        "m" # Move
                        {
                            write-host " Moving approval ..."
                            $approval.Delete()
                            $update.Approve($approval.Action, $targetGroup, $approval.Deadline) | out-null
                            write-host " Done!"
                        }
                        
                        default
                        {
                            write-host " Skipping"
                        }
                    }
                    
                    write-host ""
                    $count = $count + 1
                }
            }
        }
    }
}

if ($didNothing -eq $true)
{
    write-host "No updates detected that have approvals for older revisions"
}

trap
{
    write-host "Error Occurred"
    write-host "Exception Message: " 
    write-host $_.Exception.Message
    write-host $_.Exception.StackTrace
    exit
}

# EOFEnumerateAD($ds.SearchRoot);
Platforms
Windows Server 2008 R2 Yes
Windows Server 2008 Yes
Windows Server 2003 Yes
Windows 7 No
Windows Vista No
Windows XP No
Windows 2000 No
For online peer support, join The Official Scripting Guys Forum! To provide feedback or report bugs in sample scripts, please start a new discussion on the Discussions tab for this script.
Disclaimer The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.
Be the first to create a discussion.