Quantcast
Channel: WSUS forum
Viewing all articles
Browse latest Browse all 12874

WSUS Cleanup powershell script not working

$
0
0

So I have this script that I found online. After some tweaking I got it to work for me (once). But then this morning when I tried to run it, it is failing. Here's the script.

#Region VARIABLES
# WSUS Connection Parameters:
 ## Change settings below to your situation. ##
# Enter your FQDN of the WSUS server
 [String]$parentServer = "placeholder.FQDN.com" 

# Use secure connection $True or $False
 [Boolean]$useSecureConnection = $False
 [Int32]$portNumber = 8530
 
# 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 = "myemail@FQDN.com"
# Subject of email notification
 [String]$emailSubject = "WSUS Cleanup Results"
# Enter your FQDM for Exchange server 
[String]$emailMailserver = "smtp.FQDN.com"
# 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
#EndRegion VARIABLES

#Region SCRIPT
 
# Load .NET assembly
 [void][reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration");
# Connect to WSUS Server
 $wsusParent = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($parentServer,$useSecureConnection,$portNumber);
# Log the date first
 $DateNow = Get-Date
# Perform Cleanup
 $Body += "$parentServer ($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 

#Get list of downstream servers
 $wsusDownstreams = [Microsoft.UpdateServices.Administration.AdminProxy]::DownstreamServerCollection;
 $wsusDownstreams = $wsusParent.GetDownstreamServers();
 
#Clean each downstream server
 $wsusDownstreams | ForEach-Object {
 $ping = New-Object System.Net.NetworkInformation.Ping
 $DSServer = $_.FullDomainName
 Try{
 $Reply = $ping.send($DSServer)
 $ReplyStatus = $Reply.Status
 Write-Host $ReplyStatus
 }
 catch{
 $ReplyStatus = "False"
 Write-Host $ReplyStatus
 }
 if ($ReplyStatus -eq "Success")
 {
 # Log the date first
 $DateNow = Get-Date
 $Body += $DSServer + " ($DateNow ) : " | Out-String
 $wsusReplica = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($_.FullDomainName, $useSecureConnection,$portNumber);
 $CleanupManager = $wsusReplica.GetCleanupManager();
 $CleanupScope = New-Object Microsoft.UpdateServices.Administration.CleanupScope($supersededUpdates,$expiredUpdates,$obsoleteUpdates,$compressUpdates,$obsoleteComputers,$unneededContentFiles);
 $Body += $CleanupManager.PerformCleanup($CleanupScope) | Out-String
 }else{
 # Log the date first
 $DateNow = Get-Date
 $Body += $DSServer + " ($DateNow ) : not pingable" | 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(myemail@FQDN.com)
 $message.Subject = "Windows Update - Server Clean-Up Wizard"
 $message.Body = ($Body)
$mailer.Send($message)
#>
#EndRegion SCRIPT
 #You are done!

Like I said, it worked just fine yesterday and sent me the email. Now it will send me the email (I commented that out because it was filling up my inbox), but gives me errors on the part where it connects to the WSUS server. Here are the errors I get.

Exception calling "GetUpdateServer" with "3" argument(s): "The type initializer for 'Microsoft.UpdateServices.Internal.Constants' threw an exception."
At X:\me\WSUSCleaner.ps1:48 char:85+  $wsusParent = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer <<<< ($parentServer,$useSecureConnection,$portNumber);+ CategoryInfo          : NotSpecified: (:) [], MethodInvocationException+ FullyQualifiedErrorId : DotNetMethodException

You cannot call a method on a null-valued expression.
At X:\me\WSUSCleaner.ps1:55 char:48
+ $CleanupManager = $wsusParent.GetCleanupManager <<<< ();+ CategoryInfo          : InvalidOperation: (GetCleanupManager:String) [], RuntimeException+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At X:\me\WSUSCleaner.ps1:57 char:41
+  $Body += $CleanupManager.PerformCleanup <<<< ($CleanupScope) | Out-String+ CategoryInfo          : InvalidOperation: (PerformCleanup:String) [], RuntimeException+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At X:\me\WSUSCleaner.ps1:61 char:53
+  $wsusDownstreams = $wsusParent.GetDownstreamServers <<<< ();+ CategoryInfo          : InvalidOperation: (GetDownstreamServers:String) [], RuntimeException+ FullyQualifiedErrorId : InvokeMethodOnNull
Any help would be awesome.


Viewing all articles
Browse latest Browse all 12874

Latest Images

Trending Articles

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>