Skrypt do odrzucenia starych paczek i czyszczenia bazy WSUS
Skrypt służy do automatycznego odrzucania przestarzałych aktualizacji w WSUS oraz uruchamiania procesu czyszczenia WSUS, jeśli dokonano jakichkolwiek zmian.
Wymagania:
- Uprawnienia administracyjne do serwera WSUS.
- Zainstalowany moduł PowerShell dla WSUS.
- Połączenie z serwerem WSUS.
Zakres działania:
Skrypt odrzuca aktualizacje, jeśli spełniają one którykolwiek z poniższych warunków:
- Są zastąpione przez nowsze aktualizacje (superseded).
- Są oznaczone jako wygasłe (expired) według definicji Microsoft.
- Dotyczą systemów operacyjnych x86 lub Itanium.
- Są przeznaczone dla systemu Windows XP.
- Są pakietami językowymi.
- Dotyczą starszych wersji przeglądarki Internet Explorer (wersje 7, 8, 9).
- Są aktualizacjami specyficznymi dla regionów (zawierają nazwy krajów).
- Są aktualizacjami beta.
- Są przeznaczone dla systemów wbudowanych (embedded).
Jeśli aktualizacja obejmuje wiele systemów operacyjnych i spełnia jeden lub więcej powyższych warunków, wersje aktualizacji, które nie spełniają tych kryteriów, nie zostaną odrzucone przez ten skrypt.
Przykład użycia:
Aby uruchomić skrypt, użyj poniższej komendy w PowerShell:
.\Decline-Updates -WSUSServer WSUSServer.Company.com -WSUSPort 8530
Gdzie:
- WSUSServer.Company.com - to adres Twojego serwera WSUS
- 8530 - to numer portu WSUS (domyślnie 8530 dla HTTP, 8531 dla HTTPS).
Efekty działania:
- Skrypt analizuje listę dostępnych aktualizacji.
- Odrzuca aktualizacje zgodnie z podanymi kryteriami.
- Jeśli odrzucono jakiekolwiek aktualizacje, skrypt uruchamia czyszczenie WSUS w celu zwolnienia miejsca na serwerze.
Treść skryptu
Param(
[Parameter(Mandatory=$false,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true,
ValueFromRemainingArguments=$false,
Position=0)]
[string]$WSUSServer = "Localhost", #default to localhost
[int]$WSUSPort=8530,
[switch]$reportonly
)
Function Decline-Updates{
Param(
[string]$WsusServer,
[int]$WSUSPort,
[switch]$ReportOnly
)
write-host "Connecting to WSUS Server $WSUSServer and getting list of updates"
$Wsus = Get-WSUSserver -Name $WSUSServer -PortNumber $WSUSPort
if($WSUS -eq $Null){
write-error "unable to contact WSUSServer $WSUSServer"
}else{
$Updates = $wsus.GetUpdates()
write-host "$(($Updates | where {$_.IsDeclined -eq $false} | measure).Count) Updates before cleanup"
$updatesToDecline = $updates | where {$_.IsDeclined -eq $false -and (
$_.IsSuperseded -eq $true -or #remove superseded updates
$_.PublicationState -eq "Expired" -or #remove updates that have been pulled by Microsoft
$_.LegacyName -imatch "ia64" -or #remove updates for itanium computers (1/2)
$_.LegacyName -imatch "x86" -or #remove updates for 32-bit computers
$_.LegacyName -match "XP" -or #remove Windows XP updates (1/2)
$_.producttitles -match "XP" -or #remove Windows XP updates (1/2)
$_.Title -match "Itanium" -or #remove updates for itanium computers (2/2)
$_.Title -match "ARM64" -or #remove updates for itanium computers (2/2)
($_.Title -imatch "language" -and $_.Title -notmatch "(PL|ENG)") -or #remove langauge packs
$_.Title -match "Sprachpaket" -or
$_.title -match "Internet Explorer 7" -or #remove updates for old versions of IE
$_.title -match "Internet Explorer 8" -or
$_.title -match "Internet Explorer 9" -or
$_.title -match "Japanese" -or #some non-english updates are not filtered by WSUS language filtering
$_.title -match "Korean" -or
$_.title -match "Taiwan" -or
$_.Title -match "Beta" -or #Beta products and beta updates
$_.title -match "Embedded" #Embedded version of Windows
)}
write-host "$(($updatesToDecline | measure).Count) Updates to decline"
$changemade = $false
if($reportonly){
write-host "ReportOnly was set to true, so not making any changes"
}else{
$changemade = $true
$updatesToDecline | %{$_.Decline()}
}
#Decline updates released more then 3 months prior to the release of an included service pack
# - service packs updates don't appear to contain the supersedance information.
Foreach($SP in $($updates | where title -match "^Windows Server \d{4} .* Service Pack \d")){
if(($SP.ProductTitles |measure ).count -eq 1){
$updatesToDecline = $updates | where {$_.IsDeclined -eq $false -and $_.ProductTitles -contains $SP.ProductTitles -and $_.CreationDate -lt $SP.CreationDate.Addmonths(-3)}
if($updatesToDecline -ne $null){
write-host "$(($updatesToDecline | measure).Count) Updates to decline (superseded by $($SP.Title))"
if(-not $reportonly){
$changemade = $true
$updatesToDecline | %{$_.Decline()}
}
}
}
}
#if changes were made, run a WSUS cleanup to recover disk space
if($changemade -eq $true -and $reportonly -eq $false){
$Updates = $wsus.GetUpdates()
write-host "$(($Updates | where {$_.IsDeclined -eq $false} | measure).Count) Updates remaining, running WSUS cleanup"
Invoke-WsusServerCleanup -updateServer $WSUS -CleanupObsoleteComputers -CleanupUnneededContentFiles -CleanupObsoleteUpdates -CompressUpdates -DeclineExpiredUpdates -DeclineSupersededUpdates
}
}
}
Decline-Updates -WSUSServer $WSUSServer -WSUSPort $WSUSPort -reportonly:$reportonly