Blog

Install Microsoft and Windows Updates

A lot of scripts out there only install windows updates, this one gets the lot.
I’ve found that there are a number of patches not included in the LanGuard patch management, so here’s a script to fill in the gaps.

This script is a modified version of the one found here – blogs.technet.com/b/chrad/archive/2009/07/13/dynamic-provisioning-with-vmm-proxy-windows-updates-and-scripts.aspx

Also just added the ability to skip updates.
So if you don’t want the bing bar or windows search or any other update, just put add the name to the array. (Should also work with Most KB Numbers)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
Const ssDefault = 0
 Const ssManagedServer = 1 
 Const ssOthers = 3 'Microsoft Updates
 Const ssWindowsUpdate = 2 'Windows Updates
 'Array of updates to skip. 
 SkipUpdates = Array("bing","windows live","windows search")
 '#############################
 ' Create Session
 '#############################
 Set UpdateSession = CreateObject("Microsoft.Update.Session")
 Set UpdateSearcher = UpdateSession.CreateUpdateSearcher()
 '#############################
 ' Register Microsoft Update
 '#############################
 RegisterMu
 updateSearcher.ServerSelection = ssOthers '<==== **** CHANGE THIS TO YOUR PREFERENCE ****
 updateSearcher.ServiceID = "7971f918-a847-4430-9279-4a52d1efe18d"
 '#############################
 ' Create List of Updates to Download
 '#############################
 Set UpdatesToDownload = CreateObject("Microsoft.Update.UpdateColl")
 '#############################
 ' Search for Updates
 '#############################
 Set SearchResult = UpdateSearcher.Search("Isinstalled=0")
 '#############################
 ' Quit if No Updates Found
 '#############################
 If SearchResult.Updates.Count = 0 Then
     WScript.Echo "No updates found."
 WScript.Quit
 End If
 Dim strSpacer
 For I = 0 To SearchResult.Updates.Count-1
     Set Update = SearchResult.Updates.Item(I)
 
     ' formatting helper
     If I < 10 Then
         strSpacer = " "
     Else
         strSpacer = ""
     End If
 
     'Check updates to skip
 bSkip = false
 For Each Name in SkipUpdates
 If Instr(LCase(update.Title), LCase(Name)) > 0 Then
 bSkip = True
 Exit For
 End if
 Next
 If bSkip = True Then
 WScript.Echo "Skipping update " & update.Title
 Else
 'WScript.Echo "[" & strSpacer & I & "]  Found Update, Marking For Download:  " & update.Title
 UpdatesToDownload.Add(Update)
 End if 
 
 Next
 '#############################
 ' Download Updates
 '#############################
 Set Downloader = UpdateSession.CreateUpdateDownloader()
 Downloader.Updates = UpdatesToDownload
 On Error Resume Next
 Downloader.Download()
 If Err.number <> 0 Then
     Wscript.Echo "An error occurred in  Downloader.Download() of updates"
     Wscript.Echo "Number: " & err.number
     Wscript.Echo "Description:  " & err.Description
     Wscript.Quit (Err.number)
 End If
 On Error Goto 0
 '#############################
 ' Create List of Updates to Install
 '#############################
 Set UpdatesToInstall = CreateObject("Microsoft.Update.UpdateColl")
 For I = 0 To SearchResult.Updates.Count-1
     set Update = SearchResult.Updates.Item(I)
     If Update.IsDownloaded = true Then
         UpdatesToInstall.Add(Update)
     End If
 Next
 '#############################
 ' Install Updates
 '#############################
 Set Installer = UpdateSession.CreateUpdateInstaller()
 Installer.Updates = UpdatesToInstall
 Set InstallationResult = Installer.Install()
 wscript.Echo "Installation Result: " & InstallationResult.ResultCode
 wscript.Echo "Reboot Required: " & InstallationResult.RebootRequired
 wscript.Echo "Listing of updates installed and individual installation results:"
 
     For I = 0 to UpdatesToInstall.Count - 1
         WScript.Echo I + 1 & "> " & Chr(9) & UpdatesToInstall.Item(I).Title & Chr(9) & ": " & TranslateMuCode(InstallationResult.GetUpdateResult(i).ResultCode)
     Next
 '#############################
 ' Quit
 '#############################
 WScript.Quit
 '================================================================================
 ' Translate Microsoft Update Installation Results
 '================================================================================
 Function TranslateMuCode(theCode)
   TranslateMuCode = "[" & theCode & "] "
   if (theCode = 0) Then TranslateMuCode = TranslateMuCode & "Not Started"
   if (theCode = 1) Then TranslateMuCode = TranslateMuCode & "In Progress"
   if (theCode = 2) Then TranslateMuCode = TranslateMuCode & "Succeeded"
   if (theCode = 3) Then TranslateMuCode = TranslateMuCode & "Succeeded with Errors"
   if (theCode = 4) Then TranslateMuCode = TranslateMuCode & "Failed"
   if (theCode = 5) Then TranslateMuCode = TranslateMuCode & "Aborted"
 End Function
 '================================================================================
 ' Register Microsoft Update (if never registered)
 '================================================================================
 Function RegisterMu
     Dim fso
     Dim file
     Dim WshShell
     Dim updateService
     Dim updateServiceManager
 
     found = false
 
     Set fso = CreateObject("Scripting.FileSystemObject")    
     Set WshShell = WScript.CreateObject ("WScript.Shell")
 
     Set updateServiceManager = CreateObject("Microsoft.Update.ServiceManager")
     Set updateService = updateServiceManager.Services
 
     If err <> 0 Then
         WScript.Echo "CreateObject(Microsoft.Update.ServiceManager) failed with error 0x" & Hex(err.Number)  & err.Description
         WScript.Quit(2)
     End If
     For I=0 to updateService.Count - 1
         Set item = updateService.Item(i)
         If item.ServiceID = "7971f918-a847-4430-9279-4a52d1efe18d" Then
             found = true
         End IF
     Next    
 
     IF found = false Then
         updateServiceManager.AddService2 "7971f918-a847-4430-9279-4a52d1efe18d", 2, ""
 
         If err <> 0 Then
             WScript.Echo "updateServiceManager.AddService() failed with error 0x" & Hex(err.Number) & err.Description
         Else
             WScript.Echo "MU is registered with WU Agent"
         End IF
     End IF
 END Function

Posted in: Scripts, Technical

Leave a Comment (3) ↓

3 Comments

  1. Tim Beard June 21, 2013

    What is the difference in this script “Install Microsoft and Windows Updates” and your other script “PatchMyPC Script”? do I need both or will one do and if so, which is the best?

    reply
    • Jake June 21, 2013

      Hey Tim,

      PatchMyPC will only do windows updates.
      If you want Microsoft updates then the other script is the way to go.
      I will get in touch with the developer and see if he will integrate the script into PatchMyPC as that would be great.

      reply
  2. Jack Ryan May 2, 2014

    Great script. I have one question. The SkipUpdates Array section works great for like Bing and Microsoft Security Essentials however despite any combination for Internet Explorer 11 for Windows 7, the script continues to installs it even though it is displayed as a skipped item in command prompt.

    I’ve used Internet Explorer, Internet Explorer 11 for Windows 7 and even the KB article but it continues to install it.

    Any ideas?

    reply

Leave a Comment