[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[原创] VBS版Windows更新补丁管理工具

本人一直以来Windows 更新补丁管理而烦恼,花了大量的时间完成这段代码

2009年因为无法在线获取Windows 更新补丁补丁,所以代码还不够完美,困惑了2年多终于有办法了
  1. 'Windows 更新补丁管理工具 v1.01 UpdateMgr.vbs BY:FastSLZ  
  2. '转载请注明出处http://www.bathome.net/thread-3722-1-1.html
  3. '1.本脚本能在线获取多种方式的最新更新、采集当前系统全部微软推送更新
  4. '2.自动生成批量下载及批量安装批处理文件,方便联网状态下载,脱机状态安装
  5. '3.支持命令提示符下带参数运行
  6. '4.枚举重要更新命令及参数   Cscript.exe //NoLogo UpdateMgr.vbs 1
  7. '5.枚举可选更新并过滤隐藏关键字 Cscript.exe //NoLogo UpdateMgr.vbs 2 "KB2483139"
  8. '6.补丁安装顺序自动排列
  9. '7.自动下载安装最新Microsoft Update Agent 3.0
  10. Set WH = GetObject("Winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery _
  11.     ("Select * From Win32_DesktopMonitor")
  12. For Each aWH in WH
  13.      TW = Int(aWH.ScreenWidth/8)
  14. Next
  15. Set WH = Nothing
  16. If TW <= 0 Then :TW = 128 :End If:TW = TW - 4
  17. If (Lcase(Right(Wscript.FullName,11)) = "wscript.exe") Then
  18.    CreateObject("WScript.Shell").Run("%Comspec% /C " &Chr(34)&"mode con cols="& TW _
  19.    &"&Title Windows 更新补丁管理工具 UpdateMgr.vbs BY: FastSLZ  www.bathome.net&Cscript.exe //NoLogo " &Chr(34) _
  20.    &Wscript.ScriptFullName&Chr(34)&"&&(Echo 此窗口40秒后自动关闭...&Ping -n 40 127.0.1>nul&Exit)"&Chr(34)),3:Wscript.Quit
  21. End If
  22. Tnum = 0 :Cnum = 0:St = 0:MUAnum = 0
  23. Dim WMI,WS,Fso,objAuto,CPath,MUSU,xOS,OSx,BatXZ,BatAZ,MUAnum,ErrD
  24. Set WS=CreateObject("Wscript.Shell")
  25. Set WMI = GetObject("Winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
  26. Set cOSs = WMI.ExecQuery("Select * from Win32_OperatingSystem")
  27. Set Fso = CreateObject("Scripting.FileSystemObject")
  28. Set oArg = WScript.Arguments
  29. KBHide = "KB2483139"
  30. If oArg.Count = 1 Then St = oArg(0)
  31. If oArg.Count = 2 Then St = oArg(0):KBHide = oArg(1)
  32. If oArg.Count > 2 Then WS.Popup "命令及参数错误!" &vbTab ,4,"错误",4160 :WScript.Quit
  33. Set oArg = Nothing
  34. For Each oOS in cOSs
  35.     OSx = Split(Replace(oOS.Caption,"Microsoft Windows " ,"Win"), " ")(0)
  36.     If InStr(oOS.CSDVersion, "") Then SPx = Replace(oOS.CSDVersion,"Service Pack " ,"_SP")
  37.     xVer=Split(oOS.Version,".")(0)
  38. Next
  39. OSx = Trim(OSx & SPx)
  40. Set UpdateSvr = WMI.ExecQuery("Select * from Win32_Service Where Name = 'wuauserv'")
  41. If UpdateSvr.Count = 0 Then
  42.     WS.Popup "Windows 更新服务不可用!" &vbTab ,4,"检查服务",4160 :WScript.Quit
  43.     Else
  44.     For Each oSvr in UpdateSvr
  45.         sSvr = UCase(oSvr.StartMode) :oSvr.ChangeStartMode("Automatic")
  46.         If oSvr.State = "Stopped" Then oSvr.StartService() Else oSvr.StopService() :WScript.Sleep 500 :oSvr.StartService()
  47.     Next
  48. End If
  49. Set B = WMI.ExecQuery("Select * from Win32_PingStatus Where address = 'www.baidu.com'")
  50. For Each A in B
  51.     If IsNull(A.StatusCode) Or A.StatusCode<>0 Then WS.Popup "网络连接不可用!" &vbTab ,4,"检查网络",4160 :WScript.Quit
  52. Next
  53. Set SysEnv = WS.Environment("SYSTEM")
  54. Select Case UCase(SysEnv("PROCESSOR_ARCHITECTURE"))
  55.     Case "X86"
  56.     xOS = "x86"
  57.     Case "AMD64"
  58.     xOS = "x64"
  59.     Case "IA64"
  60.     xOS = "ia64"
  61. End Select
  62. Set B = Nothing:Set SysEnv = Nothing
  63. On Error Resume Next
  64. CPath = WS.CurrentDirectory & "\"
  65. MUAnum = MUAInfo(MUAnum)
  66. Do Until MUAnum = 2
  67. Set MUSU = Nothing:Set objAuto = Nothing
  68. Set objAuto = CreateObject("Microsoft.Update.AutoUpdate")
  69. objAuto.EnableService
  70. AUO = objAuto.Settings.NotificationLevel
  71. IRU = objAuto.Settings.IncludeRecommendedUpdates
  72. objAuto.Settings.NotificationLevel = 2
  73. If xVer < 5 Then objAuto.Settings.NonAdministratorsElevated = -1
  74. If IRU <> 0 Then objAuto.Settings.IncludeRecommendedUpdates = 0
  75. objAuto.Settings.ScheduledInstallationDay = 5
  76. objAuto.Settings.ScheduledInstallationTime = 12
  77. objAuto.Settings.Refresh
  78. objAuto.Settings.Save
  79. If St < 1 Then
  80. WScript.Echo _
  81.      "仅重要更新" &vbTab&vbTab     &"(1 Enter)" & vbCrLf _
  82.     &"仅可选更新" &vbTab&vbTab     &"(2 Enter)" & vbCrLf _
  83.     &"重要+可选" &vbTab&vbTab     &"(3 Enter)" & vbCrLf _
  84.     &"已安装更新" &vbTab&vbTab     &"(4 Enter)" & vbCrLf _
  85.     &"采集当前系统全部更新" &vbTab& "(5 Enter)" & vbCrL
  86. St = WScript.StdIn.Readline
  87. End If
  88. Waitx = "正在查找适用于您计算机的最新更新程序,持续时间取决与你的网络带宽,请耐心等候..." & vbCrLf
  89. Select Case St
  90.    Case "1"
  91.    Wscript.Echo Waitx:Set MUS = CreateObject("Microsoft.Update.Searcher").Search _
  92.    ("Type='Software' and (IsHidden=0 and IsAssigned=1 and IsInstalled=0)")
  93.    Case "2"
  94.    Wscript.Echo Waitx:Set MUS = CreateObject("Microsoft.Update.Searcher").Search _
  95.    ("Type='Software' and (IsHidden=0 and IsAssigned=0 and IsInstalled=0)")
  96.    Case "3"
  97.    Wscript.Echo Waitx:Set MUS = CreateObject("Microsoft.Update.Searcher").Search _
  98.    ("Type='Software' and (IsHidden=0 and IsAssigned=0 and IsInstalled=0) or (IsHidden=0 and IsAssigned=1 and IsInstalled=0)")
  99.    Case "4"
  100.    Wscript.Echo Waitx:Set MUS = CreateObject("Microsoft.Update.Searcher").Search _
  101.    ("Type='Software' and IsHidden=0 and IsInstalled=1")
  102.    Case "5"
  103.    Wscript.Echo Waitx:Set MUS = CreateObject("Microsoft.Update.Searcher").Search _
  104.    ("Type='Software'")
  105.    Case Else
  106.    WS.Popup "请按1~5然后回车!" &vbTab ,4,"请选择",4160 :WScript.Quit
  107. End Select
  108. Set MUSU = MUS.Updates
  109. ErrN = Err.Number :ErrD = Err.Description
  110. If MUSU.Count = 0 Then WS.Popup "您的系统是最新的!" & vbTab ,4,"恭喜",4160 :WScript.Quit
  111. If CStr(ErrN) = 0 Then Exit Do
  112. If CStr(ErrN) = "424" Then :MUAnum = MUAInfo(MUAnum)
  113. If CStr(ErrN) <> 0 Then Wscript.Echo "Error # " & ErrN & vbTab & ErrD :WScript.Quit
  114. Loop
  115. BatXZ = CPath & OSx & "_" & xOS & "_批量下载" & ".cmd"
  116. BatAZ = CPath & OSx & "_" & xOS & "_批量安装" & ".cmd"
  117. If TW > 152 Then TW =152
  118. Set XZ = Fso.CreateTextFile(BatXZ , True)
  119. XZ.WriteLine "@echo off&mode con cols=" & TW &" lines=36"
  120. XZ.WriteLine "Title 本脚本由 Windows 更新补丁管理工具 UpdateMgr.vbs 生成  BY: FastSLZ  www.bathome.net" :XZ.Close
  121. Set AZ = Fso.CreateTextFile(BatAZ , True)
  122. AZ.WriteLine "@echo off&mode con cols=" & TW &" lines=36"
  123. AZ.WriteLine "Title 本脚本由 Windows 更新补丁管理工具 UpdateMgr.vbs 生成  BY: FastSLZ  www.bathome.net" :AZ.Close
  124. For i = 0 To MUSU.Count - 1
  125.     N1 = "" :N2 = ""
  126.     N1 = MUSU.Item(i).Categories.Item(0).Name :N2 = MUSU.Item(i).Categories.Item(1).Name
  127.     If InStr(MUSU.Item(i).Title,KBHide) = 0 and InStr(N1,"Office") = 0 and InStr(N2,"Office") = 0 Then
  128.     Cnum = Cnum + 1
  129.     Else
  130.     MUSU.Item(i).IsHidden = True
  131.     End If
  132. Next
  133. For i = 0 To MUSU.Count - 1
  134.     Tit="":Dow="":KBA="":Las="":Des="":Max="":Mor="":Des="":N1 = "" :N2 = ""
  135.     N1 = MUSU.Item(i).Categories.Item(0).Name :N2 = MUSU.Item(i).Categories.Item(1).Name
  136.     If InStr(MUSU.Item(i).Title,KBHide) = 0 and InStr(N1,"Office") = 0 and InStr(N2,"Office") = 0 Then
  137.         Tnum = Tnum + 1
  138.         Tit = Left(Tnum & "/" & Cnum &"    ",7) & "#:" & MUSU.Item(i).Title
  139.         Las = "发布日期:" &MUSU.Item(i).LastDeploymentChangeTime
  140.         Max = "文件大小:" &CheckSize(MUSU.Item(i).MaxDownloadSize)
  141.         Msr = "安全等级:" &MUSU.Item(i).MsrcSeverity
  142.         Mor = MUSU.Item(i).MoreInfoUrls(0)
  143.         KBA = MUSU.Item(i).KBArticleIDs(0)
  144.         For Each BU in MUSU.Item(i).BundledUpdates
  145.             Dow=BU.DownloadContents(0).DownloadURL
  146.             'If CStr(Err.Number) = "-2145124345" Then
  147.                'For Each BUD in BU.DownloadContents
  148.                'Wscript.Echo BUD.DownloadURL :Wscript.Echo Err.Number
  149.                'Next
  150.             'End If
  151.         Next
  152.         If UBound(Split(MUSU.Item(i).Description, "。")) > 0 Then
  153.         Des = "更新概述:" &Split(MUSU.Item(i).Description, "。")(0) &"。"
  154.         Else
  155.         Des = "更新概述:" &Split(MUSU.Item(i).Description, ".")(0) &"."
  156.         End If
  157.             'Wscript.Echo Tit
  158.             Wscript.Echo Tit &vbCrLf& Des &vbCrLf& Las &vbCrLf& Msr &vbCrLf& Max
  159.             Wscript.Echo "(KB)文章:" & KBA
  160.             Wscript.Echo "详情信息:" & Mor
  161.             Wscript.Echo "下载地址:" & Dow & vbCrLf
  162.                      XZAZ Tit,Dow,KBA,Las,Des
  163.     End If
  164. Next
  165. objAuto.Settings.NotificationLevel = AUO
  166. objAuto.Settings.IncludeRecommendedUpdates = IRU
  167. objAuto.Settings.Refresh
  168. objAuto.Settings.Save
  169. For Each oSvr in UpdateSvr
  170. If (sSvr="MANUAL") Or (sSvr="DISABLED") Then oSvr.StopService()
  171. oSvr.ChangeStartMode(sSvr)
  172. Next
  173. Set MUS=Nothing  :Set WS=Nothing  :Set Fso=Nothing   :Set CPath=Nothing :Set UpdateSvr=Nothing
  174. Set MUSU=Nothing :Set xOS=Nothing :Set BatXZ=Nothing :Set BatAZ=Nothing :Set WMI = Nothing
  175. Function XZAZ(Titles,DowURL,KBID,LDCT,Desc)
  176. Set XZ = Fso.OpenTextFile(BatXZ, 8, True)
  177. DowFile =  OSx & "_" & xOS & "\" & Split(DowURL, "/")(UBound(Split(DowURL, "/")))
  178. DowFileE = Split(DowFile, ".")(UBound(Split(DowFile, ".")))
  179. XZ.WriteLine "echo 正在下载:" & Titles & "&echo "&LDCT &vbCrLf&"echo "&Desc &"&echo,"
  180. XZ.WriteLine "Wget.exe -N -P " & OSx & "_" & xOS & " " & DowURL &" &&cls" & vbCrLf
  181. XZ.Close
  182. XPEXE = Array("dotnetfx_1938","rootsupd_","langpack_7b29")
  183. If InStr(Ucase(OSx),"XP") Then
  184.     If (InStr(LCase(DowFile), "dotnetfx_1938")) or (InStr(LCase(DowFile), "rootsupd_")) or (InStr(LCase(DowFile), "langpack_7b29")) Then
  185.         SetupS = Chr(34) & "%~dp0" & DowFile & Chr(34) & " /Q:A /R:N"
  186.     Else
  187.         If InStr(LCase(DowFileE), "exe") Then SetupS = Chr(34) & "%~dp0" & DowFile & Chr(34) & " /Q /U /Z /N"
  188.         If InStr(LCase(DowFileE), "msi") Then SetupS = Chr(34) & "%~dp0" & DowFile & Chr(34) & " /Q /U /Z /N"
  189.         If InStr(LCase(DowFileE), "cab") Then _
  190.             SetupS = "md " &Chr(34)&"%temp%\"&KBID&Chr(34) &" >nul 2>nul"&vbCrLf _
  191.             & "expand.exe " &Chr(34) & "%~dp0" & DowFile & Chr(34) &" -f:* " & Chr(34)&"%temp%\"&KBID&Chr(34) &vbCrLf _
  192.             & "for /r " & Chr(34)&"%temp%\"&KBID&Chr(34) & " %%i in (*.ms*) do msiexec /quiet /norestart /update %%~si &&rd "&Chr(34)&"%temp%\"&KBID&Chr(34)
  193.     End If
  194. Else
  195.     If InStr(LCase(DowFileE), "cab") Then SetupS = "pkgmgr.exe /ip /norestart /quiet /m:" & Chr(34) & "%~dp0" & DowFile & Chr(34)
  196.     'If Lcase(Right(DowFileE,10) = "dotnetfx40") Then SetupS = Chr(34) & "%~dp0" & DowFile & Chr(34) & " /Q /NoRestart"
  197.     If InStr(LCase(DowFileE), "exe") Then SetupS = Chr(34) & "%~dp0" & DowFile & Chr(34) & " /Quiet /NoRestart"
  198.     If InStr(LCase(DowFileE), "msi") Then SetupS = Chr(34) & "%~dp0" & DowFile & Chr(34) & " /Quiet /NoRestart"
  199.     If InStr(LCase(DowFileE), "msu") Then SetupS = Chr(34) & "%~dp0" & DowFile & Chr(34) & " /Quiet /NoRestart" '/passive
  200. End If
  201. Set AZ = Fso.OpenTextFile(BatAZ, 8, True)
  202. AZ.WriteLine "echo 正在安装:" & Titles
  203. AZ.WriteLine SetupS & vbCrLf
  204. AZ.Close
  205. PC = Array("mpas-","mpam-","am_","890830")
  206. For z = 0 To UBound(PC)
  207.     If InStr(LCase(DowFile),PC(z)) <> 0 Then
  208.     WS.Run("%Comspec% /C " &Chr(34) &"if not exist " &Chr(34)&CPath&DowFile&Chr(34) _
  209.     &" del /a/f/q "&Chr(34)&CPath&OSx&"_"&xOS&"\*"&PC(z)&"*.*" &Chr(34)&Chr(34)),vbHide
  210.     End If
  211. Next
  212. End Function
  213. Function Download(dlurl, file)
  214.   Set oxml = CreateObject("MSXML2.XMLHTTP")
  215.   Set ostream = CreateObject("adodb.stream")
  216.   oxml.open "get", dlurl, False
  217.   Do Until oxml.ReadyState = 1 : WScript.Sleep 200 : Loop
  218.   oxml.send() :ostream.Mode = 3 :ostream.Type = 1 :ostream.Open()
  219.   ostream.Write oxml.responseBody
  220.   ostream.SaveToFile file, 2
  221.   ostream.Close
  222.   Set oxml = Nothing :Set ostream = Nothing
  223. End Function
  224. Function MUAInfo(Rnum)
  225. MUAInfo = Rnum + 1 :aVer = "7.4.7600.226"
  226. DownUx = "正在下载最新的更新代理程序,请等候..."
  227. DownUU = "http://download.windowsupdate.com/WindowsUpdate/redist/standalone/"&aVer&"/WindowsUpdateAgent30-"&xOS&".exe"
  228. DownUF = CPath & OSx & "_" & xOS & "\WindowsUpdateAgent30-"&xOS&".exe"
  229. DownUR = Chr(34)& DownUF &Chr(34) &" /wuforce /quiet /norestart"
  230. Set uVer = CreateObject("Microsoft.Update.AgentInfo")
  231. If uVer.GetInfo("ProductVersionString") < aVer Then
  232.    Wscript.Echo DownUx :Download DownUU,DownUF :WS.Run DownUR ,1,True
  233. End If
  234. If MUAInfo = 3 Then Exit Function
  235. If (MUAInfo = 2) and (InStr(Ucase(OSx),"XP") > 0) Then
  236.    ErrD = ErrD & vbTab &"修复更新代理后仍错误!"
  237.    Wscript.Echo "尝试修复WinXP精简版更新代理错误,"&DownUx :Download DownUU,DownUF :WS.Run DownUR ,1,True :Set uVer = Nothing
  238. End If
  239. End Function
  240. Function CheckSize(SZ)
  241.     SZ = Replace(SZ,vbCrLf,"")
  242.     If SZ => 1048576 Then SZ = Round(SZ/1048576,2) & " MB" Else SZ = Round(SZ/1024,2) & " KB"
  243.     CheckSize = SZ
  244. End Function
复制代码
3

评分人数

    • stiger: 感谢分享技术 + 1
    • Batcher: 感谢分享PB + 10
    • BBCC: 太太太棒了.谢谢分享.PB + 8

可以用VBS就不用系統自帶的了

TOP

本帖最后由 xiangqian91 于 2015-1-5 16:37 编辑

在 win 8.1 版本 6.3.9600

通过批处理下载的补丁与 windows 更新 下载的补丁 不一致

似乎是update 版本的问题

又向大牛伸手了

TOP

用了两天,感觉非常好。

已经下载了 Win7 with SP1 x64的所有补丁,以及 Win8.1 with update x64的补丁。 虚拟机里实验后很满意。


突发奇想,如果能够支持Office的补丁下载就好了。

TOP

非常感谢

找了很多工具,试用之后,发觉我最需要,最满意的还是这个。

TOP

回复 4# lomey


    不是因为系统原因,而是不同显卡导致的,一般未安装显卡驱动导致,或部分集成显卡,即使报错也正常使用的

TOP

回复 1# fastslz


Win7与2008下DOS窗口有个小错误,望楼主更新一下

TOP

回复 2楼 的帖子

你没看见作者写的标题是VBS版嘛。
这不是批处理。
(*^_^*)

TOP

【系统辅助】VBS版Windows更新补丁管理工具

好长啊,看不懂, 这是批处理吗

TOP

返回列表