Advertisement
tsk138

Cleanup-Wsus.ps1

Mar 29th, 2017
527
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ?<#
  2. Created:     2015-06-10
  3. Version:     1.5
  4. Author :     Peter Lofgren
  5. Twitter:     @LofgrenPeter
  6. Blog   :     http://syscenramblings.wordpress.com
  7.  
  8. Disclaimer:
  9. This script is provided "AS IS" with no warranties, confers no rights and
  10. is not supported by the author
  11. #>
  12.  
  13. <#
  14. .Synopsis
  15.    Does Cleanup of the WSUS database.
  16. .DESCRIPTION
  17.    Will initiate a WSUS database cleanup and log output to a logfile.
  18. .EXAMPLE
  19.    Cleanup-WSUS.ps1
  20. .EXAMPLE
  21.    Cleanup-WSUS.ps1 -LogFile C:\WSUSLog\Cleanup.log
  22. #>
  23.  
  24. [CmdletBinding(SupportsShouldProcess=$true)]
  25. param (
  26. [Parameter(Mandatory=$false)]
  27. $logFile = "C:\Windows\Temp\Wsus.log"
  28. )
  29.  
  30. Add-Content -Path $logFile -Value "$(get-date) Cleanup starting"
  31. [reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")
  32. $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer()
  33. $cleanupScope = new-object Microsoft.UpdateServices.Administration.CleanupScope;
  34. $cleanupScope.DeclineSupersededUpdates = $true        
  35. $cleanupScope.DeclineExpiredUpdates = $true
  36. $cleanupScope.CleanupObsoleteUpdates = $true
  37. $cleanupScope.CompressUpdates = $true
  38. $cleanupScope.CleanupObsoleteComputers = $true
  39. $cleanupScope.CleanupUnneededContentFiles = $true
  40. $cleanupManager = $wsus.GetCleanupManager();
  41. $Result = ($cleanupManager.PerformCleanup($cleanupScope))
  42. Add-Content -Path $logFile -Value "SupersededUpdatesDeclined: $($Result.SupersededUpdatesDeclined)"
  43. Add-Content -Path $logFile -Value "ExpiredUpdatesDeclined: $($Result.ExpiredUpdatesDeclined)"
  44. Add-Content -Path $logFile -Value "ObsoleteUpdatesDeleted: $($Result.ObsoleteUpdatesDeleted)"
  45. Add-Content -Path $logFile -Value "UpdatesCompressed: $($Result.UpdatesCompressed)"
  46. Add-Content -Path $logFile -Value "ObsolotetComputersDeleted: $($Result.ObsoleteComputersDeleted)"
  47. Add-Content -Path $logFile -Value "DiskSpaceFreed: $($Result.DiskSpaceFreed)"
  48. Add-Content -Path $logFile -Value "$(get-date) Cleanup completed"
  49. Add-Content -Path $logFile -Value ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement