Skip to main content

WSUS Server Cleanup Step by Step

WSUS Server Cleanup

Recently, i checked my WSUS Server that was installed on Server 2012 R2 and the D Drive was showing Full. My WSUS is storing Updates and all WSUS data on the D Drive. That means it was time for WSUS Server Cleanup.

The D Drive looked like this:

WSUS Disk Full

WSUS Server Cleanup Wizard:

First option was to run the Server Cleanup Wizard. So i opened the WSUS console, under the options ran the Server cleanup wizard but after few minutes it gave the following error.

WSUS Server Cleanup Error

WSUS Server Cleanup Script:

So it was time for some other method to optimize the server and cleanup the storage. While searching for the solution i found this WSUS Database Cleanup Script.

Edit: The website hosting the script is down, so i am copying the script here.

===========================================

Add-Type -Path “C:\Program Files\Update Services\API\Microsoft.UpdateServices.Administration.dll”

$UseSSL = $False
$PortNumber = 8530
$Server = “WSUS1.domain.com”
$ReportLocation = “C:\WSUS_CleanUpReport.html”
#$SMTPServer = “Mail.Contoso.com”
#$To = “IT Admin <[email protected]>”
#$FROM = “IT Admin <[email protected]>”

$WSUSConnection = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer($Server,$UseSSL,$PortNumber)

#Clean Up Scope
$CleanupScopeObject = New-Object Microsoft.UpdateServices.Administration.CleanupScope
$CleanupScopeObject.CleanupObsoleteComputers = $True
$CleanupScopeObject.CleanupObsoleteUpdates = $True
$CleanupScopeObject.CleanupUnneededContentFiles = $True
$CleanupScopeObject.CompressUpdates = $True
$CleanupScopeObject.DeclineExpiredUpdates = $True
$CleanupScopeObject.DeclineSupersededUpdates = $True

$CleanupTASK = $WSUSConnection.GetCleanupManager()

$Results = $CleanupTASK.PerformCleanup($CleanupScopeObject)

$DObject = New-Object PSObject
$DObject | Add-Member -MemberType NoteProperty -Name “SupersededUpdatesDeclined” -Value $Results.SupersededUpdatesDeclined
$DObject | Add-Member -MemberType NoteProperty -Name “ExpiredUpdatesDeclined” -Value $Results.ExpiredUpdatesDeclined
$DObject | Add-Member -MemberType NoteProperty -Name “ObsoleteUpdatesDeleted” -Value $Results.ObsoleteUpdatesDeleted
$DObject | Add-Member -MemberType NoteProperty -Name “UpdatesCompressed” -Value $Results.UpdatesCompressed
$DObject | Add-Member -MemberType NoteProperty -Name “ObsoleteComputersDeleted” -Value $Results.ObsoleteComputersDeleted
$DObject | Add-Member -MemberType NoteProperty -Name “DiskSpaceFreed” -Value $Results.DiskSpaceFreed

#HTML style
$HeadStyle = “<style>”
$HeadStyle = $HeadStyle + “BODY{background-color:peachpuff;}”
$HeadStyle = $HeadStyle + “TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}”
$HeadStyle = $HeadStyle + “TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:thistle}”
$HeadStyle = $HeadStyle + “TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:palegoldenrod}”
$HeadStyle = $HeadStyle + “</style>”

$Date = Get-Date

$DObject | ConvertTo-Html -Head $HeadStyle -Body “<h2>$($ENV:ComputerName) WSUS Report: $date</h2>” | Out-File $ReportLocation

#Send-MailMessage -To $To -from $FROM -subject “WSUS Clean Up Report” -smtpServer $SMTPServer -Attachments $ReportLocation

==========================================

Change the lines in red and save it as Invoke-WSUSCleanupTask.ps1

It is a great scrip that cleans up the WSUS and generates a report about the actions taken.

So i downloaded the script and ran through Powershell on the WSUS Server.  But after half an hour i received the following error:

WSUS Server Cleanup Script Error

The script was mentioning about “Connection was closed”, so i ran the script again, next time it gave error after around 1 hour. That means i twas doing its work, but getting the session disconnected by the Server. So i continued to run the script, after few unsuccessful runs it completed without error.

WSUS Server Cleanup Script Success

Next thing was to check the report, so i open the the report and found the following:

WSUS ServerCleanup Script Report

This script cleared around 70 GB of space. After this successful cleanup the disk looked like this:

WSUS Server Disk After Cleanup

Lastly, you can optimize the WSUS WID database through Microsoft SQL Server Management Studio as i explained previously.

2 thoughts on “WSUS Server Cleanup Step by Step

Leave a Reply

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