WSUS CLEANUP SCRIPT

Script voor het automatisch opschonen van je WSUS server. Sla het script op in een powershell format en draai het maandelijks/wekelijks via een task op je WSUS Server!

SCRIPT:

<# .SYNOPSIS This script will ... .DESCRIPTION This script will ... .PARAMETER None .INPUTS None .OUTPUTS Log file stored in C:\Windows\Temp\ .NOTES Version: 0.1 Author: HKLM_ & Tweakers.net Creation Date: 02/07/2017 Purpose/Change: v0.1 - Initial script development .EXAMPLE .\WSUS Script v0.1.ps1 #>

#——————————————[Initialisations]—————————————

# Set Error Action to Silently Continue
# $ErrorActionPreference = “SilentlyContinue”

#——————————————-[Declarations]—————————————–

# Script Name and Version
$scriptName = “WSUS Server CleanUp Script”
$scriptVersion = “0.1”

# Log File Info
$date = Get-Date -format MM-dd-yyyy.HH_mm_ss
$logPath = “C:\Windows\Temp”
$logName = “$scriptName.$date.log”
$logFile = Join-Path -Path $logPath -ChildPath $logName

# Variables that can be set if needed
$to = “Vul hier je mail adres in”
$from = “Vul hier een mail adres in>”
$smtpServer = “Vul hier je mailserver in”
$subject = “WSUS $env:COMPUTERNAME Monthly CleanUp is complete “
$subjectIncorrect = “Wsus $env:COMPUTERNAME monthly cleanup has an error”
$body = “Het opschonen van de WSUS database is uitgevoerd”
$bodyIncorrect = “Er is een fout opgetreden bij het uitvoeren van het cleanup script check de logfiles in C:\Windows\TEMP voor meer informatie.”

# Contstants that can be set if needed

#——————————————–[Functions]——————————————-

Function DNL-loadAssemblies {
Param()

Begin {
Add-Content $logFile “Loading assemblies…”
}

Process {
Try {
[reflection.assembly]::LoadWithPartialName(“Microsoft.UpdateServices.Administration”) | out-null
Write-Host “Pass – Loading assemblies finished” -ForegroundColor Green
}

Catch {
Write-Host “Failed – Loading assemblies” -ForegroundColor Red
Add-Content $logFile $_.Exception
}
}

End {
If($?) {
Add-Content $logFile “Loading assemblies completed successfully…”
Add-Content $logFile ” “
}
}
}

Function DNL-setWsus {
Param()

Begin {
Add-Content $logFile “Setting WSUS…”
}

Process {
Try {
$wsus = [microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer()
$cleanupScope = new-object Microsoft.UpdateServices.Administration.CleanupScope
$cleanupScope.DeclineSupersededUpdates = $true
$cleanupScope.DeclineExpiredUpdates = $true
$cleanupScope.CleanupObsoleteUpdates = $true
$cleanupScope.CompressUpdates = $true
$cleanupScope.CleanupUnneededContentFiles = $true
$cleanupManager = $wsus.GetCleanupManager()
$cleanupManager.PerformCleanup($cleanupScope)
Write-Host “Pass – Setting WSUS finished” -ForegroundColor Green
}

Catch {
Write-Host “Failed – Setting WSUS” -ForegroundColor Red
Send-MailMessage -To $to -From $from -SMTPServer $smtpServer -Subject $subjectIncorrect -Body $bodyIncorrect
Add-Content $logFile $_.Exception
Exit
}
}

End {
If($?) {
Add-Content $logFile “Setting WSUS completed successfully…”
Add-Content $logFile ” “
}
}
}

Function DNL-sendEmail {
Param()

Begin {
Add-Content $logFile “Sending email…”
}

Process {
Try {
Send-MailMessage -To $to -From $from -SMTPServer $smtpServer -Subject $subject -Body $body
Write-Host “Pass – Sending email finished” -ForegroundColor Green
}

Catch {
Write-Host “Failed – Sending email” -ForegroundColor Red
Add-Content $logFile $_.Exception
Exit
}
}

End {
If($?) {
Add-Content $logFile “Sending email completed successfully…”
Add-Content $logFile ” “
}
}
}

#——————————————–[Execution]——————————————-

Add-Content $logFile “$scriptName version $scriptVersion started at $(Get-Date)”
Add-Content $logFile ” “

DNL-loadAssemblies
DNL-setWsus
DNL-sendEmail

Add-Content $logFile “$scriptName version $scriptVersion ended at $(Get-Date)”