вторник, 13 мая 2014 г.

PowerShell: скрипт для автоматизации отклонения обновлений определённого типа на сервере WSUS

Данный скрипт поможет автоматически отклонять приходящие обновления на сервер WSUS, содержащие определённые словосочетания в названии. Так в нижеприведённом примере будут отклоняться обновления, содержащие слова: "itanium", "ia64", "lync", "publisher".

$WsusServer = "wsus.mycompany.corp"
$UseSSL = $false
$PortNumber = 80
$TrialRun = $false

#E-mail Configuration
$SMTPServer = "smtp.mycompany.corp"
$FromAddress = "WSUS@mycompany.corp"
[string[]] $Recipients = "admin1@mycompany.corp", "admin2@mycompany.corp", "admin3@mycompany.corp"
$MessageSubject = "WSUS :: Declining Updates"

Function SendEmailStatus($MessageSubject, $MessageBody)
{
$SMTPMessage = New-Object System.Net.Mail.MailMessage
$SMTPMessage.from = $FromAddress
foreach($recipient in $Recipients)
{
$SMTPMessage.to.Add($recipient)
}
$SMTPMessage.Subject = $MessageSubject
$SMTPMessage.Body = $MessageBody
$SMTPMessage.IsBodyHTML = $true
#Send the message via the local SMTP Server
$SMTPClient = New-Object System.Net.Mail.SMTPClient $SMTPServer
$SMTPClient.Send($SMTPMessage)
$SMTPMessage.Dispose()
rv SMTPClient
rv SMTPMessage
}

#Connect to the WSUS 3.0 interface.
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null
$WsusServerAdminProxy = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer($WsusServer,$UseSSL,$PortNumber);
$itanium = $WsusServerAdminProxy.SearchUpdates('itanium') | ?{-not $_.IsDeclined -and -not $_.IsApproved}
$itanium += $WsusServerAdminProxy.SearchUpdates('ia64') | ?{-not $_.IsDeclined -and -not $_.IsApproved}
$itanium += $WsusServerAdminProxy.SearchUpdates('lync') | ?{-not $_.IsDeclined -and -not $_.IsApproved}
$itanium += $WsusServerAdminProxy.SearchUpdates('publisher') | ?{-not $_.IsDeclined -and -not $_.IsApproved}
If ($TrialRun)
{$MessageSubject += " Trial Run"}
Else
{$itanium | %{$_.Decline()}}
If ($itanium.Count -gt 0)
{
$MessageBody = $itanium | Select `
@{Name="Title";Expression={[string]$_.Title}},`
@{Name="KB Article";Expression={[string]::join(' | ',$_.KnowledgebaseArticles)}},`
@{Name="Classification";Expression={[string]$_.UpdateClassificationTitle}},`
@{Name="Product Title";Expression={[string]::join(' | ',$_.ProductTitles)}},`
@{Name="Product Family";Expression={[string]::join(' | ',$_.ProductFamilyTitles)}},`
@{Name="Uninstallation Supported";Expression={[string]$_.UninstallationBehavior.IsSupported}} | ConvertTo-HTML
SendEmailStatus $MessageSubject $MessageBody
}
Else
{
SendEmailStatus $MessageSubject "No new updates for declining"
}

Перед началом использования необходимо установить константы в соответствии с вашей инфраструктурой. А конкретнее это: имя сервера, порт, флаг использования протокола SSL, флаг использования пробного режима, имя SMTP сервера для отправки отчёта, адреса отправителя и получателей, тема письма.

Комментариев нет:

Отправить комментарий