Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Powershell Script for WSUS Cleanup Help needed

Posted on 2016-07-29
12
Medium Priority
?
423 Views
Last Modified: 2016-08-08
Windows 2012 R2 Server
SQL 2012 Enterprise
WSUS 6.3

I found this power shell script that runs in 3 stages to cleanup WSUS databases.

Back on WSUS 3.2 you had wsuscleanup.exe program that I had running and it would email me the Cleanup Results.

This script I have it emailing me but I cant get the Cleanup Results to be sent in the email

When I run the script from power shell it does produce the clean up results at the end of the script to the console.

How do I get the cleanup results output into the email?

See attached script

Any ideas

Thanks in advance

Tom
wsus-servercleanup.txt
0
Comment
Question by:Thomas Grassi
  • 6
  • 3
  • 3
12 Comments
 
LVL 41

Expert Comment

by:footech
ID: 41735290
The only thing that it should output to console is "Emailing WSUS Cleanup Report".
Other than that it emails everything.  If you're not getting the email then I would say you need to change the details at the end of the script to work with your SMTP server.
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 41735298
footech

Sorry I was not clear

I am getting the email I get this
==========================================================================

WSUS Cleanup Report

Beginning WSUS Cleanup on SERVER at 07/25/2016 14:00:31
Starting Pass 1 of 3: Unused Updates and Update Revisions Only
Completed Pass 1 of 3 in: 00:15:50.5986073.hours hrs 00:15:50.5986073.minutes min 00:15:50.5986073.seconds sec
Starting Pass 2 of 3: Unneeded Update Files Only
Completed Pass 2 of 3 in: 00:00:59.3293990.hours hrs 00:00:59.3293990.minutes min 00:00:59.3293990.seconds sec
Starting Pass 3 of 3: Stale Computers, Expired Updates and Superseded Updates
Completed Pass 3 of 3 in: 00:00:07.4845806.hours hrs 00:00:07.4845806.minutes min 00:00:07.4845806.seconds sec
Total Run Time: 00:16:57.6782140.hours hrs 00:16:57.6782140.minutes min 00:16:57.6782140.seconds sec
—————————————————————————-
Identified 0 DownStream WSUS Servers
Script is Complete
Total Run Time: days 00:17:04.4041492.days 00:17:04.4041492.hours hrs 00:17:04.4041492.minutes min 00:17:04.4041492.seconds sec
—————————————————————————-


I would like to see the cleanup results added to this email
0
 
LVL 41

Expert Comment

by:footech
ID: 41735715
Can you provide a screenshot of what the cleanup results look like?
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
LVL 23

Author Comment

by:Thomas Grassi
ID: 41735727
footech

Cleanup Results:
265070901byte(s) removed from the WSUS Content directory.
0 expired update(s) were declined in WSUS.
0 computer(s) were removed from WSUS.
256 obsolete update(s) were removed from WSUS.
0 superseded update(s) were declined in WSUS.
7258 Unneeded revision(s) were removed from WSUS.


This is what I am missing from the email
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 41735733
The script is overcomplicated and confusing - e.g. the setting of the cleanup tasks for each pass are made somewhere else than the progress description naming those actions; code parts are repeated for main and downstream servers, ...

The main server code does not include the results of the cleanup call, the downstream server code does (but it is wrong).

The next issue is that you need to use $($var.method(...)) inside of double quotes. The same applies to object properties. Only simple variables will be expanded correctly inside of double quotes without enclosing in a sub-expression $(). You see that on the times reported.

I don't think the output of the cleanup task would display nicely the simple way it is used in the downstream server code, so we should apply some more formating, shown for one fragment of the main server code:
# Perform First Pass on “Unused Updates and Update Revisions Only”
$StartFirstPass = Get-Date
$ReportOutput += “<p>Beginning WSUS Cleanup on $PrimaryWSUSServer at $StartFirstPass</p>”
$ReportOutput += “<p>Starting Pass 1 of 3: Unused Updates and Update Revisions Only</p>”

