• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 625
  • Last Modified:

Powershell Script for WSUS Cleanup Help needed

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
Thomas Grassi
Asked:
Thomas Grassi
  • 6
  • 3
  • 3
1 Solution
 
footechCommented:
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
 
Thomas GrassiSystems AdministratorAuthor Commented:
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
 
footechCommented:
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.

 
Thomas GrassiSystems AdministratorAuthor Commented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
Thomas GrassiSystems AdministratorAuthor Commented:
Qlemo

Thanks  I will make the changes and will post results.

The next schedule run is Monday.
0
 
Thomas GrassiSystems AdministratorAuthor Commented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
footechCommented:
There's some "pretty" quotes in there on lines 1, 3, 4, and 10.
0
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
Thanks footech, no clue where those came from, but I've fixed the code snippet.
0
 
Thomas GrassiSystems AdministratorAuthor Commented:
Thanks Qlemo

Made the changes will test again this coming Monday

Will post results
0
 
Thomas GrassiSystems AdministratorAuthor Commented:
Thanks it is now working as  I need

Thanks for everyone who chimed in too
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now