Advertisement
anonit

WSUS Cleanup Server 2012

Feb 16th, 2016
766
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # SERVER 2012 WSUS CLEANUP
  2.  
  3. # This will perform the following actions on a WSUS 3.0 SP2 server running on server 2012:
  4. # Run the cleanup wizard;
  5. # Reindex the database;
  6. # Decline itanium updates;
  7. # Shrink the database.
  8. # Optionally send an email with results from the above
  9.  
  10. # Requirements:
  11. # WSUSDBMaintenance script: https://gallery.technet.microsoft.com/scriptcenter/6f8cde49-5c52-4abd-9820-f1d270ddea61
  12. # SQL Management Studio: https://www.microsoft.com/en-us/download/details.aspx?id=43351
  13.  
  14. # Configuration
  15. # SendReport - $TRUE will send the email / $FALSE will not
  16. # WsusServer - Name or IP Address of WSUS Server
  17. # UseSSL - Is the WSUS server using SSL or not
  18. # PortNumber - WSUS Port Number
  19. # SMTPServer - Name or IP address of SMTP server for email
  20. # FromAddress - email address the email will come from
  21. # Recipients - email address the email will be sent to
  22. # MessageSubject - Subject of the email
  23. # SQLCMDFILEPATH - location to sqlcmd.exe from SQL Management Studio
  24. # HypenI - "-i" to allow the sqlcmd.exe process to run the WSUSDBMaintenance.sql script
  25. # WSUSCLEANUPSCRIPT - location to WSUSDBMaintenance.sql script - available https://gallery.technet.microsoft.com/scriptcenter/6f8cde49-5c52-4abd-9820-f1d270ddea61
  26. # WSUSSHRINKCOMMAND - sql command to shrink the database
  27. # HypenS - "-S" to allow the sqlcmd.exe process to connect to a server
  28. # HypenQ - "-Q" to allow the sqlcmd.exe process to run the Shrink command
  29. # SQLDatabaseConnection - Connection string to WSUS database
  30.  
  31. #Report Configuration
  32. $SendReport=$TRUE
  33.  
  34. #WSUS Configuration
  35. $WsusServer = "server08"
  36. $UseSSL = $false
  37. $PortNumber = 8530
  38.  
  39. #E-mail Configuration
  40. $SMTPServer = "smtp.anonit.net"
  41. $FromAddress = "wsus@anonit.net"
  42. $Recipients = "maintenance@anonit.net"
  43. $MessageSubject = "WSUS maintenance"
  44.  
  45. #SQL Configuration
  46.  
  47. # If SQL Management studio is a different version than 2012 R2 - change the path to sqlcmd.exe below
  48. $SQLCMDFILEPATH="c:\program files\microsoft sql server\110\tools\binn\sqlcmd.exe"
  49. $WSUSCLEANUPSCRIPT="C:\maintenance\WSUSDBMaintenance.sql"
  50.  
  51. #------------------------------------------------------------
  52.  
  53. # The following probably doesn't need to be changed
  54.  
  55. $WSUSSHRINKCOMMAND='"DBCC ShrinkDatabase (SUSDB,10)"'
  56. $HypenS="-S"
  57. $HypenQ="-Q"
  58. $HypenI="-i"
  59. # Server 2008 / SQL2008 should use 'np:\\.\pipe\MSSQL$MICROSOFT##SSEE\sql\query'
  60. $SQLDatabaseConnection='np:\\.\pipe\Microsoft##WID\tsql\query'
  61.  
  62. #------------------------------------------------------------
  63.  
  64. Function SendEmailStatus($MessageSubject, $MessageBody)
  65. {
  66.     $SMTPMessage = New-Object System.Net.Mail.MailMessage $FromAddress, $Recipients, $MessageSubject, $MessageBody
  67.     $SMTPMessage.IsBodyHTML = $false
  68.     $SMTPClient = New-Object System.Net.Mail.SMTPClient $SMTPServer
  69.     $SMTPClient.Send($SMTPMessage)
  70.     $SMTPMessage.Dispose()
  71.     rv SMTPClient
  72.     rv SMTPMessage
  73. }
  74.  
  75. #------------------------------------------------------------
  76.  
  77. # Out-Null used to prevent writing to console
  78.  
  79. # Run the WSUS Cleanup Wizard
  80. Invoke-WsusServerCleanup -CleanupObsoleteComputers -CleanupObsoleteUpdates -CleanupUnneededContentFiles -DeclineExpiredUpdates -DeclineSupersededUpdates  | Tee-Object -Variable WSUSCleanupResults | Out-Null
  81.  
  82.  
  83. # Reindex the database
  84. & $SQLCMDFILEPATH $HypenS $SQLDatabaseConnection $hypenI $WSUSCLEANUPSCRIPT | Tee-Object -Variable SQLCleanupResults | Out-Null
  85.  
  86.  
  87. # Decline itanium updates
  88. # The following two scripts were combined to give the best results.
  89. # http://learn-powershell.net/2013/11/12/automatically-declining-itanium-updates-in-wsus-using-powershell/
  90. # https://gallery.technet.microsoft.com/scriptcenter/Automatically-Declining-a4fec7be#content
  91. #Connect to the WSUS 3.0 interface.
  92. [reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null
  93. $WsusServerAdminProxy = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer($WsusServer,$UseSSL,$PortNumber);
  94. $approveState = 'Microsoft.UpdateServices.Administration.ApprovedStates' -as [type]
  95. $updateScope = New-Object Microsoft.UpdateServices.Administration.UpdateScope -Property @{
  96.     TextIncludes = 'itanium'
  97.     ApprovedStates = $approveState::NotApproved,
  98.     $approveState::LatestRevisionApproved,
  99.     $approveState::HasStaleUpdateApprovals
  100. }
  101. $itanium = $WsusServerAdminProxy.GetUpdates($updateScope) | ?{-not $_.IsDeclined -and $_.Title -match “ia64|itanium”}
  102. $itanium | %{$_.Decline()} | Tee-Object -Variable itaniumdeclineresults
  103.  
  104.  
  105. # Shrink the database
  106. & $SQLCMDFILEPATH $HypenS $SQLDatabaseConnection $HypenQ $WSUSSHRINKCOMMAND | Tee-Object -Variable SQLShrinkResults | Out-Null
  107.  
  108.  
  109. If ($itanium.Count -gt 0)
  110. {
  111.     $ITaniumUpdateName=""
  112.     ForEach ($ItaniumUpdate in $Itanium)
  113.     {
  114.         $ItaniumUpdateName=$ItaniumUpdateName+$ItaniumUpdate.Title+"`r`n"
  115.     }
  116.     $ItaniumMessageBody=$ItaniumUpdateName
  117. }
  118. else
  119. {
  120.     $ItaniumMessageBody="No Itanium Updates to decline"
  121. }
  122.  
  123. $MessageBreak="`r`n`r`n`r`n-------------------------------------------------------------------------------------------`r`n`r`n`r`n"
  124. $MessageBody1=$WSUSCleanupResults+$MessageBreak
  125. $MessageBody2=$SQLCleanupResults+$MessageBreak
  126. $MessageBody2=$MessageBody2 -replace "  ","`r`n"
  127. $MessageBody3=$SQLShrinkResults+$MessageBreak
  128. $MessageBody4=$ItaniumMessageBody+$MessageBreak
  129. $MessageBody=$MessageBody1+$MessageBody2+$MessageBody3+$MessageBody4
  130.  
  131. if ($SendReport)
  132. {
  133.     SendEmailStatus $MessageSubject $Messagebody
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement