Windows: Cleanup files from WSUS which are no longer needed

Hi,

the content of an WSUS Server crows every month for several GiBytes because WSUS does held all files which has the approval state “install”.

The GUI way to cleanup the WSUS Content folder is to first decline all expired and superseded updates and than run the WSUS Cleanup Wizard.

But you can also use the script way 🙂

[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")

# Only needed if the script doesn't run on the WSUS Server
[String]$sWSUSServer = "yourWSUSServer"
[Boolean]$bSecureConnection = $False
[Int32]$iWSUSPort = 8531
try
{
       if([String]::IsNUllOrEmpty($sWSUSServer))
       {
            $oWSUSServer = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer()
       }
       else
       {
	    $oWSUSServer = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer($sWSUSServer,$bSecureConnection,$iWSUSPort)
       }
}
catch [Exception]
{
	write-warning ("Cannot connect to WSUS Server: $sWSUSServer. Error: " + $_)
        exit 1
}


$oCleanUpManger=$oWSUSServer.GetCleanUpManager()

$oCleanUpScope=new-object Microsoft.UpdateServices.Administration.CleanupScope
$oCleanUpScope.DeclineSupersededUpdates=$true
$oCleanUpScope.DeclineExpiredUpdates=$true
$oCleanUpScope.CleanupUnneededContentFiles=$true
$oCleanUpScope.CleanupLocalPublishedContentFiles=$true
$oCleanUpScope.CleanupObsoleteUpdates=$false
$oCleanUpScope.CompressUpdates=$false
$oCleanUpScope.CleanupObsoleteComputers=$false

$oCleanUpManger.PerformCleanup($oCleanUpScope)

Run the script

PS C:\> .\WSUSCleanup.ps1
SupersededUpdatesDeclined : 18
ExpiredUpdatesDeclined    : 0
ObsoleteUpdatesDeleted    : 0
UpdatesCompressed         : 0
ObsoleteComputersDeleted  : 0
DiskSpaceFreed            : 274123310463

Update:
Now there is also a Powershell command let which does the same job

PS C:\> Invoke-WsusServerCleanup -CleanupObsoleteUpdates -CleanupUnneededContentFiles -DeclineExpiredUpdates -DeclineSupersededUpdates

If you often got the “Reset Server Node Error” you should do a maintenance task on your WSUS Database. Microsoft offers on SQL script for this issue.

You need the sqlcmd command line tool which is part of MSSQL Management Studio

To start maintenance. Open a cmd shell as Administrator and execute the SQL Script. The database path is for a SQL Express installation. If you use an SQL Server you have to adjust it.

C:\> sqlcmd -S np:\\.\pipe\MSSQL$MICROSOFT##SSEE\sql\query –i D:\temp\WsusDBMaintenance.sql

Or just use the MSSQL Management Studio, connect to the database, open a query, copy the SQL Statement and execute it.

Michael

References
Maintenance Guide
Reindex WSUS DB
Decline-SupersededUpdatesWithExclusionPeriod.ps1
Decline-SupersededUpdatesWithExclusionPeriod.ps1 runs in timeout or error 401
WSUS Cleaning wizard runs in timeout

Advertisment to support michlstechblog.info

WSUS Cleanup
WSUS Cleanup
WSUSCleanup.ps1
Version: 1.0.0.0
1.5 KiB
860 Downloads
Details...

2 thoughts on “Windows: Cleanup files from WSUS which are no longer needed”

    1. Hi Daniel,

      have tried it on the same machine the wsus runs?

      Have you definied a system proxy?
      netsh winhttp show proxy

      Michael

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.