Monitoring

如何找出有多少更新正在等待安裝在遠端伺服器上

  • September 11, 2020

有沒有辦法查明 Windows Server(2003、2008)是否已經下載了 Microsoft Windows 更新並正在等待使用者確認“安裝更新並重新啟動伺服器”?

我們使用 WSUS 和 SSCM 來收集和發布更新,並且大多數伺服器在周日早上自動安裝,主要是開發和測試伺服器。

我們已將重要的生產伺服器設置為僅手動安裝,但有時有些伺服器有一段時間沒有手動重新啟動(人類忘記了!)

如果有某種方法(powershell 腳本、WMI 查詢、一些魔術命令)可以用來計算或發現是否有更新掛起,那就太好了。

這是我寫的腳本。它會告訴你:

  • 有多少更新檔等待安裝
  • 如果更新檔需要重新啟動
  • 如果伺服器目前正在等待重新啟動以啟用更新檔

範例用法:C:\> cscript ServerPendingUpdates.vbs myserver01 myserver02

Connecting to myserver01 to check software update status...
myserver01 has 2 updates pending installation
myserver01 WILL need to be rebooted to complete the installation of these updates.
myserver01 is waiting for a REBOOT to complete a previous installation.

<snip>

腳本:

'#
'# ServerPendingUpdates.vbs
'#
'# Usage: cscript ServerPendingUpdates.vbs {servername} {servername} {servername} {servername}
'#    If no {servername} specified then 'localhost' assumed
'#
'# To do: Error handling
'#
Option Explicit
Dim strServer        : strServer         =  GetArgValue(0,"localhost")


'#
'# Loop through the input parameters for each server
'#
Dim i
For i = 0 To WScript.Arguments.Count - 1
   CheckServerUpdateStatus GetArgValue(i,"localhost") 'strServer
Next

WScript.Quit(0)

Function CheckServerUpdateStatus( ByVal strServer )

   WScript.Echo vbCRLF & "Connecting to " & strServer & " to check software update status..."

   Dim blnRebootRequired    : blnRebootRequired     = False
   Dim blnRebootPending    : blnRebootPending     = False
   Dim objSession        : Set objSession    = CreateObject("Microsoft.Update.Session", strServer)
   Dim objUpdateSearcher     : Set objUpdateSearcher    = objSession.CreateUpdateSearcher
   Dim objSearchResult    : Set objSearchResult     = objUpdateSearcher.Search(" IsAssigned=1 and IsHidden=0 and Type='Software'")

   '#
   '#
   '#
   Dim i, objUpdate
   Dim intPendingInstalls    : intPendingInstalls     = 0

   For i = 0 To objSearchResult.Updates.Count-1
       Set objUpdate = objSearchResult.Updates.Item(I) 

       If objUpdate.IsInstalled Then
           If objUpdate.RebootRequired Then
               blnRebootPending     = True
           End If
       Else
           intPendingInstalls    = intPendingInstalls + 1
           'If objUpdate.RebootRequired Then    '### This property is FALSE before installation and only set to TRUE after installation to indicate that this patch forced a reboot.
           If objUpdate.InstallationBehavior.RebootBehavior <> 0 Then
               '# http://msdn.microsoft.com/en-us/library/aa386064%28v=VS.85%29.aspx
               '# InstallationBehavior.RebootBehavior = 0    Never reboot
               '# InstallationBehavior.RebootBehavior = 1    Must reboot
               '# InstallationBehavior.RebootBehavior = 2    Can request reboot
               blnRebootRequired     = True
           End If

       End If
   Next

   WScript.Echo strServer & " has " & intPendingInstalls & " updates pending installation"

   If blnRebootRequired Then
       WScript.Echo strServer & " WILL need to be rebooted to complete the installation of these updates."
   Else
       WScript.Echo strServer & " WILL NOT require a reboot to install these updates."
   End If


   '#
   '#
   '#
   If blnRebootPending Then
       WScript.Echo strServer & " is waiting for a REBOOT to complete a previous installation."
   End If 
End Function



'#
'#
'#
Function GetArgValue( intArgItem, strDefault )
   If WScript.Arguments.Count > intArgItem Then
       GetArgValue = WScript.Arguments.Item(intArgItem)
   Else
       GetArgValue = strDefault
   End If
End Function

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