Solved

Powershell Script for WSUS Cleanup Help needed

Posted on 2016-07-29
12
100 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 39

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 39

Expert Comment

by:footech
ID: 41735715
Can you provide a screenshot of what the cleanup results look like?
0
 
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 68

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 68

Accepted Solution

by:
Qlemo earned 500 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 39

Expert Comment

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Why would I want to create a function for tracking messages? I am glad you asked. As with most monotonous/routine tasks, human error tends to creep in after doing the same task over and over again. By creating a function, you load the function once…
Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now