$CleanupPrimary1 = New-Object Microsoft.UpdateServices.Administration.CleanupScope($FirstPassSupersededUpdates,$FirstPassExpiredUpdates,$FirstPassObsoleteUpdates,$FirstPassCompressUpdates,$FirstPassObsoleteComputers,$FirstPassUnneededContentFiles)
$ReportOutput += "<p>" + $CleanupManager.PerformCleanup($CleanupPrimary1) | ConvertTo-HTML -Fragment + "</p>"
 $EndFirstPass = Get-Date
 $TotFirstPass = $EndFirstPass-$StartFirstPass
$ReportOutput += "<p>Completed Pass 1 of 3 in:  $($TotFirstPass.hours) hrs $($TotFirstPass.minutes) min $($TotFirstPass.seconds) sec</p>”

Open in new window

As said, the code could be improved much to be easier to understand and maintain, and to reduce error-prone repetitions.
1
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 41735747
Qlemo

Thanks  I will make the changes and will post results.

The next schedule run is Monday.
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 41738284
Guys

It failed

PS C:\util> .\WSUS-ServerCleanup.ps1
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 C:\util\WSUS-ServerCleanup.ps1:101 char:1
+ $ReportOutput += "<p>" + $CleanupManager.PerformCleanup($CleanupPrimary1) | Conv ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : SqlException

ConvertTo-Html : A positional parameter cannot be found that accepts argument '</p>'.
At C:\util\WSUS-ServerCleanup.ps1:113 char:77
+ $ReportOutput += "<p>" + $CleanupManager.PerformCleanup($CleanupPrimary2) | Conv ...
+                                                                             ~~~~
    + CategoryInfo          : InvalidArgument: (:) [ConvertTo-Html], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.ConvertToHtmlCommand

ConvertTo-Html : A positional parameter cannot be found that accepts argument '</p>'.
At C:\util\WSUS-ServerCleanup.ps1:125 char:77
+ $ReportOutput += "<p>" + $CleanupManager.PerformCleanup($CleanupPrimary3) | Conv ...
+                                                                             ~~~~
    + CategoryInfo          : InvalidArgument: (:) [ConvertTo-Html], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.ConvertToHtmlCommand
wsus-servercleanup.txt
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 41738991
There is a syntax error in the code snippet I showed, it should be:
# Perform First Pass on "Unused Updates and Update Revisions Only"
$StartFirstPass = Get-Date
$ReportOutput += "<p>Beginning WSUS Cleanup on $PrimaryWSUSServer at $StartFirstPass</p>"
$ReportOutput += "<p>Starting Pass 1 of 3: Unused Updates and Update Revisions Only</p>"

$CleanupPrimary1 = New-Object Microsoft.UpdateServices.Administration.CleanupScope($FirstPassSupersededUpdates,$FirstPassExpiredUpdates,$FirstPassObsoleteUpdates,$FirstPassCompressUpdates,$FirstPassObsoleteComputers,$FirstPassUnneededContentFiles)
$ReportOutput += "<p>" + $($CleanupManager.PerformCleanup($CleanupPrimary1) | ConvertTo-HTML -Fragment) + "</p>"
 $EndFirstPass = Get-Date
 $TotFirstPass = $EndFirstPass-$StartFirstPass
$ReportOutput += "<p>Completed Pass 1 of 3 in:  $($TotFirstPass.hours) hrs $($TotFirstPass.minutes) min $($TotFirstPass.seconds) sec</p>"

Open in new window

That explains the second and following errors. The first one is a timeout - the cleanup lasts too long, and the library has given up holding the connection for that reason. I don't know whether you can do something about it, but it is the same as with the GUI Cleanup task, which tells you it has lost the connection. You need to reconnect then.

Edit: Fixed smart quotes - thanks to footech
0
 
LVL 41

Expert Comment

by:footech
ID: 41739258
There's some "pretty" quotes in there on lines 1, 3, 4, and 10.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 41739323
Thanks footech, no clue where those came from, but I've fixed the code snippet.
0
 
LVL 23

Author Comment

by:Thomas Grassi
ID: 41740025
Thanks Qlemo

Made the changes will test again this coming Monday

Will post results
0
 
LVL 23

Author Closing Comment

by:Thomas Grassi
ID: 41748164
Thanks it is now working as  I need

Thanks for everyone who chimed in too
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Loops Section Overview

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question