rj07thomas

optimzeWsus

May 17th, 2021
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. <#
  2. Performs a series of steps to clear out WSUS:
  3. - Declines all superseded updates;
  4. - Uses Invoke-WsusServerCleanup to perform indivdual cleanup tasks in sequence;
  5. - Runs the SQL optimisation T-SQL script from https://docs.microsoft.com/en-us/troubleshoot/mem/configmgr/reindex-the-wsus-database
  6. - Restarts WSUS services (stops completely then starts);
  7. - Restarts IIS (stops completely then restarts);
  8. #>
  9.  
  10. Clear-Host
  11.  
  12. # START DECLINE PROCESS
  13. [void][reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")
  14.  
  15. Clear-Host
  16.  
  17. $script:wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer('<WSUS_SERVER>', $false, '8530')
  18. $<WSUS_SERVER>
  19. $unwantedUpdates = $wsus.GetUpdates() | Where-Object {$_.IsDeclined -like 'False' -and $_.IsSuperseded -like 'True'}
  20. <#$unwantedUpdates.Title
  21. $unwantedUpdates.Count#>
  22.  
  23. ForEach($update in $unwantedUpdates)
  24. {
  25. $update.Title
  26. $update.Decline()
  27. }
  28. # STOP DECLINE PROCESS #
  29.  
  30. # START CLEAN PROCESS
  31. Invoke-WsusServerCleanup -CleanupObsoleteComputers
  32. Invoke-WsusServerCleanup -CleanupObsoleteUpdates
  33. Invoke-WsusServerCleanup -CleanupUnneededContentFiles
  34. Invoke-WsusServerCleanup -CompressUpdates
  35. Invoke-WsusServerCleanup -DeclineExpiredUpdates
  36. Invoke-WsusServerCleanup -DeclineSupersededUpdates
  37. # STOP CLEAN PROCESS #
  38.  
  39. # START SQL OPTIMISATION PROCESS
  40. cmd /c 'C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\sqlcmd.exe' -S \\.\pipe\MICROSOFT##WID\tsql\query -E -i C:\Support\Scripts\WSUS\dbBackup\reindexWSUS.sql
  41. # STOP SQL OPTIMISATION PROCESS #
  42.  
  43. # RESTART/STOP WSUS SERVICES
  44. Restart-WebAppPool WsusPool
  45. Restart-Service 'MSSQL$MICROSOFT##WID'
  46. Restart-Service WsusService
  47. Restart-Service w3logsvc
  48. Stop-Service W3SVC
  49. Stop-Service WAS
  50. Restart-Service wuauserv
  51. #
  52.  
  53. # START WSUS SERVICES
  54. Start-Service w3logsvc
  55. Start-Service W3SVC
  56. Start-Service WAS
  57. #
  58.  
  59. Start-Sleep 20
  60.  
  61. Get-Service 'MSSQL$MICROSOFT##WID'
  62. Get-Service WsusService
  63. Get-Service w3logsvc
  64. Get-Service W3SVC
  65. Get-Service WAS
  66. Get-Service wuauserv
  67. Get-WebAppPoolState WsusPool
  68.  
  69. # RESTART IIS
  70. cmd /c iisreset /stop
  71. cmd /c iisreset /start
  72. ##
Advertisement
Add Comment
Please, Sign In to add comment