WSUS automated cleanup script

The script runs WSUS cleanup tasks using stored procedures in WSUS database directly, avoiding timeout errors and WSUS service crash that may occur when using WSUS' Cleanup Wizard.

 
 
 
 
 
4.3 Star
(18)
16,149 times
Add to favorites
Windows Update
3/12/2018
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Got time out anyway
    2 Posts | Last post June 07, 2019
    • Hi
      I did use the script in PowerShell ISE because the console crashed with timeout
      ... as did the script :
      
      Reading obsolete update list.
      Unhandled exception:
      Exception calling "ExecuteReader" with "0" argument(s): "Execution Timeout Expired.  The timeout period elapsed prior to completion of the operation or the server is not 
      responding."
      Completed script execution with 1 error(s)
      Execution time 0 hours and 10 minutes.
      
    • Hi,
      have a look at the script itself. For every action there are timeouts defined. I got the same error as you and I'm now adjusting the timeouts and will try again
  • Doesn't work
    3 Posts | Last post April 08, 2019
    • Exequting locally, hostname and db name checked.
      
      PS C:\Users\admin> C:\Temp\wsus-cleanup-updates-v4.ps1
      Exception calling "SourceExists" with "1" argument(s): "The source was not found, but some or all event logs could not be searched.  Inaccessible logs: Security."
      At C:\Temp\wsus-cleanup-updates-v4.ps1:43 char:10
      +     if ( -not [System.Diagnostics.EventLog]::SourceExists($log_source ...
      +          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordException
          + FullyQualifiedErrorId : SecurityException
    • I didn't work too
    • Looks like the account running the script does not have permissions to list event sources and/or create a new one. Try running the script using an account with local administrator permissions on the server or replace logging code to dump ifo to a file instead of event log. Something like
      
      function log_init{
        "Log started" | Out-File "c:\temp\wsuscleanup.log" -force
      }
      
      function log( [string] $msg, [int32] $eventID, [System.Diagnostics.EventLogEntryType] $level ){
        "$level : $eventID : $msg" | Out-File "c:\temp\wsuscleanup.log" -append
      }
  • Was working now it is throwing an error
    2 Posts | Last post April 08, 2019
    • Unhandled exception:
      Cannot determine SQL server name
      Completed script execution with 1 error(s)
      Execution time 0 hours and 0 minutes.
      
      I am using WIDS DB not SQL.
    • When running the script remotely (not on the WSUS server), you will need to spesify sql server name in the $SqlServer variable at the top of the script. 
  • Not showing any saved space
    2 Posts | Last post April 08, 2019
    • It says that it is deleting updates (## of 8120)
      but it does not appear to be cleaning any space from the drive
    • the script will only attempt to delete unused content files when running locally on WSUS server (sql server name variable at the top of the script is left empty). The cleanup itself is done by invoking WSUS' own cleanup procedure. If it reports that 0 bytes were freed, it means that that deleted updates had no content stored locally. This is expected when WSUS is configured to download content after update is approved. 
  • OS ?
    1 Posts | Last post March 19, 2019
    • nice work but in what version did you run?
  • Connection Error
    1 Posts | Last post December 03, 2018
    • Hi,
      
      I got errors while trying to run the script.
      
      OS: Windows Server 2016 Standart
      Server Version: 10.0.14393.2608
      DB: WID
      
      Error Message:
      wsus-cleanup-updates-v4.ps1
      
      Connecting to database SUSDB on MICROSOFT##WID
      Unhandled exception:
      Exception calling "Open" with "0" argument(s): "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The
       server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (pr
      ovider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
      Completed script execution with 1 error(s)
      Execution time 0 hours and 0 minutes.
      
  • Error message
    1 Posts | Last post November 03, 2018
    • This is the output I receive:
      
      Connecting to database SUSDB on MICROSOFT##WID
      Declining expired updates
      Unhandled exception:
      Cannot validate argument on parameter 'Message'. The argument length of 19027271 is too long. Shorten the length of the
      argument to less than or equal to "32766" and then try the command again.
      Completed script execution with 2 error(s)
      Execution time 1 hours and 60 minutes.
      
      WS2012 WSUS, using WID
  • Connection error
    2 Posts | Last post October 11, 2018
    • I also receive the same error "xception calling "Open" with "0" argument(s): "A network-related or instance-specific error occurred while establishing
      " . I have found this error is only on Server 2008 and Server 2008R2 systems, the script is working wel on later server versions. Is there something to change in the script for Server 2008/2008R2 to work?
      
      Thank you
    • I am seeing the following error for each of the updates slated for deletion:
      Deleting update 250406 (134 of 4525)
      Error deleting update 250406:
      Exception calling "ExecuteNonQuery" with "0" argument(s): "@errorMessage
      spDeleteUpdate got error from spDeleteRevision"
      
      Windows Server 2016, WSUS 10.0.14393.2485 using WID
  • Error database
    2 Posts | Last post October 05, 2018
    • Hello,
      When run the script after some seconds i recieve this error >
      
      Connecting to database SUSDB on WSUS\MICROSOFT##SSEE
      Unhandled exception:
      Exception calling "Open" with "0" argument(s): "A network-related or instance-specific error occurred while establishing
       a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct an
      d that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open
       a connection to SQL Server)"
    • Could you please provide more details about your setup? OS version, WSUS version, WSUS db config?