WSUS Maintenance Utility

Clean up your WSUS

WSUS Maintenance Utility can also be downloaded from:

Please consider supporting my work:

  • Sign up using Patreon.
  • Support with a one-time payment using PayPal.

If you’d like to get in touch with me please leave a comment, send me a tweet or DM, or send me a message via my contact form.

-Mike

 

Features and Requirements

  • It’s designed to run either on a WSUS server itself or can be run from a remote machine.
  • The computer that is running the utility must have the WSUS management PowerShell modules installed.
  • The utility requires at least PowerShell 5.0.

This utility has been tested on Windows 10, Windows Server 2019 and Windows Server 2016.

 

Generating A Password File

The password used for SMTP server authentication must be in an encrypted text file. To generate the password file, run the following command in PowerShell on the computer and logged in with the user that will be running the utility. When you run the command, you will be prompted for a username and password. Enter the username and password you want to use to authenticate to your SMTP server.

Please note: This is only required if you need to authenticate to the SMTP server when send the log via e-mail.

$creds = Get-Credential
$creds.Password | ConvertFrom-SecureString | Set-Content c:\scripts\ps-script-pwd.txt

After running the commands, you will have a text file containing the encrypted password. When configuring the -Pwd switch enter the path and file name of this file.

 

Configuration

The table below shows all the command line options available with descriptions and example configurations.

Command Line Switch Description Example
-Server The WSUS server to run the maintenance routine on. wsus01
-Port The port WSUS is running on the server. If you do not configure this, the default port of 8530 will be used. If the WsusSSL switch is used the default port will be 8531. 6969
-WsusSsl Use this option if your WSUS server uses SSL. N/A
-NoBanner Use this option to hide the ASCII art title in the console. N/A
-L The path to output the log file to. The file name will be WSUS-Maint_YYYY-MM-dd_HH-mm-ss.log. Do not add a trailing \ backslash. C:\scripts\logs
-Subject The subject line for the e-mail log. Encapsulate with single or double quotes. If no subject is specified, the default of “WSUS Maintenance Utility Log” will be used. ‘Server: Notification’
-SendTo The e-mail address the log should be sent to. me@contoso.com
-From The e-mail address the log should be sent from. WSUS-Maint@contoso.com
-Smtp The DNS name or IP address of the SMTP server. smtp.live.com OR smtp.office365.com
-User The user account to authenticate to the SMTP server. example@contoso.com
-Pwd The txt file containing the encrypted password for SMTP authentication. C:\scripts\ps-script-pwd.txt
-UseSsl Configures the utility to connect to the SMTP server using SSL. N/A

 

Example

WSUS-Maintenance.ps1 -Server wsus01 -L C:\scripts\logs
-Subject 'Server: WSUS Maintenance' -SendTo me@contoso.com
-From WSUS-Maint@contoso.com -Smtp smtp.outlook.com
-User me@contoso.com -Pwd C:\foo\pwd.txt -UseSsl

The above command will run the maintenance on the server wsus01 using the default port. The log file will be output to C:\scripts\logs and sent via e-mail with a custom subject line.

 

Change Log

2020-03-20: Version 20.03.20

  • Added code contribution from ideas@habs.homelinux.net.
  • Individual cleanup jobs now run separately.
  • Improved reporting.
  • Made slight improvements to documentation.

2020-03-05: Version 20.03.03 ‘Burger’

  • Added SSL option for connecting to the WSUS server.
  • Made the -Port switch optional. If it is not specified, the default port is used. If -WsusSsl is specified, the default port for SSL is used.
  • Added config report.
  • Added ASCII banner art when run in the console.
  • Added option to disable the ASCII banner art.
  • Refactored code.
  • Fully backwards compatible.

2019-09-04 v1.8

  • Added custom subject line for e-mail.

2019-04-23 v1.7

  • The script will now not run the clean up process twice.
  • The script will now report if the service isn’t running before starting.

2017-10-16 v1.6

  • Changed SMTP authentication to require an encrypted password file.
  • Added instructions on how to generate an encrypted password file.

2017-10-07 v1.5

  • Added necessary information to add the script to the PowerShell Gallery.

2017-09-25 v1.4

  • Cleaned up formatting, minor changes to code for efficiency.

