/
OfflineUpdateScan.ps1
346 lines (295 loc) · 9.82 KB
/
OfflineUpdateScan.ps1
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<#
.SYNOPSIS
Scans a computer for Windows updates using the offline CAB file
.DESCRIPTION
Scans a computer for Windows updates using the offline CAB file, or creates a Scheduled Task to do so.
.PARAMETER AddTask
Specifies that a Scheduled Task should be created.
.PARAMETER Run
Specifies that a scan should be run interactively
.PARAMETER Format
Specifies the report format: CSV, HTML or XML for files, Console for the ineractive screen.
.PARAMETER Path
Specifies the file path for the report file.
.PARAMETER StartAt
Specifies a Date/Time that the Scheduled Task should start. By default the task will start 45 seconds after it is created.
.PARAMETER CabSource
The location of the Microsoft Offline Update CAB file. Defaults to the user's Documents folder.
.INPUTS
None. You cannot pipe objects to this script.
.OUTPUTS
None to the pipeline.
.EXAMPLE
OfflineUpdateScan.ps1 -AddTask
This example creates a Scheduled Task that will run in 45 seconds time.
.EXAMPLE
OfflineUpdateScan.ps1 -Run
This example starts an offline scan.
.NOTES
#>
Param(
[Parameter(Mandatory=$true,ParameterSetName='Task')]
[Switch]
$AddTask,
[Parameter(Mandatory=$false,ParameterSetName='Task')]
[DateTime]
$StartAt,
[Parameter(Mandatory=$true,ParameterSetName="Exec")]
[Switch]
$Run,
[Parameter(ParameterSetName="Exec")]
[Parameter(ParameterSetName="Task")]
[ValidateSet("CSV","XML","Console","HTML")]
[String]
$Format="csv",
[Parameter(Mandatory=$false,ParameterSetName="Exec")]
[Parameter(Mandatory=$false,ParameterSetName="Task")]
[String]
$Path,
[Parameter(ParameterSetName="Exec")]
[String]
$CabSource = "$env:userprofile\documents\wsusscn2.cab"
)
$ScriptGuid = '50bf2b41-ffb4-4381-b693-71a14f5874dd'
## Constant Enums for Schedule Tasks. Derived from taskschd.h
Add-Type -TypeDefinition @"
public enum TASK_RUN_FLAGS
{
TASK_RUN_NO_FLAGS = 0,
TASK_RUN_AS_SELF = 0x1,
TASK_RUN_IGNORE_CONSTRAINTS = 0x2,
TASK_RUN_USE_SESSION_ID = 0x4,
TASK_RUN_USER_SID = 0x8
}
public enum TASK_ENUM_FLAGS
{
TASK_ENUM_HIDDEN = 0x1
}
public enum TASK_LOGON_TYPE
{
TASK_LOGON_NONE = 0,
TASK_LOGON_PASSWORD = 1,
TASK_LOGON_S4U = 2,
TASK_LOGON_INTERACTIVE_TOKEN = 3,
TASK_LOGON_GROUP = 4,
TASK_LOGON_SERVICE_ACCOUNT = 5,
TASK_LOGON_INTERACTIVE_TOKEN_OR_PASSWORD = 6
}
public enum TASK_RUNLEVEL
{
TASK_RUNLEVEL_LUA = 0,
TASK_RUNLEVEL_HIGHEST = 1
}
public enum TASK_PROCESSTOKENSID
{
TASK_PROCESSTOKENSID_NONE = 0,
TASK_PROCESSTOKENSID_UNRESTRICTED = 1,
TASK_PROCESSTOKENSID_DEFAULT = 2
}
public enum TASK_STATE
{
TASK_STATE_UNKNOWN = 0,
TASK_STATE_DISABLED = 1,
TASK_STATE_QUEUED = 2,
TASK_STATE_READY = 3,
TASK_STATE_RUNNING = 4
}
public enum TASK_CREATION
{
TASK_VALIDATE_ONLY = 0x1,
TASK_CREATE = 0x2,
TASK_UPDATE = 0x4,
TASK_CREATE_OR_UPDATE = ( TASK_CREATE | TASK_UPDATE ) ,
TASK_DISABLE = 0x8,
TASK_DONT_ADD_PRINCIPAL_ACE = 0x10,
TASK_IGNORE_REGISTRATION_TRIGGERS = 0x20
}
public enum TASK_TRIGGER_TYPE2
{
TASK_TRIGGER_EVENT = 0,
TASK_TRIGGER_TIME = 1,
TASK_TRIGGER_DAILY = 2,
TASK_TRIGGER_WEEKLY = 3,
TASK_TRIGGER_MONTHLY = 4,
TASK_TRIGGER_MONTHLYDOW = 5,
TASK_TRIGGER_IDLE = 6,
TASK_TRIGGER_REGISTRATION = 7,
TASK_TRIGGER_BOOT = 8,
TASK_TRIGGER_LOGON = 9,
TASK_TRIGGER_SESSION_STATE_CHANGE = 11,
TASK_TRIGGER_CUSTOM_TRIGGER_01 = 12
}
public enum TASK_SESSION_STATE_CHANGE_TYPE
{
TASK_CONSOLE_CONNECT = 1,
TASK_CONSOLE_DISCONNECT = 2,
TASK_REMOTE_CONNECT = 3,
TASK_REMOTE_DISCONNECT = 4,
TASK_SESSION_LOCK = 7,
TASK_SESSION_UNLOCK = 8
}
public enum TASK_ACTION_TYPE
{
TASK_ACTION_EXEC = 0,
TASK_ACTION_COM_HANDLER = 5,
TASK_ACTION_SEND_EMAIL = 6,
TASK_ACTION_SHOW_MESSAGE = 7
}
public enum TASK_INSTANCES_POLICY
{
TASK_INSTANCES_PARALLEL = 0,
TASK_INSTANCES_QUEUE = 1,
TASK_INSTANCES_IGNORE_NEW = 2,
TASK_INSTANCES_STOP_EXISTING = 3
}
public enum TASK_COMPATIBILITY
{
TASK_COMPATIBILITY_AT = 0,
TASK_COMPATIBILITY_V1 = 1,
TASK_COMPATIBILITY_V2 = 2,
TASK_COMPATIBILITY_V2_1 = 3,
TASK_COMPATIBILITY_V2_2 = 4,
TASK_COMPATIBILITY_V2_3 = 5,
TASK_COMPATIBILITY_V2_4 = 6
}
"@
#Constants from wuapi.h
Add-Type -TypeDefinition @"
public enum OperationResultCode
{
orcNotStarted = 0,
orcInProgress = 1,
orcSucceeded = 2,
orcSucceededWithErrors = 3,
orcFailed = 4,
orcAborted = 5
}
"@
#Constant from wuapicommon.h
Add-Type -TypeDefinition @"
public enum ServerSelection
{
ssDefault = 0,
ssManagedServer = 1,
ssWindowsUpdate = 2,
ssOthers = 3
}
"@
Function Remove-OfflineUpdateScantask {
$STService = New-Object -ComObject Schedule.Service
$STService.Connect()
$RootFolder = $STService.GetFolder("\")
try {
$RootFolder.DeleteTask($Script:ScriptGuid,$Null)
}
catch {}
}
Function Add-OfflineUpdateScanTask {
$STService = New-Object -ComObject Schedule.Service
$STService.Connect()
$RootFolder = $STService.GetFolder("\")
$NewTaskDef = $STService.NewTask(0)
$RegInfo = $NewTaskDef.RegistrationInfo
$RegInfo.Description = "Offline Update Scan"
$RegInfo.Author = "Stuart Squibb"
$Principal = $NewTaskDef.Principal
$Principal.LogonType = [TASK_LOGON_TYPE]::Task_Logon_Service_Account
$Principal.UserId = 'NT AUTHORITY\SYSTEM'
$Principal.Id = "System"
$Principal | Select-Object * | Write-Verbose
$Settings = $NewTaskDef.Settings
$Settings.Enabled = $True
$Settings.DisallowStartIfOnBatteries = $False
$Trigger = $NewTaskDef.Triggers.Create([TASK_TRIGGER_TYPE2]::TASK_TRIGGER_TIME)
if ($Script:StartAt) {
$StartTime = $Script:StartAt
Write-Verbose $StartTime
}
else {
$StartTime = (Get-Date).AddSeconds(45)
}
$EndTime = ($StartTime.AddMinutes(5)).ToString("yyyy-MM-ddTHH:mm:ss")
$StartTime = $StartTime.toString("yyyy-MM-ddTHH:mm:ss")
Write-Verbose "Time Now : $((Get-Date).ToString('yyyy-MM-ddTHH:mm:ss'))"
Write-Verbose "Start Time: $($StartTime)"
Write-Verbose "End Time : $($EndTime)"
$Trigger.StartBoundary = $StartTime
$Trigger.EndBoundary = $EndTime
$Trigger.ExecutionTimeLimit = "PT5M"
$Trigger.Id = "TimeTriggerId"
$Trigger.Enabled = $True
$Action = $NewTaskDef.Actions.Create([TASK_ACTION_TYPE]::TASK_ACTION_EXEC)
$Action.Path = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"
$Action.Arguments = "-ExecutionPolicy ByPass -NoProfile -NonInteractive -File C:\OfflineUpdateScan\OfflineUpdateScan.ps1 -Run -Format $Format -Path $Path"
$Action.WorkingDirectory = "C:\OfflineUpdateScan"
Write-Verbose "Task Definition created. About to submit Task..."
[Void]$RootFolder.RegisterTaskDefinition($ScriptGuid, $NewTaskDef,[TASK_CREATION]::TASK_CREATE_OR_UPDATE,$Null,$Null,$Null)
Write-Verbose "Task $ScriptGuid Submitted"
}
#Microsoft link for CAB file: http://go.microsoft.com/fwlink/?LinkID=74689
#WIN-90CID1J2CS5
$WorkDirectory = 'C:\OfflineUpdateScan'
$CabLocation = "$WorkDirectory\wsusscn2.cab"
if (!($Path)) {
$Path = "C:\OfflineUpdateScan\OfflineUpdateScan.$($Format)"
}
# get-updatecollection
Function Get-OfflineUpdateCollection {
$UpdateServiceManager = New-Object -ComObject "Microsoft.Update.ServiceManager"
$UpdateService = $UpdateServiceManager.AddScanPackageService("Offline Sync Service", $CabLocation, 1)
$UpdateSession = New-Object -ComObject "Microsoft.Update.Session"
$UpdateSearcher = $UpdateSession.CreateUpdateSearcher()
Write-Verbose "Searching for updates..."
$UpdateSearcher.ServerSelection = [ServerSelection]::SSOthers
$UpdateSearcher.ServiceID = $UpdateService.ServiceID
$SearchResult = $UpdateSearcher.Search("IsInstalled=0")
$Updates = $SearchResult.Updates
Write-Verbose "$($Updates.Count) updates found."
If ($Updates.Count -eq 0) {
#This area blank by design
}
else {
Return $Updates
}
}
# export-updatecollection
Function Export-OfflineUpdateCollection {
Param (
[Parameter(Mandatory=$True)]
[ValidateSet("xml","csv","console","html")]
[String]
$Format,
[Parameter(Mandatory=$False)]
[String]
$FileName,
[Parameter(ValueFromPipeline=$True,Mandatory=$True)]
[Object]
$OfflineUpdateCollection
)
$OutPutObject = $OfflineUpdateCollection | Select-Object -Property MsrcSeverity, Title, MaxDownloadSize, MinDownloadSize, @{Name="KBs";Expression={$_.KBArticleIds -join ';'}}
switch ($Format) {
'csv' { $OutPutObject | Export-Csv -Path $FileName -NoTypeInformation }
'xml' { ($OutPutObject | ConvertTo-Xml -NoTypeInformation -As Document).OuterXML | Out-File -FilePath $FileName }
'html' { $OutPutObject | ConvertTo-Html -Title "Needed Windows Updates for $env:ComputerName" | Out-File -FilePath $FileName}
'console' { Format-Table -InputObject $OutPutObject}
}
}
if ($AddTask.IsPresent) {
Add-OfflineUpdateScanTask
}
if ($Run.IsPresent) {
try {
New-Item -ItemType Directory -Path $WorkDirectory -ErrorAction Stop
}
catch {}
try {
Copy-Item $CabSource -Destination $CabLocation -ErrorAction Stop
}
catch {}
Write-Verbose "Exporting $Format format file to $Path"
$ExportCollection = Get-OfflineUpdateCollection
if ($ExportCollection) {
Export-OfflineUpdateCollection -Format $Format -FileName $Path -OfflineUpdateCollection $ExportCollection
}
Remove-OfflineUpdateScantask
}