阻止 Windows Server 2003 Service Pack 1

发布者 Microsoft Corporation 脚本专家

这两个脚本可用来远程地阻止或不阻止从 Windows Update Web 站点或通过“自动更新”程序自动发送的 Windows Server 2003 Service Pack 1。第一个脚本针对单台本地或远程计算机。第二个脚本针对多台计算机,计算机列表从文本文件获取。两者的代码均使用 Windows 管理规范 (WMI) 注册表提供程序的 StdRegProv 类。

阻止发送 Windows Server 2003 Service Pack 1 的能力只在有限的时间内有效。有关暂时禁止通过 Windows Update 及“自动更新”程序发送 Service Pack 1 的有效期和详细信息,请参阅 https://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 已被告知这种损害可能性。