locked
error in command line to do a wsus cleanup RRS feed

  • Question

  • Hi,

    I wanted to make a script to automate cleaning of the wsus.  All my rules work quite good, just on (last) rule gives me a time-out. (i'm fairly new to scripting).  I've been looking to solve it but can't seem to find what the problem is.

    The last line that gives the error is :   $CleanupManager.PerformCleanup($CleanupScope)

    When i execute this line i get the following message :
    Exception calling “PerformCleanup” with “1″ argument(s): “Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
    The statement has been terminated.”
    At line:1 char:31
    + $CleanupManager.PerformCleanup <<<< ($CleanupScope)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

    I suggest that the cleanup is not executed then ?  How can i avoid the timeout ?
    Any help would be appriciated.

    Regards,
    Jo

    Thursday, May 30, 2013 12:15 PM

Answers

All replies

  • going to need to see more of your script cant really go of 1 line
    Thursday, May 30, 2013 12:24 PM
  • I"m sorry about that, underneath the complete commented script :

    #VARIABLES

    # From address for email notifications. You can name it to whatever you want.
    [String]$emailFromAddress = "noresponse@fqdn.com"

    # To address for email notifications. The recipient whom needs to receive emails upon the cleanup completion.
     [String]$emailToAddress = "myaddress@fqdn.com"

    # Subject of email notification
     [String]$emailSubject = "WSUS Cleanup Results"

    # Enter your FQDM for Exchange server
    [String]$emailMailserver = "smtp.server"

    # Cleanup Parameters:
     ## Set to $True or $False ##
     # Decline updates that have not been approved for 30 days or more, are not currently needed by any clients, and are superseded by an approved update.
     [Boolean]$supersededUpdates = $True
     # Decline updates that aren't approved and have been expired my Microsoft.
     [Boolean]$expiredUpdates = $True
     # Delete updates that are expired and have not been approved for 30 days or more.
     [Boolean]$obsoleteUpdates = $True
     # Delete older update revisions that have not been approved for 30 days or more.
     [Boolean]$compressUpdates = $True
     # Delete computers that have not contacted the server in 30 days or more.
     [Boolean]$obsoleteComputers = $True
     # Delete update files that aren't needed by updates or downstream servers.
     [Boolean]$unneededContentFiles = $True
     
    #End VARIABLES



    #SCRIPT

    # Load .NET assembly
     [void][reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration");
     
    # Connect to WSUS Server
     $wsusParent = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer();
     
    # Log the date first
     $DateNow = Get-Date
     
    # Perform Cleanup
     $Body += " ($DateNow ) :" | Out-String
    $CleanupManager = $wsusParent.GetCleanupManager();
     $CleanupScope = New-Object Microsoft.UpdateServices.Administration.CleanupScope($supersededUpdates,$expiredUpdates,$obsoleteUpdates,$compressUpdates,$obsoleteComputers,$unneededContentFiles);
     $Body += $CleanupManager.PerformCleanup($CleanupScope) | Out-String

    # Mail the report...
     $message = new-object Net.Mail.MailMessage
     $mailer = new-object Net.Mail.SmtpClient($emailMailserver)
     
    # From address for email notifications. You can name it to whatever you want.
     $message.From = $EmailFromAddress
     # To address for email notifications. The recipient whom needs to receive emails upon the cleanup completion.
     $message.To.Add($emailToAddress)
     $message.Subject = "Windows Update - Server Clean-Up Wizard"
     $message.Body = ($Body)
     
    $mailer.Send($message)

    #End SCRIPT

    Thursday, May 30, 2013 12:58 PM
  • Hi,

    You've got a line break where you're declaring $CleanupScope. Try putting that all on one line. Also, why do you have hanging semicolons on some lines?

    Thursday, May 30, 2013 1:12 PM
  • There isn't a way to increase the timeout of the cleanup. I've had this issue before and the best workaround is to run each item at a time in the cleanup vs. trying to run all of the options. The error isn't in PowerShell but in theWSUS server database.

    Here is a great link that can help you to possibly mitigate the timeout issues:

    http://thwack.solarwinds.com/community/application-and-server_tht/patchzone/blog/2013/01/14/wsus-timeout-errors--when-and-why-eliminating-and-avoiding


    Boe Prox
    Blog | PoshWSUS | PoshPAIG | PoshChat

    Thursday, May 30, 2013 1:19 PM
  • @Mike

    It is one line, but in notepad the line is split in 2.  I removed the hanging semicolons. Thanks for the tips.

    @Boe

    I"m looking into the link you posted.  Also i"m in the process of re-Indexing the wsus database to speed him up and to see if the error (timeout) still persists

    I'l post back with the result.

    Regards,

    Jo

    Links for the re-indexing :

    http://gallery.technet.microsoft.com/ScriptCenter/6f8cde49-5c52-4abd-9820-f1d270ddea61/

    http://www.madanmohan.com/2010/10/sms-wsus-synchronization-failed.html

    Thursday, May 30, 2013 2:00 PM
  • Hi,

    Posting back with the results.  After the re-indexing of the wsus database, i didn't get a time out.  Altough after about 14 hours i got the following error :

    Exception calling "PerformCleanup" with "1" argument(s): "Cannot open database "SUSDB" requested by the login. The login failed. Login failed for user 'NT AUTHORITY\NETWORK SERVICE'."

    Any help with this error would be great.

    Regards,

    Jo

    Friday, May 31, 2013 6:31 AM
  • Hi Jo,

    Take a look at the script on the following link:

    http://www.peetersonline.nl/2009/04/wsus-cleanup-with-powershell/

    There's a different options used there for the getUpdateServer() method, perhaps adjusting the connection will help?

    • Marked as answer by Yan Li_ Thursday, June 6, 2013 2:11 AM
    Friday, May 31, 2013 1:41 PM
  • Hi,

    I have 2 colleagues who are sick :-( so i"m falling in for them, so i'm swappend in work for the moment. So i have no time to further troubleshoot the issue.  I'll will surely post back all the steps i did to make it work, one of the comming days.

    Regards,

    Jo

    Thursday, June 6, 2013 6:50 AM
  • Hi,

    I’m back, after lots of work, I was able to further troubleshoot the problem. So these are the steps I took to solve my problem.

    First I do a database maintenance (reindex) to speed up the wsus database (before doing my cleanup)

    Make a batch file with the following rule :

    sqlcmd -i c:\tools\wsuscleanup\wsusdbmaintenance.sql -S \\.\pipe\MSSQL$MICROSOFT##SSEE\sql\query -E -I

    The SQL file, "wsusdbmaintenance.sql", you can find here :  http://technet.microsoft.com/en-us/library/dd939795(v=ws.10).aspx

    Put the batch file in the task scheduler and run on regular base or before you do the cleanup. Run with highest privileges and when user is logged on or not.

     

    Secondly, for the wsus cleanup I also made a batch file with the following rules :

    @echo off

    @echo Starting cleanup: %date% %time% >> C:\Tools\WSUSCleanup\WSUS_Cleanup.log

    powershell.exe C:\Tools\WSUSCleanup\wsuscleanupscript.ps1 >> C:\Tools\WSUSCleanup\WSUS_Cleanup.log

    @echo Finished cleanup: %date% %time% >> C:\Tools\WSUSCleanup\WSUS_Cleanup.log

     

    Underneath the rules in my script (wsuscleanupscript.ps1) :

    #Region VARIABLES

    # WSUS Connection Parameters:

    [String]$updateServer = "your servername"

    [Boolean]$useSecureConnection = $False

    [Int32]$portNumber = 8530

    # Email parameters:

    [String]$emailFromAddress = "noresponse@yourdomain.com"

    [String]$emailToAddress = "john.doe@yourdomain.com"

    [String]$emailSubject = "WSUS Cleanup Results"

    [String]$emailMailserver = "smtp server"

    # Log the date first

    $DateNow = Get-Date

    #EndRegion VARIABLES

    #Region SCRIPT

    # Load .NET assembly

    [reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null

    # Connect to WSUS Server

    $wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer()

    # Perform Cleanup

    $Body += " ($DateNow ) :" | Out-String

    $cleanupScope = new-object Microsoft.UpdateServices.Administration.CleanupScope

    $cleanupScope.DeclineSupersededUpdates = $true       

    $cleanupScope.DeclineExpiredUpdates = $true

    $cleanupScope.CleanupObsoleteUpdates = $true

    $cleanupScope.CompressUpdates = $true

    #$cleanupScope.CleanupObsoleteComputers = $true

    $cleanupScope.CleanupUnneededContentFiles = $true

    $cleanupManager = $wsus.GetCleanupManager()

    $Body += $cleanupManager.PerformCleanup($cleanupScope) | Out-String

    # Mail the report

    $message = new-object Net.Mail.MailMessage

    $mailer = new-object Net.Mail.SmtpClient($emailMailserver)

    $message.From = $EmailFromAddress

    $message.To.Add($emailToAddress)

    $message.Subject = "Windows Update - Server Clean-Up Wizard"

    $message.Body = ($Body)

    $mailer.Send($message)

    #EndRegion SCRIPT

     

    Put the batch file in the task scheduler and run on regular base, so that the wsus doesn’t grow big. Run with highest privileges and when user is logged on or not.

     

     

     

    If I first ran the database maintenance and the cleanup. I had a problem connecting to the Microsoft##SEE SQL instance which is in control for the wsus (i you have choosen Internal database when installing wsus). So I did 2 things :

    Before connecting to the database instance, enable the network protocols.

    Enable  named Pipes  and  TCP/IP

    On the server start the SQM Server Configuration Manager  (run as Admin)

    Expand the  SQL Server Network Configuration and select the  Protocols for MICROSOFT##SEE

    In the right window enable  TCP/IP  and  Named Pipes

     

    Now connect to the SQL database, by starting the SQL Server Management Studio, to connect to the following instance MICROSOFT##SE,

    with the following credentials :

    Run as Admin   (important)

    Server Type :  Database Engine

    Server Name : \\.\pipe\MSSQL$MICROSOFT##SSEE\sql\query

    Authentication :  Windows Authentication

    Go to Security, logins and right click on the NT AUTHORITY\NETWORK SERVICE and select properties.

    Go to the User Mapping,  select the database  SUSDB and in the role membership, select db_owner

    Click OK

     

     

    I hope this will help to setup or troubleshoot your wsus database cleanup ( automated). Thanks for all the tips and hints i got in this post.

     

    Regards,

    Jo

     

     

     




    Thursday, June 13, 2013 3:09 PM
  • I know this is an old thread, but my script does what you're looking for and WAY more.

    Have a peek at my Adamj Clean-WSUS script. It is the last WSUS Script you will ever need.

    http://community.spiceworks.com/scripts/show/2998-adamj-clean-wsus

    What it does:

    1. Remove all Drivers from the WSUS Database.
    2. Shrink your WSUSContent folder's size by declining superseded updates.
    3. Remove declined updates from the WSUS Database.
    4. Clean out all the synchronization logs that have built up over time (configurable, with the default keeping the last 14 days of logs).
    5. Compress Update Revisions.
    6. Remove Obsolete Updates.
    7. Computer Object Cleanup (configurable, with the default of deleting computer objects that have not synced within 30 days).
    8. Application Pool Memory Configuration to display the current private memory limit and easily increase it by any configurable amount.
    9. Run the Recommended SQL database Maintenance script on the actual SQL database.
    10. Run the Server Cleanup Wizard.

    It will email the report out to you or save it to a file, or both.

    Although the script is lengthy, it has been made to be super easy to setup and use. There are some prerequisites and instructions at the top of the script. After installing the prerequisites and configuring the variables for your environment, simply run:

    .\Clean-WSUS.ps1 -FirstRun

    and then

    .\Clean-WSUS.ps1 -InstallTask

    If you wish to view or increase the Application Pool Memory Configuration, you must run it with the required switch. See Get-Help .\Clean-WSUS.ps1 -Examples

    If you're having trouble, there's also a -HelpMe option that will create a log so you can send it to me for support.

    • Proposed as answer by AJTek.caMVP Friday, May 12, 2017 3:31 PM
    Friday, May 12, 2017 3:29 PM