2017-08-11 v1.3

  • Improved, cleaner logging. The log file is no longer produced from PowerShell’s Transcript function.

2017-07-22 v1.2

  • Improved commenting on the code for documentation purposes.
  • Added authentication and SSL options for e-mail notification.

2017-05-22 v1.1

  • Added configuration via command line switches.

 

16 thoughts on “WSUS Maintenance Utility

Add yours

    1. Hi Cory,
      I’ve not tested it on a SCCM installation with WSUS, but I see no reason why it shouldn’t work. As as I understand it WSUS is essentially the same, even when it is installed with SCCM.

      Like

  1. Hi Mike,
    I’m trying to run your script for the first time and I’m not sure if it’s doing anything or not. I’m running via PS ISE, it just says running script press CTRL BREAK to stop, but I set it up with a log and I’m not seeing a log being generated. Is this normal, I mean does the log not get generated until the end?

    Like

    1. Hi there,

      The script is designed to be run from Task Scheduler (or command line). If you configure the log output, the log will be created as the script is run. It can take a long time to run depending on if the WSUS server has been cleaned up recently. The script is designed to keep WSUS tidy and responsive by running it regularly.

      -Mike

      Like

  2. I get the following error. Could it be b/c it hasn’t been run, and no maintenance has been done since it’s inception 6 months ago?

    Invoke-WsusServerCleanup : Execution Timeout Expired. The timeout period elapsed prior to completion of the operation
    or the server is not responding.
    The statement has been terminated.
    At C:\_scripts\Wsus-Maintenance.ps1:133 char:34
    + … susServer | Invoke-WsusServerCleanup -CleanupObsoleteComputers -Clean …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidData: (Microsoft.Updat…rCleanupCommand:InvokeWsusServerCleanupCommand) [Invoke-
    WsusServerCleanup], SqlException
    + FullyQualifiedErrorId : UnexpectedError,Microsoft.UpdateServices.Commands.InvokeWsusServerCleanupCommand

    Like

    1. Hi Brian, Yes I would say that that’s why it’s not running correctly. Although the script does an pretty good job at keeping WSUS clean and running well, I’ve found that running it on an older server that has never, or has not had any maintenance done recently cause the script to fail. If you run the clean up wizard first then the script should run successfully afterwards. You may have to run the cleanup wizard multiple times – just keep running it until it gives a successful completion report.

      -Mike

      Like

  3. Hi there,

    running the script on a Windows Server 2012 R2 as a task, I get an incomplete logfile:
    (Running it manually in Powershell Console or PowerShell ISE does work with complete output logfile…

    ****************************************
    4/26/2018 9:46:46 AM Log started

    4/26/2018 9:46:46 AM WSUS maintenance routine starting…

    4/26/2018 9:46:47 AM Log finished
    ****************************************

    Running the script with the same parameters and settings as a task on a Windows Server 2016 gives me a detailed output:

    ****************************************
    26.04.2018 09:49:13 Log started

    26.04.2018 09:49:13 WSUS maintenance routine starting…

    Gelöschte veraltete Updates:0
    Abgelehnte abgelaufene Updates: 0
    Gelöschte veraltete Updates:0
    Komprimierte Updates:32
    Freigegebener Speicherplatz:0

    26.04.2018 09:49:48 Log finished
    ****************************************
    Does anybody has the same problem?

    Regards,
    Roland

    Like

    1. Hi Roland,

      This may or may not help but I’ve had the first result (the less detailed one) on Windows Server 2016 when the cleanup wizard hasn’t been able to complete. I solved it by:

      1) Rebooting the server
      2) Running the clean up wizard manually via Settings in the WSUS MMC

      After I did these things, the script ran normally.

      Not sure if it will help with Windows Server 2012 R2, but hopefully it will help.

      -Mike

      Like

  4. Hi there !

    Little suggestion to make this script more friendly with other langages.

    We use french server over here, and the out file and the email were getting a lot of crazy caracters.

    So I modified those lines :

    $WsusMaintCmd | Out-File -Append $Log -Encoding UTF8
    $MailBody = Get-Content -Path $Log -Encoding UTF8 | Out-String

    and then added -Encoding UTF8 to each line starting with Send-Mail-Message

    Thank for your great work !

    Like

Leave a Reply to Cory Cancel reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: