在多部電腦上封鎖 Windows XP Service Pack 2

Microsoft Corporation 的 The Scripting Guys

此指令碼可在多部電腦上執行,以遠端封鎖或解除封鎖從 Windows Update 網站或透過「自動更新」傳送 Windows XP Service Pack 2 (SP2) 的動作。程式碼會使用 Windows Management Instrumentation 登錄提供者的 StdRegProv 類別。封鎖 Windows XP SP2 的傳送動作是有時間限制的。如需暫時停用透過 Windows Update 及「自動更新」傳送 Service Pack 2 之過期日及詳細資料的相關資訊,請參閱 https://www.microsoft.com/technet/prodtechnol/winxppro/maintain/sp2aumng.mspx (英文)。

此指令碼的輸入來自一個逗點分隔的文字檔 hosts.csv,它包含將執行指令碼的電腦名稱。

輸入檔的每一行都必須包含電腦名稱,後面跟著逗點及 b (封鎖) 或 u (解除封鎖)。電腦必須可透過網路存取,而指令碼用來執行的認證必須有本機或網域管理權限。例如,若要封鎖 client1 及 server1 並解除封鎖 client2 及 server2:

client1,b client2,u server1,b server2,u

確定最後一行後面沒有分行符號,因為指令碼會將它解譯為空字串。

若要使用指令碼,請複製程式碼,將它貼入 [記事本],然後將指令碼儲存為 .vbs 副檔名 (例如 blockxpsp2-multi.vbs)。

若要執行指令碼,請輸入下列文字:

cscript blockxpsp2-multi.vbs

如果電腦上的預設指令碼主機是 Cscript.exe,就可以省略開頭的 "cscript"。

指令碼

' Copyright (c) Microsoft Corporation 2004
' File:       BlockXPSP2.vbs
' Contents:   Remotely blocks or unblocks the delivery of
' Windows XP SP2 from Windows Update web site or via Automatic
' Updates. 
' History:    8/20/2004   Peter Costantini   Created
' Version:    1.0

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 = "DoNotAllowXPSP2"
dwValue = 1

'If text file exists, read hosts list 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 XP " & _
   "SP2 deployment via Windows Update or Automatic Update."
Else
  WScript.Echo "ERROR: Unable to add registry entry to " & _
   "block Windows XP SP2 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 XP SP2 deployment via Windows " & _
   "Update or Automatic Update."
Else
  WScript.Echo "Unable to delete registry entry " & _
   strEntryName & ". Windows XP SP2 deployment via " & _
   "Windows Update or Automatic Update is not blocked."
End If

End Sub

如需線上對等支援,請加入 msnews.microsoft.com 新聞伺服器上的 microsoft.public.windows.server.scripting (英文) 社群。若您想要對範例指令碼或指令碼指南,提供意見、回報問題,請與 Microsoft TechNet (英文) 連絡。

免責聲明

此範例指令碼不支援任何 Microsoft 標準技術支援方案或服務。上述的範例指令碼係依「現況」提供,不附帶任何擔保。Microsoft 公司不提供任何的默示擔保,包括但不限於任何商業適售性及特定用途之適用性的默示擔保。您必須承擔此範例指令碼或文件所造成的一切風險。在任何情況下,無論是使用或無法使用此範例指令碼或文件所造成的損害 (包括但不限於營業之損失、營業之中斷、營業資訊之滅失及其他金錢損失),Microsoft 公司、作者群或此指令碼之創作、製造或散發有關之人員概不負責,即使 Microsoft 已經被告知損害發生之可能性亦同。