封鎖 Windows Server 2003 Service Pack 1

作者: Scripting Guys, Microsoft Corporation

這兩個指令碼可以在遠端封鎖或解除封鎖經由 Windows Update 網站或自動更新而自動傳送 Windows Server 2003 Service Pack 1 的作業。第一個指令碼適用於一台電腦或遠端電腦。第二個指令碼則適用於多部電腦,電腦名單取自文字檔。這兩種指令碼的程式碼,都是採用 Windows Management Instrumentation (WMI) 登錄提供者的 StdRegProv 類別。

封鎖 Windows Server 2003 Service Pack 1 的傳送作業只能在限定的時間內執行。如需有關到期日的資訊,以及有關暫時停用由 Windows Update 和自動更新傳送 Service Pack 1 的詳細資訊,請參閱 http://www.microsoft.com/windowsserver2003/evaluation/news/bulletins/ws03sp1blockertool.mspx

本页内容

指令碼 1:封鎖一部機器的 Service Pack 1
指令碼 2:封鎖多部機器上的 Service Pack 1


指令碼 1:封鎖一部機器的 Service Pack 1

若要使用這個指令碼,請複製程式碼,將它貼到記事本,再以 .vbs 副檔名儲存該指令碼 (例如,blockws03sp1.vbs)。執行指令碼時,請在指令碼名稱後面加上一個參數:/b (代表封鎖) 或 /u (代表解除封鎖)。您也可以加入遠端電腦名稱作為第二個參數。如果沒有指定任何遠端電腦,就表示指令碼是在本機執行。比方說,若要封鎖 server1 的 SP1,請輸入:

cscript blockws03sp1.vbs /b server1

如果該電腦上的預設指令碼是 Cscript.exe,則可省略開頭的「cscript」。

指令碼 1 程式碼


On Error Resume Next

' Define constants and global variables.
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strKeyPath = "Software\Policies\Microsoft\Windows\WindowsUpdate"
strEntryName = "DoNotAllowSP"
dwValue = 1 'block

' Handle command-line arguments.
Set colArgs = WScript.Arguments
If (colArgs.Count = 0) Or (colArgs.Count > 2) Then
  ShowUsage
Else
  If colArgs.Count = 2 Then
    strComputer = colArgs(1)
  End If
' Connect with WMI service and StdRegProv class.
  Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
   strComputer & "\root\default:StdRegProv")
  If Err = 0 Then
    If (LCase(colArgs(0)) = "/b") Or (LCase(colArgs(0)) = "-b" ) Then
      AddBlock
    ElseIf (LCase(colArgs(0)) = "/u") Or (LCase(colArgs(0)) = "-u") Then
      RemoveBlock
    Else
      ShowUsage
    End If
  Else
    WScript.Echo "Unable to connect to WMI service on " & strComputer & "."
  End If
  Err.Clear
End If

'******************************************************************************

Sub AddBlock

'Check whether WindowsUpdate subkey exists.
strParentPath = "SOFTWARE\Policies\Microsoft\Windows"
strTargetSubKey = "WindowsUpdate"
intCount = 0
intReturn1 = objReg.EnumKey(HKEY_LOCAL_MACHINE, strParentPath, arrSubKeys)
If intReturn1 = 0 Then
  For Each strSubKey In arrSubKeys
    If strSubKey = strTargetSubKey Then
      intCount = 1
    End If
  Next
  If intCount = 1 Then
    SetValue
  Else
    WScript.Echo "Unable to find registry subkey " & strTargetSubKey & _
     ". Creating ..."
    intReturn2 = objReg.CreateKey(HKEY_LOCAL_MACHINE, strKeyPath)
    If intReturn2 = 0 Then
      SetValue
    Else
      WScript.Echo "ERROR: Unable to create registry subkey " & _
       strTargetSubKey & "."
    End If
  End If
Else
  WScript.Echo "ERROR: Unable to find registry path " & strParentPath & "."
End If

End Sub

'******************************************************************************

Sub SetValue

intReturn = objReg.SetDWORDValue(HKEY_LOCAL_MACHINE, strKeyPath, _
 strEntryName, dwValue)
