talama

WSUS Automatic Cleanup Script

May 7th, 2017
1,215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Region VARIABLES
  2.  
  3. # WSUS Connection Parameters:
  4. ## Change settings below to your situation. ##
  5. # FQDN of the WSUS server
  6. [String]$parentServer = "wsus.yourdomain.local"
  7. # Use secure connection $True or $False
  8. [Boolean]$useSecureConnection = $False
  9. [Int32]$portNumber = 80
  10. # From address of email notification
  11. [String]$emailFromAddress = "[email protected]"
  12. # To address of email notification
  13. [String]$emailToAddress = "[email protected]"
  14. # Subject of email notification
  15. [String]$emailSubject = "WSUS Cleanup Results"
  16. # Exchange server
  17. [String]$emailMailserver = "mailserver.yourdomain.local"
  18.  
  19.  
  20. # Cleanup Parameters:
  21. ## Set to $True or $False ##
  22. # 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.
  23. [Boolean]$supersededUpdates = $True
  24. # Decline updates that aren't approved and have been expired my Microsoft.
  25. [Boolean]$expiredUpdates = $True
  26. # Delete updates that are expired and have not been approved for 30 days or more.
  27. [Boolean]$obsoleteUpdates = $True
  28. # Delete older update revisions that have not been approved for 30 days or more.
  29. [Boolean]$compressUpdates = $True
  30. # Delete computers that have not contacted the server in 30 days or more.
  31. [Boolean]$obsoleteComputers = $True
  32. # Delete update files that aren't needed by updates or downstream servers.
  33. [Boolean]$unneededContentFiles = $True
  34.  
  35. #EndRegion VARIABLES
  36.  
  37. #Region SCRIPT
  38.  
  39. # Load .NET assembly
  40. [void][reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration");
  41.  
  42. # Connect to WSUS Server
  43. $wsusParent = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($parentServer,$useSecureConnection,$portNumber);
  44.  
  45. # Log the date first
  46. $DateNow = Get-Date
  47.  
  48. # Perform Cleanup
  49. $Body += "$parentServer ($DateNow ) :" | Out-String
  50. $CleanupManager = $wsusParent.GetCleanupManager();
  51. $CleanupScope = New-Object Microsoft.UpdateServices.Administration.CleanupScope($supersededUpdates,$expiredUpdates,$obsoleteUpdates,$compressUpdates,$obsoleteComputers,$unneededContentFiles);
  52. $Body += $CleanupManager.PerformCleanup($CleanupScope) | Out-String
  53.  
  54. #Get list of downstream servers
  55. $wsusDownstreams = [Microsoft.UpdateServices.Administration.AdminProxy]::DownstreamServerCollection;
  56. $wsusDownstreams = $wsusParent.GetDownstreamServers();
  57.  
  58. #Clean each downstream server
  59. $wsusDownstreams | ForEach-Object {
  60.             $ping = New-Object System.Net.NetworkInformation.Ping
  61.             $DSServer = $_.FullDomainName
  62.             Try{
  63.                 $Reply = $ping.send($DSServer)
  64.                 $ReplyStatus = $Reply.Status
  65.                 Write-Host $ReplyStatus
  66.             }
  67.             catch{
  68.                 $ReplyStatus = "False"
  69.                 Write-Host $ReplyStatus
  70.             }
  71.             if ($ReplyStatus -eq "Success")
  72.             {
  73.                 # Log the date first
  74.                 $DateNow = Get-Date
  75.                 $Body += $DSServer + " ($DateNow ) : " | Out-String
  76.                 $wsusReplica = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($_.FullDomainName,$useSecureConnection,$portNumber);
  77.                 $CleanupManager = $wsusReplica.GetCleanupManager();
  78.                 $CleanupScope = New-Object Microsoft.UpdateServices.Administration.CleanupScope($supersededUpdates,$expiredUpdates,$obsoleteUpdates,$compressUpdates,$obsoleteComputers,$unneededContentFiles);
  79.                 $Body += $CleanupManager.PerformCleanup($CleanupScope) | Out-String
  80.             }else{
  81.                 # Log the date first
  82.                 $DateNow = Get-Date
  83.                 $Body += $DSServer + " ($DateNow ) : not pingable`n" | Out-String
  84.             }
  85. }
  86.  
  87. # Send the results in an email
  88. Send-MailMessage -From $emailFromAddress -To $emailToAddress -Subject $emailSubject -Body $Body -SmtpServer $emailMailserver
  89.  
  90. #EndRegion SCRIPT
Advertisement
Add Comment
Please, Sign In to add comment