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

7 comments:

  1. Very good written article. It will be supportive to anyone who utilizes it, including me. Keep doing what you are doing can’t wait to read more posts.
    Packers and movers in Delhi

    ReplyDelete
  2. Having read this I thought it was very enlightening. I appreciate you taking the time and energy to put this informative article together.
    Download quickbooks tool hub

    https://qbcuservice.com/download-quickbooks-tool-hub/

    ReplyDelete
  3. Really enjoyed this article post. Really looking forward to read more. Will read on...
    Transfer large files

    ReplyDelete
  4. Really informative article post. Really thank you! Really Great. best portfolio folder

    ReplyDelete
  5. Writing prompts can help you practice different writing techniques, such as using metaphor or imagery. Generate Prompts for Midjourney

    ReplyDelete
  6. The way to having an effective content creation methodology is guaranteeing that you are growing new and interesting content on a normal basis.
    ChatGPT Prompts

    ReplyDelete