If intReturn = 0 Then
  WScript.Echo "Added registry entry to block Windows Server 2003 " & _
   "Service Pack 1 deployment via Windows Update or Automatic Update."
Else
  WScript.Echo "ERROR: Unable to add registry entry to block " & _
   "Windows Server 2003 Service Pack 1 deployment via Windows Update or " & _
   "Automatic Update."
End If

End Sub

'******************************************************************************

Sub RemoveBlock

intReturn = objReg.DeleteValue(HKEY_LOCAL_MACHINE, strKeyPath, strEntryName)
If intReturn = 0 Then
  WScript.Echo "Deleted registry entry " & strEntryName & _
   ". Unblocked Windows Server 2003 Service Pack 1 deployment via " & _
   "Windows Update or Automatic Update."
Else
  WScript.Echo "Unable to delete registry entry " & strEntryName & _
   ". Windows Server 2003 Service Pack 1 deployment via " & _
   "Windows Update or Automatic Update is not blocked."
End If

End Sub

'******************************************************************************

Sub ShowUsage

WScript.Echo VbCrLf & _
"This script allows you to temporarily block or unblock " & VbCrLf & _
"delivery of Windows Server 2003 Service Pack 1 from the " & VbCrLf & _
"Windows Update Web site or via Automatic Updates." & VbCrLf & VbCrLf & _
"The ability to block delivery of this service pack will be " & VbCrLf & _
"available only for a limited time." & VbCrLf & VbCrLf & _
 "Usage:" & VbCrLf & _
 "  BlockWS03SP1.vbs { /b | /u | /? } [hostname]" & VbCrLf & _
 "    /b = Block (deny) Windows Server 2003 SP 1 deployment" & VbCrLf & _
 "    /u = Unblock (allow) Windows Server 2003 SP 1 deployment" & VbCrLf & _
 "    /? = Show usage" & VbCrLf & _
 "    hostname = Name of remote computer. " & _
 "Default is local computer. (Optional)" & VbCrLf & VbCrLf & _
 "Example:" & VbCrLf & _
 "  BlockWS03SP1.vbs /b server1"

End Sub


指令碼 2:封鎖多部機器上的 Service Pack 1

這個指令碼的輸入,是來自以逗號分隔的值文字檔 hosts.csv,其中包含該指令碼所要執行的電腦名稱。如指令碼所示,該文字檔必須與該指令碼位於同一個目錄下;如果不在同一個目錄下,請將文字檔的完整路徑指派給 strFilename。

這個輸入檔的每一行,必須包含電腦名稱、逗號,接著是 b (代表封鎖) 或 u (代表解除封鎖)。這些電腦必須能夠透過網路存取,而且執行指令碼所用的認證,也必須具備每一部電腦的本機或遠端管理權限。比方說,若要封鎖 server1 和 server3,以及解除封鎖 server2 和 server4:


server1,b
server2,u
server3,b
server4,u


若要使用這個指令碼,請複製程式碼,將它貼到記事本,再以 .vbs 副檔名儲存該指令碼 (例如,blockws03sp1-multi.vbs)。

若要執行這個指令碼,請輸入:

cscript blockws03sp1-multi.vbs

指令碼輸出會傳送到顯示畫面。若要將指令碼輸出重新導向文字檔,請輸入:

cscript blockws03sp1-multi.vbs > log.txt

如果電腦上的預設指令碼是 Cscript.exe,則可省略開頭的「cscript」。

指令碼 2 程式碼


On Error Resume Next

'Define constants and global variables.
Const HKEY_LOCAL_MACHINE = &H80000002
Const FOR_READING = 1
strFilename = "hosts.csv"
strKeyPath = "Software\Policies\Microsoft\Windows\WindowsUpdate"
strEntryName = "DoNotAllowSP"
dwValue = 1

'If text file exists, read list of hosts and operation for each.
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strFilename) Then
  Set objFile = objFSO.OpenTextFile(strFilename, FOR_READING)
Else
  WScript.Echo "Input file " & strFilename & " not found."
  WScript.Quit
