WSUSサーバのクリーンアップ

自宅で、WSUSサーバを利用しているがいつのまにかディスク容量が枯渇していた。
当然といえば、当然。ひたすらパッチをダウンロードしているのだから。
※自宅用だからいいけど、本番でこんな運用を間違ってもしないように。

WSUSの不要なパッチを削除するには、
「管理コンソール」>「オプション」>「サーバクリーンアップウィザード」
で手動実行ができるわけだが、定期的に実施するのは面倒くさいので、バッチ化する。
(余談だが、自宅はHinemosを利用している)

すでに情報もあり、MicrosoftのScriptCenterで公開されていた。
今回は、このスクリプトをそのまま採用している。

gallery.technet.microsoft.com

[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null

$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer(); 
$cleanupScope = new-object Microsoft.UpdateServices.Administration.CleanupScope;

# whether or not superseded updates should be declined.
$cleanupScope.DeclineSupersededUpdates = $true
# whether or not expired updates should be declined. 
$cleanupScope.DeclineExpiredUpdates = $true 
# whether or not obsolete updates should be deleted from the database.
$cleanupScope.CleanupObsoleteUpdates = $true 
# whether or not obsolete revisions to updates should be deleted from the database.
$cleanupScope.CompressUpdates = $true
# whether or not obsolete computers should be deleted from the database. 
# $cleanupScope.CleanupObsoleteComputers = $true 
# whether or not unneeded update files should be deleted.
$cleanupScope.CleanupUnneededContentFiles = $true

$cleanupManager = $wsus.GetCleanupManager(); 
$cleanupManager.PerformCleanup($cleanupScope); 

各プロパティの内容が何を示しているかは、下記ページを参照した。
おおむね、GUIで実施する際の設定画面の項目に準拠していた。

CleanupScope Properties (Microsoft.UpdateServices.Administration)

で、ここまで実施した後に気づいたのだが、2012R2だとWsus関連のPowerShellコマンドレットが充実していた。
reflection.assemblyで.Netを叩かなくても、PowerShellの世界で完結できていたかもしれない。

Windows Server Update Services (WSUS) Cmdlets
Invoke-WsusServerCleanup

2015/11/23 etsubo