Windows

如何從命令行安裝 Windows 更新

  • February 16, 2017

是否可以從命令行安裝 Windows 更新?圖形更新工具在 Windows 7 中似乎不太好用。有時可以,有時不能……我的問題是,當我點擊查看詳細資訊列表時,我看不到任何更新。因此,當有預設情況下未選中的更新時,我無法安裝它們…

那麼,還有其他方法可以安裝這些嗎?

不確定 Windows 7,但使用 XP/Vista,您可以執行以下命令來檢測和下載更新:

wuauclt /detectnow /reportnow

如果您在使用它時遇到問題,請查看WSUS Wiki

否則,如果您有想要的特定更新,只需從 Microsoft 網站下載。如果有一些問題阻止您安裝,這是最好的找出方法,因為 Windows 更新日誌很難處理。

您可以使用腳本同步檢查、下載和安裝更新。我經常使用這個 vbscript的修改版本來手動修補 Windows Core 伺服器。

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

Set updateSearcher = updateSession.CreateUpdateSearcher()

WScript.Echo "Searching for updates..." & vbCRLF

Set searchResult = _
updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")

WScript.Echo "List of applicable items on the machine:"

For I = 0 To searchResult.Updates.Count-1
   Set update = searchResult.Updates.Item(I)
   WScript.Echo I + 1 & "> " & update.Title
Next

If searchResult.Updates.Count = 0 Then
   WScript.Echo "There are no applicable updates."
   WScript.Quit
End If

WScript.Echo vbCRLF & "Creating collection of updates to download:"

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

For I = 0 to searchResult.Updates.Count-1
   Set update = searchResult.Updates.Item(I)
   addThisUpdate = false
   If update.InstallationBehavior.CanRequestUserInput = true Then
       WScript.Echo I + 1 & "> skipping: " & update.Title & _
       " because it requires user input"
   Else
       If update.EulaAccepted = false Then
           WScript.Echo I + 1 & "> note: " & update.Title & _
           " has a license agreement that must be accepted:"
           WScript.Echo update.EulaText
           WScript.Echo "Do you accept this license agreement? (Y/N)"
           strInput = WScript.StdIn.Readline
           WScript.Echo 
           If (strInput = "Y" or strInput = "y") Then
               update.AcceptEula()
               addThisUpdate = true
           Else
               WScript.Echo I + 1 & "> skipping: " & update.Title & _
               " because the license agreement was declined"
           End If
       Else
           addThisUpdate = true
       End If
   End If
   If addThisUpdate = true Then
       WScript.Echo I + 1 & "> adding: " & update.Title 
       updatesToDownload.Add(update)
   End If
Next

If updatesToDownload.Count = 0 Then
   WScript.Echo "All applicable updates were skipped."
   WScript.Quit
End If

WScript.Echo vbCRLF & "Downloading updates..."

Set downloader = updateSession.CreateUpdateDownloader() 
downloader.Updates = updatesToDownload
downloader.Download()

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

rebootMayBeRequired = false

WScript.Echo vbCRLF & "Successfully downloaded updates:"

For I = 0 To searchResult.Updates.Count-1
   set update = searchResult.Updates.Item(I)
   If update.IsDownloaded = true Then
       WScript.Echo I + 1 & "> " & update.Title 
       updatesToInstall.Add(update) 
       If update.InstallationBehavior.RebootBehavior > 0 Then
           rebootMayBeRequired = true
       End If
   End If
Next

If updatesToInstall.Count = 0 Then
   WScript.Echo "No updates were successfully downloaded."
   WScript.Quit
End If

If rebootMayBeRequired = true Then
   WScript.Echo vbCRLF & "These updates may require a reboot."
End If

WScript.Echo  vbCRLF & "Would you like to install updates now? (Y/N)"
strInput = WScript.StdIn.Readline
WScript.Echo 

If (strInput = "Y" or strInput = "y") Then
   WScript.Echo "Installing updates..."
   Set installer = updateSession.CreateUpdateInstaller()
   installer.Updates = updatesToInstall
   Set installationResult = installer.Install()

   'Output results of install
   WScript.Echo "Installation Result: " & _
   installationResult.ResultCode 
   WScript.Echo "Reboot Required: " & _ 
   installationResult.RebootRequired & vbCRLF 
   WScript.Echo "Listing of updates installed " & _
   "and individual installation results:" 

   For I = 0 to updatesToInstall.Count - 1
       WScript.Echo I + 1 & "> " & _
       updatesToInstall.Item(i).Title & _
       ": " & installationResult.GetUpdateResult(i).ResultCode   
   Next
End If

它似乎對此很有吸引力,但我當然沒有在 Windows 7 下測試過它。如果需要,還有指向另一篇文章的連結,用於針對特定更新。

還有一個 Powershell 模組可以提供類似的體驗。

快速瀏覽後,我還發現這個第三方應用程序也使用更新 API,但有更多選項(儘管需要您信任第三方程式碼)。

引用自:https://serverfault.com/questions/10122