Thursday, June 29, 2017

Cleanup WSUS content folder with PowerShell

WSUS drive alerting for low disk space? Run this script and go get some coffee!

This script is the equivalent of running the WSUS cleanup wizard with all checkboxes checked.


Function Start-WSUSCleanup{
<#
.SYNOPSIS
Cleans up a WSUS content folder.

.DESCRIPTION
This script is the equivalent of running a WSUS cleanup with all checkboxes checked from within the WSUS console> options > WSUS Cleanup. `
It scrunbs the WSUS content folder for unneeded data.

.EXAMPLE
PS C:\> Start-WSUSCleanup

.NOTES
Version : 3.0+
Author/Copyright : © Matthew Kerfoot - All Rights Reserved
Email/Blog/Twitter : mkkerfoot@gmail.com www.TheOvernightAdmin.com @mkkerfoot

Disclaimer : THIS CODE IS MADE AVAILABLE AS IS, WITHOUT WARRANTY OF ANY KIND. THE ENTIRE RISK
OF THE USE OR THE RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
While these scripts are tested and working in my environment, it is recommended
that you test these scripts in a test environment before using in your production environment
Matthew Kerfoot 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 this script and documentation remains with you.
In no event shall Matthew Kerfoot, its authors, or anyone else involved in the creation, production,
or delivery of this script/tool 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 Matthew Kerfoot has been advised of the possibility of such damages.

Assumptions : ExecutionPolicy of AllSigned (recommended), RemoteSigned or Unrestricted (not recommended)
Limitations : not all functions work with PSv1
Ideas/Wish list : Now working with PSv2, PSv3, PSv4 and PSv5
Known issues : None
Authors notes : I found this somewhere on the internet and adapted it.. not sure where..
#>


[CmdletBinding(SupportsShouldProcess=$True)]
    param(
                [Parameter(Mandatory=$false,ValueFromPipeline=$true)]
                $Date = (Get-date -Format MMddyyyy),
                [Parameter(Mandatory=$false,ValueFromPipeline=$true)]
                $OutFilePath = "$env:USERPROFILE\Desktop\wsusCleanUpResults_$date.txt"
             )
            # Loads the WSUS .NET assembly - should probably be changed to load as loadwithpartial name is outdated
            # This is piped to out-null so that the next line won't run until the WSUS assembly is fully loaded
            [reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null

                # retrieves the primary WSUS servers information
                $wsusServer = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer();

                # Gets the Cleanup scope options, this is the same as the radio buttons in the WSUS console `
                # Switch any of them to false to disable cleaning up that kind of data
                $cleanupScope = new-object Microsoft.UpdateServices.Administration.CleanupScope;

                        # $True means that item will be scrubbed and data will be deleted!
                        $cleanupScope.DeclineSupersededUpdates    = $true       
                        $cleanupScope.DeclineExpiredUpdates       = $true
                        $cleanupScope.CleanupObsoleteUpdates      = $true
                        $cleanupScope.CompressUpdates             = $true
                        $cleanupScope.CleanupObsoleteComputers    = $true
                        $cleanupScope.CleanupUnneededContentFiles = $true

                # Opens WSUS cleanup console as a background task
                $cleanupManager = $wsusServer.GetCleanupManager();

                    # Initiats the cleanup and writes outputs to $OutputFile
                    $cleanupManager.PerformCleanup($cleanupScope) | Out-File -FilePath $OutFilePath -Verbose

                    # Opens the output file so you can see how many items this script cleaned up
                    Invoke-Item $OutFilePath -Verbose

} Start-WSUSCleanup