windows Update自動化

1. Windows Update を確認するセッションを開始

$updateSession = New-Object -com Microsoft.Update.Session

2. Windows Update の検索

$searcher = $updateSession.CreateUpdateSearcher()
$searchResult = $searcher.search(“IsInstalled=0 and Type=’software'”)

3. Windows Update の結果確認

1か月以内の更新プログラムを絞って表示する。
$searchResult.Updates | select Title, LastDeploymentChangeTime, MaxDownloadSize | Where-Object {$_.LastDeploymentChangeTime -gt (Get-Date).AddDays(-30)}

4. ダウンロードする(すべて)

$downloader = $updateSession.CreateUpdateDownloader()
$downloader.Download()

5. インストールする

$installer = $updateSession.CreateUpdateInstaller()
$installationResult = $installer.Install()
if ($installationResult . ResultCode -eq 2) {
# OK
}
else {
# error
}

6. 再起動する

date
restart-computer

7. Windows Update を確認するセッションを開始

$updateSession = New-Object -com Microsoft.Update.Session

8. Windows Update の検索

$searcher = $updateSession.CreateUpdateSearcher()
$searchResult = $searcher.search(“IsInstalled=0 and Type=’software'”)

9. 更新・インストール後結果の確認

1か月以内の更新プログラムを絞って表示する。
$searchResult.Updates | select Title, LastDeploymentChangeTime, MaxDownloadSize | Where-Object {$_.LastDeploymentChangeTime -gt (Get-Date).AddDays(-30)}

【参考記事】
https://qiita.com/asterisk9101/items/8a52562ade6d2a47a467