WSUS Database cleanup/update using PowerShell

Using Powershell to cleanup the WSUS database, free disk space increased 12 GB.

#Region VARIABLES
# WSUS Connection Parameters:
# YOUR SERVER NAME
[String]$updateServer = "SERVER NAME"
[Boolean]$useSecureConnection = $False
# Port number
[Int32]$portNumber = 8530

# Cleanup Parameters:
# Decline updates that have not been approved for 30 days or more, are not currently needed by any clients, and are superseded by an aproved update.
[Boolean]$supersededUpdates = $True
# Decline updates that aren’t approved and have been expired my Microsoft.
[Boolean]$expiredUpdates = $True
# Delete updates that are expired and have not been approved for 30 days or more.
[Boolean]$obsoleteUpdates = $True
# Delete older update revisions that have not been approved for 30 days or more.
[Boolean]$compressUpdates = $True
# Delete computers that have not contacted the server in 30 days or more.
[Boolean]$obsoleteComputers = $True
# Delete update files that aren’t needed by updates or downstream servers.
[Boolean]$unneededContentFiles = $True

#EndRegion VARIABLES

#Region SCRIPT

# Load .NET assembly
[void][reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")

# Connect to WSUS Server
$Wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($updateServer,$useSecureConnection,$portNumber)

# Perform Cleanup
$CleanupManager = $Wsus.GetCleanupManager()
$CleanupScope = New-Object Microsoft.UpdateServices.Administration.CleanupScope($supersededUpdates,$expiredUpdates,$obsoleteUpdates,$compressUpdates,
$obsoleteComputers,$unneededContentFiles)
$CleanupManager.PerformCleanup($CleanupScope)

#EndRegion SCRIPT

If you want to do a forced update.

#Region VARIABLES

# WSUS Connection Parameters:
# YOUR SERVER NAME
[String]$updateServer = "SERVER NAME"
[Boolean]$useSecureConnection = $False
# Port number
[Int32]$portNumber = 8530

#EndRegion VARIABLES
#Region SCRIPT
# Load .NET assembly
[void][reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")
# Connect to WSUS Server
$Wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($updateServer,$useSecureConnection,$portNumber)
# Perform Synchronization
$Subscription = $Wsus.GetSubscription()
$Subscription.StartSynchronization()
#EndRegion SCRIPT

This entry was posted in PowerShell, Windows Server 2008 and tagged , . Bookmark the permalink.

Leave a Reply

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