标题: [原创] VBS版Windows更新补丁管理工具 [打印本页]
作者: fastslz 时间: 2009-3-15 18:17 标题: VBS版Windows更新补丁管理工具
本人一直以来Windows 更新补丁管理而烦恼,花了大量的时间完成这段代码
2009年因为无法在线获取Windows 更新补丁补丁,所以代码还不够完美,困惑了2年多终于有办法了- 'Windows 更新补丁管理工具 v1.01 UpdateMgr.vbs BY:FastSLZ
- '转载请注明出处http://www.bathome.net/thread-3722-1-1.html
- '1.本脚本能在线获取多种方式的最新更新、采集当前系统全部微软推送更新
- '2.自动生成批量下载及批量安装批处理文件,方便联网状态下载,脱机状态安装
- '3.支持命令提示符下带参数运行
- '4.枚举重要更新命令及参数 Cscript.exe //NoLogo UpdateMgr.vbs 1
- '5.枚举可选更新并过滤隐藏关键字 Cscript.exe //NoLogo UpdateMgr.vbs 2 "KB2483139"
- '6.补丁安装顺序自动排列
- '7.自动下载安装最新Microsoft Update Agent 3.0
-
- Set WH = GetObject("Winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery _
- ("Select * From Win32_DesktopMonitor")
- For Each aWH in WH
- TW = Int(aWH.ScreenWidth/8)
- Next
- Set WH = Nothing
- If TW <= 0 Then :TW = 128 :End If:TW = TW - 4
- If (Lcase(Right(Wscript.FullName,11)) = "wscript.exe") Then
- CreateObject("WScript.Shell").Run("%Comspec% /C " &Chr(34)&"mode con cols="& TW _
- &"&Title Windows 更新补丁管理工具 UpdateMgr.vbs BY: FastSLZ www.bathome.net&Cscript.exe //NoLogo " &Chr(34) _
- &Wscript.ScriptFullName&Chr(34)&"&&(Echo 此窗口40秒后自动关闭...&Ping -n 40 127.0.1>nul&Exit)"&Chr(34)),3:Wscript.Quit
- End If
- Tnum = 0 :Cnum = 0:St = 0:MUAnum = 0
- Dim WMI,WS,Fso,objAuto,CPath,MUSU,xOS,OSx,BatXZ,BatAZ,MUAnum,ErrD
- Set WS=CreateObject("Wscript.Shell")
- Set WMI = GetObject("Winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
- Set cOSs = WMI.ExecQuery("Select * from Win32_OperatingSystem")
- Set Fso = CreateObject("Scripting.FileSystemObject")
- Set oArg = WScript.Arguments
- KBHide = "KB2483139"
- If oArg.Count = 1 Then St = oArg(0)
- If oArg.Count = 2 Then St = oArg(0):KBHide = oArg(1)
- If oArg.Count > 2 Then WS.Popup "命令及参数错误!" &vbTab ,4,"错误",4160 :WScript.Quit
- Set oArg = Nothing
- For Each oOS in cOSs
- OSx = Split(Replace(oOS.Caption,"Microsoft Windows " ,"Win"), " ")(0)
- If InStr(oOS.CSDVersion, "") Then SPx = Replace(oOS.CSDVersion,"Service Pack " ,"_SP")
- xVer=Split(oOS.Version,".")(0)
- Next
- OSx = Trim(OSx & SPx)
- Set UpdateSvr = WMI.ExecQuery("Select * from Win32_Service Where Name = 'wuauserv'")
- If UpdateSvr.Count = 0 Then
- WS.Popup "Windows 更新服务不可用!" &vbTab ,4,"检查服务",4160 :WScript.Quit
- Else
- For Each oSvr in UpdateSvr
- sSvr = UCase(oSvr.StartMode) :oSvr.ChangeStartMode("Automatic")
- If oSvr.State = "Stopped" Then oSvr.StartService() Else oSvr.StopService() :WScript.Sleep 500 :oSvr.StartService()
- Next
- End If
- Set B = WMI.ExecQuery("Select * from Win32_PingStatus Where address = 'www.baidu.com'")
- For Each A in B
- If IsNull(A.StatusCode) Or A.StatusCode<>0 Then WS.Popup "网络连接不可用!" &vbTab ,4,"检查网络",4160 :WScript.Quit
- Next
- Set SysEnv = WS.Environment("SYSTEM")
- Select Case UCase(SysEnv("PROCESSOR_ARCHITECTURE"))
- Case "X86"
- xOS = "x86"
- Case "AMD64"
- xOS = "x64"
- Case "IA64"
- xOS = "ia64"
- End Select
- Set B = Nothing:Set SysEnv = Nothing
- On Error Resume Next
- CPath = WS.CurrentDirectory & "\"
- MUAnum = MUAInfo(MUAnum)
- Do Until MUAnum = 2
- Set MUSU = Nothing:Set objAuto = Nothing
- Set objAuto = CreateObject("Microsoft.Update.AutoUpdate")
- objAuto.EnableService
- AUO = objAuto.Settings.NotificationLevel
- IRU = objAuto.Settings.IncludeRecommendedUpdates
- objAuto.Settings.NotificationLevel = 2
- If xVer < 5 Then objAuto.Settings.NonAdministratorsElevated = -1
- If IRU <> 0 Then objAuto.Settings.IncludeRecommendedUpdates = 0
- objAuto.Settings.ScheduledInstallationDay = 5
- objAuto.Settings.ScheduledInstallationTime = 12
- objAuto.Settings.Refresh
- objAuto.Settings.Save
- If St < 1 Then
-
- WScript.Echo _
- "仅重要更新" &vbTab&vbTab &"(1 Enter)" & vbCrLf _
- &"仅可选更新" &vbTab&vbTab &"(2 Enter)" & vbCrLf _
- &"重要+可选" &vbTab&vbTab &"(3 Enter)" & vbCrLf _
- &"已安装更新" &vbTab&vbTab &"(4 Enter)" & vbCrLf _
- &"采集当前系统全部更新" &vbTab& "(5 Enter)" & vbCrL
- St = WScript.StdIn.Readline
- End If
- Waitx = "正在查找适用于您计算机的最新更新程序,持续时间取决与你的网络带宽,请耐心等候..." & vbCrLf
- Select Case St
- Case "1"
- Wscript.Echo Waitx:Set MUS = CreateObject("Microsoft.Update.Searcher").Search _
- ("Type='Software' and (IsHidden=0 and IsAssigned=1 and IsInstalled=0)")
- Case "2"
- Wscript.Echo Waitx:Set MUS = CreateObject("Microsoft.Update.Searcher").Search _
- ("Type='Software' and (IsHidden=0 and IsAssigned=0 and IsInstalled=0)")
- Case "3"
- Wscript.Echo Waitx:Set MUS = CreateObject("Microsoft.Update.Searcher").Search _
- ("Type='Software' and (IsHidden=0 and IsAssigned=0 and IsInstalled=0) or (IsHidden=0 and IsAssigned=1 and IsInstalled=0)")
- Case "4"
- Wscript.Echo Waitx:Set MUS = CreateObject("Microsoft.Update.Searcher").Search _
- ("Type='Software' and IsHidden=0 and IsInstalled=1")
- Case "5"
- Wscript.Echo Waitx:Set MUS = CreateObject("Microsoft.Update.Searcher").Search _
- ("Type='Software'")
- Case Else
- WS.Popup "请按1~5然后回车!" &vbTab ,4,"请选择",4160 :WScript.Quit
- End Select
- Set MUSU = MUS.Updates
- ErrN = Err.Number :ErrD = Err.Description
- If MUSU.Count = 0 Then WS.Popup "您的系统是最新的!" & vbTab ,4,"恭喜",4160 :WScript.Quit
- If CStr(ErrN) = 0 Then Exit Do
- If CStr(ErrN) = "424" Then :MUAnum = MUAInfo(MUAnum)
- If CStr(ErrN) <> 0 Then Wscript.Echo "Error # " & ErrN & vbTab & ErrD :WScript.Quit
- Loop
-
- BatXZ = CPath & OSx & "_" & xOS & "_批量下载" & ".cmd"
- BatAZ = CPath & OSx & "_" & xOS & "_批量安装" & ".cmd"
- If TW > 152 Then TW =152
- Set XZ = Fso.CreateTextFile(BatXZ , True)
- XZ.WriteLine "@echo off&mode con cols=" & TW &" lines=36"
- XZ.WriteLine "Title 本脚本由 Windows 更新补丁管理工具 UpdateMgr.vbs 生成 BY: FastSLZ www.bathome.net" :XZ.Close
- Set AZ = Fso.CreateTextFile(BatAZ , True)
- AZ.WriteLine "@echo off&mode con cols=" & TW &" lines=36"
- AZ.WriteLine "Title 本脚本由 Windows 更新补丁管理工具 UpdateMgr.vbs 生成 BY: FastSLZ www.bathome.net" :AZ.Close
- For i = 0 To MUSU.Count - 1
- N1 = "" :N2 = ""
- N1 = MUSU.Item(i).Categories.Item(0).Name :N2 = MUSU.Item(i).Categories.Item(1).Name
- If InStr(MUSU.Item(i).Title,KBHide) = 0 and InStr(N1,"Office") = 0 and InStr(N2,"Office") = 0 Then
- Cnum = Cnum + 1
- Else
- MUSU.Item(i).IsHidden = True
- End If
- Next
- For i = 0 To MUSU.Count - 1
- Tit="":Dow="":KBA="":Las="":Des="":Max="":Mor="":Des="":N1 = "" :N2 = ""
- N1 = MUSU.Item(i).Categories.Item(0).Name :N2 = MUSU.Item(i).Categories.Item(1).Name
- If InStr(MUSU.Item(i).Title,KBHide) = 0 and InStr(N1,"Office") = 0 and InStr(N2,"Office") = 0 Then
- Tnum = Tnum + 1
- Tit = Left(Tnum & "/" & Cnum &" ",7) & "#:" & MUSU.Item(i).Title
- Las = "发布日期:" &MUSU.Item(i).LastDeploymentChangeTime
- Max = "文件大小:" &CheckSize(MUSU.Item(i).MaxDownloadSize)
- Msr = "安全等级:" &MUSU.Item(i).MsrcSeverity
- Mor = MUSU.Item(i).MoreInfoUrls(0)
- KBA = MUSU.Item(i).KBArticleIDs(0)
- For Each BU in MUSU.Item(i).BundledUpdates
- Dow=BU.DownloadContents(0).DownloadURL
- 'If CStr(Err.Number) = "-2145124345" Then
- 'For Each BUD in BU.DownloadContents
- 'Wscript.Echo BUD.DownloadURL :Wscript.Echo Err.Number
- 'Next
- 'End If
- Next
- If UBound(Split(MUSU.Item(i).Description, "。")) > 0 Then
- Des = "更新概述:" &Split(MUSU.Item(i).Description, "。")(0) &"。"
- Else
- Des = "更新概述:" &Split(MUSU.Item(i).Description, ".")(0) &"."
- End If
- 'Wscript.Echo Tit
- Wscript.Echo Tit &vbCrLf& Des &vbCrLf& Las &vbCrLf& Msr &vbCrLf& Max
- Wscript.Echo "(KB)文章:" & KBA
- Wscript.Echo "详情信息:" & Mor
- Wscript.Echo "下载地址:" & Dow & vbCrLf
- XZAZ Tit,Dow,KBA,Las,Des
- End If
- Next
- objAuto.Settings.NotificationLevel = AUO
- objAuto.Settings.IncludeRecommendedUpdates = IRU
- objAuto.Settings.Refresh
- objAuto.Settings.Save
- For Each oSvr in UpdateSvr
- If (sSvr="MANUAL") Or (sSvr="DISABLED") Then oSvr.StopService()
- oSvr.ChangeStartMode(sSvr)
- Next
- Set MUS=Nothing :Set WS=Nothing :Set Fso=Nothing :Set CPath=Nothing :Set UpdateSvr=Nothing
- Set MUSU=Nothing :Set xOS=Nothing :Set BatXZ=Nothing :Set BatAZ=Nothing :Set WMI = Nothing
-
- Function XZAZ(Titles,DowURL,KBID,LDCT,Desc)
- Set XZ = Fso.OpenTextFile(BatXZ, 8, True)
- DowFile = OSx & "_" & xOS & "\" & Split(DowURL, "/")(UBound(Split(DowURL, "/")))
- DowFileE = Split(DowFile, ".")(UBound(Split(DowFile, ".")))
- XZ.WriteLine "echo 正在下载:" & Titles & "&echo "&LDCT &vbCrLf&"echo "&Desc &"&echo,"
- XZ.WriteLine "Wget.exe -N -P " & OSx & "_" & xOS & " " & DowURL &" &&cls" & vbCrLf
- XZ.Close
- XPEXE = Array("dotnetfx_1938","rootsupd_","langpack_7b29")
- If InStr(Ucase(OSx),"XP") Then
- If (InStr(LCase(DowFile), "dotnetfx_1938")) or (InStr(LCase(DowFile), "rootsupd_")) or (InStr(LCase(DowFile), "langpack_7b29")) Then
- SetupS = Chr(34) & "%~dp0" & DowFile & Chr(34) & " /Q:A /R:N"
- Else
- If InStr(LCase(DowFileE), "exe") Then SetupS = Chr(34) & "%~dp0" & DowFile & Chr(34) & " /Q /U /Z /N"
- If InStr(LCase(DowFileE), "msi") Then SetupS = Chr(34) & "%~dp0" & DowFile & Chr(34) & " /Q /U /Z /N"
- If InStr(LCase(DowFileE), "cab") Then _
- SetupS = "md " &Chr(34)&"%temp%\"&KBID&Chr(34) &" >nul 2>nul"&vbCrLf _
- & "expand.exe " &Chr(34) & "%~dp0" & DowFile & Chr(34) &" -f:* " & Chr(34)&"%temp%\"&KBID&Chr(34) &vbCrLf _
- & "for /r " & Chr(34)&"%temp%\"&KBID&Chr(34) & " %%i in (*.ms*) do msiexec /quiet /norestart /update %%~si &&rd "&Chr(34)&"%temp%\"&KBID&Chr(34)
- End If
- Else
- If InStr(LCase(DowFileE), "cab") Then SetupS = "pkgmgr.exe /ip /norestart /quiet /m:" & Chr(34) & "%~dp0" & DowFile & Chr(34)
- 'If Lcase(Right(DowFileE,10) = "dotnetfx40") Then SetupS = Chr(34) & "%~dp0" & DowFile & Chr(34) & " /Q /NoRestart"
- If InStr(LCase(DowFileE), "exe") Then SetupS = Chr(34) & "%~dp0" & DowFile & Chr(34) & " /Quiet /NoRestart"
- If InStr(LCase(DowFileE), "msi") Then SetupS = Chr(34) & "%~dp0" & DowFile & Chr(34) & " /Quiet /NoRestart"
- If InStr(LCase(DowFileE), "msu") Then SetupS = Chr(34) & "%~dp0" & DowFile & Chr(34) & " /Quiet /NoRestart" '/passive
- End If
- Set AZ = Fso.OpenTextFile(BatAZ, 8, True)
- AZ.WriteLine "echo 正在安装:" & Titles
- AZ.WriteLine SetupS & vbCrLf
- AZ.Close
- PC = Array("mpas-","mpam-","am_","890830")
- For z = 0 To UBound(PC)
- If InStr(LCase(DowFile),PC(z)) <> 0 Then
- WS.Run("%Comspec% /C " &Chr(34) &"if not exist " &Chr(34)&CPath&DowFile&Chr(34) _
- &" del /a/f/q "&Chr(34)&CPath&OSx&"_"&xOS&"\*"&PC(z)&"*.*" &Chr(34)&Chr(34)),vbHide
- End If
- Next
- End Function
-
- Function Download(dlurl, file)
- Set oxml = CreateObject("MSXML2.XMLHTTP")
- Set ostream = CreateObject("adodb.stream")
- oxml.open "get", dlurl, False
- Do Until oxml.ReadyState = 1 : WScript.Sleep 200 : Loop
- oxml.send() :ostream.Mode = 3 :ostream.Type = 1 :ostream.Open()
- ostream.Write oxml.responseBody
- ostream.SaveToFile file, 2
- ostream.Close
- Set oxml = Nothing :Set ostream = Nothing
- End Function
-
- Function MUAInfo(Rnum)
- MUAInfo = Rnum + 1 :aVer = "7.4.7600.226"
- DownUx = "正在下载最新的更新代理程序,请等候..."
- DownUU = "http://download.windowsupdate.com/WindowsUpdate/redist/standalone/"&aVer&"/WindowsUpdateAgent30-"&xOS&".exe"
- DownUF = CPath & OSx & "_" & xOS & "\WindowsUpdateAgent30-"&xOS&".exe"
- DownUR = Chr(34)& DownUF &Chr(34) &" /wuforce /quiet /norestart"
- Set uVer = CreateObject("Microsoft.Update.AgentInfo")
- If uVer.GetInfo("ProductVersionString") < aVer Then
- Wscript.Echo DownUx :Download DownUU,DownUF :WS.Run DownUR ,1,True
- End If
- If MUAInfo = 3 Then Exit Function
- If (MUAInfo = 2) and (InStr(Ucase(OSx),"XP") > 0) Then
- ErrD = ErrD & vbTab &"修复更新代理后仍错误!"
- Wscript.Echo "尝试修复WinXP精简版更新代理错误,"&DownUx :Download DownUU,DownUF :WS.Run DownUR ,1,True :Set uVer = Nothing
- End If
- End Function
-
- Function CheckSize(SZ)
- SZ = Replace(SZ,vbCrLf,"")
- If SZ => 1048576 Then SZ = Round(SZ/1048576,2) & " MB" Else SZ = Round(SZ/1024,2) & " KB"
- CheckSize = SZ
- End Function
复制代码
作者: lujiaqu 时间: 2009-7-30 11:48 标题: 【系统辅助】VBS版Windows更新补丁管理工具
好长啊,看不懂, 这是批处理吗
作者: keen 时间: 2009-7-30 12:08 标题: 回复 2楼 的帖子
你没看见作者写的标题是VBS版嘛。
这不是批处理。
作者: lomey 时间: 2013-12-29 14:49
回复 1# fastslz
Win7与2008下DOS窗口有个小错误,望楼主更新一下
作者: fastslz 时间: 2013-12-29 15:29
回复 4# lomey
不是因为系统原因,而是不同显卡导致的,一般未安装显卡驱动导致,或部分集成显卡,即使报错也正常使用的
作者: stiger 时间: 2014-5-31 09:46
非常感谢
找了很多工具,试用之后,发觉我最需要,最满意的还是这个。
作者: stiger 时间: 2014-6-1 23:13
用了两天,感觉非常好。
已经下载了 Win7 with SP1 x64的所有补丁,以及 Win8.1 with update x64的补丁。 虚拟机里实验后很满意。
突发奇想,如果能够支持Office的补丁下载就好了。
作者: xiangqian91 时间: 2015-1-5 14:35
本帖最后由 xiangqian91 于 2015-1-5 16:37 编辑
在 win 8.1 版本 6.3.9600
通过批处理下载的补丁与 windows 更新 下载的补丁 不一致
似乎是update 版本的问题
又向大牛伸手了
作者: 飛雪 时间: 2015-2-27 03:02
可以用VBS就不用系統自帶的了
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |