Skip to content

Instantly share code, notes, and snippets.

@tckz
Created July 1, 2009 07:12
Show Gist options
  • Save tckz/138645 to your computer and use it in GitHub Desktop.
Save tckz/138645 to your computer and use it in GitHub Desktop.
条件に合致したIUpdateをインストール承認する
param([switch]$nomail)
trap [Exception] {
$t = $error[0].ToString().Trim() + "`n" + $error[0].InvocationInfo.PositionMessage.Trim()
[Diagnostics.EventLog]::WriteEntry("approve_update", $t, "Error", 1)
}
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null
$update_server = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer()
$update_server.PreferredCulture ="ja"
$all_computers_group = $update_server.GetComputerTargetGroup([Microsoft.UpdateServices.Administration.ComputerTargetGroupId]::AllComputers)
$unassigned_computers_group = $update_server.GetComputerTargetGroup([Microsoft.UpdateServices.Administration.ComputerTargetGroupId]::UnassignedComputers)
$target_computer_group = new-object Microsoft.UpdateServices.Administration.ComputerTargetGroupCollection
$target_computer_group.Add($all_computers_group) | out-null
function approveUpdate($update_scope) {
foreach($update in $update_server.GetUpdates($update_scope) | sort ArrivalDate)
{
if($update.IsDeclined -or -not $update.IsLatestRevision -or $update.IsSuperseded)
{
# 既に拒否済みのもの
# 最新リビジョンでないもの
# 優先された更新があるもの
# は相手にしない
continue
}
# 更新のサマリ
$rev_id = $update.Id
write-host ($update.ArrivalDate.ToLocalTime(), $rev_id.UpdateId, $rev_id.RevisionNumber, $update.Title)
# 対象グループに対してインストール承認
foreach($target_group in $target_computer_group)
{
$update.Approve(
[Microsoft.UpdateServices.Administration.UpdateApprovalAction]::Install,
$target_group) | out-null
}
# 現在の承認状況を表示
foreach($app in $update.GetUpdateApprovals() | sort CreationDate)
{
$target_group = $app.GetComputerTargetGroup()
write-host ("`t", $app.CreationDate.ToLocalTime(), $app.Action.ToString(), $target_group.Name)
}
}
}
foreach($text in @(
"ルート証明書の更新プログラム",
"辞書のアップデート"
)) {
$update_scope = new-object Microsoft.UpdateServices.Administration.UpdateScope
$update_scope.TextIncludes = $text
approveUpdate($update_scope)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment