Windows Updateを実行するVBスクリプト「WUA_SearchDownloadInstall.vbs」

スクリプトからWindows Update

Windows10のアップデートに時間がかかりすぎるので、スクリプトでサクッとアップデートできないかな?と探してみたらありました。すべてのWindowsで使えるようです。

Link

https://docs.microsoft.com/en-us/previous-versions/windows/desktop/aa387101%28v%3dvs.85%29

ソースを.vbsで保存

上記のページあるソースをメモ帳などに貼り付け、WUA_SearchDownloadInstall.vbsなど、適当に .vbs を付けて保存します。

そのソース(WUA_SearchDownloadInstall.vbs)

Set updateSession = CreateObject("Microsoft.Update.Session")
updateSession.ClientApplicationID = "MSDN Sample Script"

'Get update title to search for
WScript.Echo "Enter the title of the update: " & _
"(for example, Update for Windows Rights Management client 1.0)"
updateTitle = WScript.StdIn.Readline

WScript.Echo vbCRLF & "Searching for: " & updateTitle & "..."

Set updateSearcher = updateSession.CreateupdateSearcher()

'Search for all software updates, already installed and not installed
Set searchResult = _
updateSearcher.Search("Type='Software'")

Set updateToInstall = CreateObject("Microsoft.Update.UpdateColl")

updateIsApplicable = False

'Cycle through search results to look for the update title
For i = 0 To searchResult.Updates.Count-1
   Set update = searchResult.Updates.Item(i)
   If UCase(update.Title) = UCase(updateTitle) Then
   'Update in list of applicable updates 
   'Determine if it is already installed or not
      If update.IsInstalled = False Then
         WScript.Echo vbCRLF & _
         "Result: Update applicable, not installed."
         updateIsApplicable = True
         updateToInstall.Add(update)
      Else 
         'Update is installed so notify user and quit
         WScript.Echo vbCRLF & _
         "Result: Update applicable, already installed."
         updateIsApplicable = True
         WScript.Quit 
      End If
   End If 
Next

If updateIsApplicable = False Then
   WScript.Echo "Result: Update is not applicable to this machine."
   WScript.Quit
End If

WScript.Echo vbCRLF & "Would you like to install now? (Y/N)"
stdInput = WScript.StdIn.Readline
 
If (strInput = "N" or strInput = "n") Then 
   WScript.Quit
ElseIf  (stdInput = "Y" OR stdInput = "y") Then
   'Download update
   Set downloader = updateSession.CreateUpdateDownloader() 
   downloader.Updates = updateToInstall
   WScript.Echo vbCRLF & "Downloading..."
   Set downloadResult = downloader.Download()
   WScript.Echo "Download Result: " & downloadResult.ResultCode
  
   'Install Update
   Set installer = updateSession.CreateUpdateInstaller()
   WScript.Echo vbCRLF & "Installing..."
   installer.Updates = updateToInstall
   Set installationResult = installer.Install()
  
   'Output the result of the installation
   WScript.Echo "Installation Result: " & _
   installationResult.ResultCode
   WScript.Echo "Reboot Required: " & _
   installationResult.RebootRequired 
End If

コマンドプロンプトからcscriptで呼び出す

上記のWUA_SearchDownloadInstall.vbsを管理者として起動したコマンドプロンプトからcscriptで呼び出して実行します。

WUA_SearchDownloadInstall.vbsをCドライブ直下に保存していた場合

cscript "c:\WUA_SearchDownloadInstall.vbs"

これだけで実行できます。

重要な更新プログラムのみインストールしたいとき

オプションの更新プログラムなどは除き、重要な更新プログラムのみをインストールした場合は。検索対象を指定するupdateSearcher.Search("Type='Software'")の部分を以下に変更します。

updateSearcher.Search("IsInstalled=0 and Type='Software' and AutoSelectOnWebSites=1")

パラメーターの意味

パラメーター 意味
IsInstalled=0 インストールされていない更新プログラムを対象にする
Type='Software' 更新プログラムが対象
AutoSelectOnWebSites=1 重要な更新プログラムのみを適用
パラメーターの参考サイト

Link

https://docs.microsoft.com/en-us/windows/win32/api/wuapi/nf-wuapi-iupdatesearcher-search

その他類似のスクリプトの参考サイト

Link

WpsでWindowsupdateの再起動までを自動化。 | Shinke1987.net

http://shinke1987.net/?p=276

Link

Windows Updateを行うためのPowershellスクリプト – Osakana Taroのメモ帳

https://blog.osakana.net/archives/8270

Link

301 Moved Permanently

https://www.atmarkit.co.jp/ait/articles/1808/06/news012.html

Link

301 Moved Permanently

http://qiita.com/Marukaziler/items/54bfbf054a372328be3f

PAGETOP