Solved

Powershell Script for WSUS Cleanup Help needed

Posted on 2016-07-29
12
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
12 Comments
 
LVL 40

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 40

Expert Comment

by:footech
ID: 41735715
Can you provide a screenshot of what the cleanup results look like?
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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 69

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 69

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 40

Expert Comment

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

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

756 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