End If
Do Until objFile.AtEndOfStream
  strHost = objFile.ReadLine
  arrHost = Split(strHost, ",")
  strComputer = arrHost(0)
  strBlock = LCase(arrHost(1))
  Wscript.Echo VbCrLf & strComputer
  Wscript.Echo String(Len(strComputer), "-")
'Connect with WMI service and StdRegProv class.
  Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
   strComputer & "\root\default:StdRegProv")
  If Err = 0 Then
    If strBlock = "b" Then
      AddBlock
    ElseIf strBlock = "u" Then
      RemoveBlock
    Else
      WScript.Echo "Invalid value in input file for this computer."
    End If
  Else
    WScript.Echo "Unable to connect to WMI service on " & strComputer & "."
  End If
  Err.Clear
Loop
objFile.Close

'******************************************************************************

Sub AddBlock

'Check whether WindowsUpdate subkey exists.
strParentPath = "SOFTWARE\Policies\Microsoft\Windows"
strTargetSubKey = "WindowsUpdate"
intCount = 0
intReturn1 = objReg.EnumKey(HKEY_LOCAL_MACHINE, strParentPath, arrSubKeys)
If intReturn1 = 0 Then
  For Each strSubKey In arrSubKeys
    If strSubKey = strTargetSubKey Then
      intCount = 1
    End If
  Next
  If intCount = 1 Then
    SetValue
  Else
    WScript.Echo "Unable to find registry subkey " & strTargetSubKey & _
     ". Creating ..."
    intReturn2 = objReg.CreateKey(HKEY_LOCAL_MACHINE, strKeyPath)
    If intReturn2 = 0 Then
      SetValue
    Else
      WScript.Echo "ERROR: Unable to create registry subkey " & _
       strTargetSubKey & "."
    End If
  End If
Else
  WScript.Echo "ERROR: Unable to find registry path " & strParentPath & "."
End If

End Sub

'******************************************************************************

Sub SetValue

intReturn = objReg.SetDWORDValue(HKEY_LOCAL_MACHINE, strKeyPath, _
 strEntryName, dwValue)
If intReturn = 0 Then
  WScript.Echo "Added registry entry to block Windows Server 2003 " & _
   "Service Pack 1 deployment via Windows Update or Automatic Update."
Else
  WScript.Echo "ERROR: Unable to add registry entry to block " & _
   "Windows Server 2003 Service Pack 1 deployment via Windows Update or " & _
   "Automatic Update."
End If

End Sub

'******************************************************************************

Sub RemoveBlock

intReturn = objReg.DeleteValue(HKEY_LOCAL_MACHINE, strKeyPath, strEntryName)
If intReturn = 0 Then
  WScript.Echo "Deleted registry entry " & strEntryName & _
   ". Unblocked Windows Server 2003 Service Pack 1 deployment via " & _
   "Windows Update or Automatic Update."
Else
  WScript.Echo "Unable to delete registry entry " & strEntryName & _
   ". Windows Server 2003 Service Pack 1 deployment via " & _
   "Windows Update or Automatic Update is not blocked."
End If

End Sub


如需線上同儕支援,請加入 msnews.microsoft.com 新聞伺服器上的 microsoft.public.windows.server.scripting 社群。若要提供意見,或者報告範例指令碼或 Scripting Guide 中的錯誤,請聯絡 Microsoft TechNet

免責聲明

本範例指令碼不受任何 Microsoft 標準支援程式或服務之支援。本範例指令碼僅按現狀提供,不含任何瑕疵責任擔保。Microsoft 再次聲明不提供所有默示瑕疵責任擔保,其中包括但不限於適售性或適合某特定用途之任何默示擔保。所有因使用本範例指令碼及文件所導致的風險及其效能,一概由使用者承擔。對於任何因使用或無法使用本範例指令碼或文件所導致之任何損失 (其中包括但不限於商業利潤之損失、營業中斷、商業資訊之損失或其他金錢損失),即使 Microsoft 經事先告知該損害發生之可能性,Microsoft、其作者或任何建立、生產或傳送指令碼者概不負責。

顯示: