Scripts > WSUS Serverbereinigung mit Powershell #2

# WSUS Connection Parameters:
[String]$WSUSServer = "FQDN vom WSUS Server angeben"
[Boolean]$useSecureConnection = $False
[Int32]$portNumber = 80
[String]$LogFile = "C:\WSUS\MeinLogfile.txt" #Beispiel!
[String]$SMTPServer = "IP-Adresse vom SMTP Server"

# Windows PowerShell example to check 'If File Exists'
$FileExists = Test-Path $LogFile
If ($FileExists -eq $True) {
#Alte Logdatei vorsichtshalber löschen.
Remove-Item $LogFile

# Cleanup Parameters:
# Decline updates that have not been approved for 30 days or more, are not currently needed by any clients, and are superseded by an aproved 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 = $False
# Delete update files that aren't needed by updates or downstream servers.
[Boolean]$unneededContentFiles = $True


#Region SCRIPT

# Load .NET assembly

# Connect to WSUS Server
$wsusParent = [Microsoft.UpdateServices.Administration.AdminProxy]::getUpdateServer($WSUSServer,$useSecureConnection,$portNumber);

# Log the date first
date | out-file -filepath $LogFile -append -noClobber;

# Perform Cleanup
$WSUSServer | out-file -filepath $LogFile -append -noClobber;
$CleanupManager = $wsusParent.GetCleanupManager();
$CleanupScope = New-Object Microsoft.UpdateServices.Administration.CleanupScope($supersededUpdates,$expiredUpdates,$obsoleteUpdates,$compressUpdates,$obsoleteComputers,$unneededContentFiles);
$CleanupManager.PerformCleanup($CleanupScope) | out-file -filepath $LogFile -append -noClobber;
#EndRegion SCRIPT

# Der nachfolgende Teil ist von hier kopiert:
# Mail the report...
$message = new-object Net.Mail.MailMessage
$mailer = new-object Net.Mail.SmtpClient($SMTPServer)
$message.From = "Absender <
$message.To.Add("Empfänger <
$MeinText = "WSUS - Server CleanUp Bericht " + $WSUSServer
$message.Subject = $MeinText
$message.Body = [string]::join([environment]::NewLine, (get-content $logfile))

#Logdatei löschen.