-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathOfficeUpdater.ps1
More file actions
513 lines (436 loc) · 15.3 KB
/
Copy pathOfficeUpdater.ps1
File metadata and controls
513 lines (436 loc) · 15.3 KB
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
<#
.SYNOPSIS
Microsoft Office Updater
.DESCRIPTION
This script will keep the Updates folder populated with the latest Office
updates that SCCM has downloaded. It should be setup to execute as a
scheduled task on the SCCM server. I suggest executing it once a week.
.PARAMETER Country
The language-country of the updates
.PARAMETER EmailLogs
True or false on whether to email logs of the latest updates applied to the Office Updates folder.
.PARAMETER EmailRecipients
Email address to send report to
.PARAMETER EmailSender
Sender email address
.PARAMETER ExclusionFileName
Text file containing a list of updates to exclude
.PARAMETER LogFileName
Name of the log file to be written to
.PARAMETER ProcessedUpdatesFile
Name of the file containing a list of Microsoft Office updates that have already been copied over
.PARAMETER SMTPServer
fully qualified SMTP server name
.PARAMETER SourceFolder
The folder where SCCM stores the updates
.PARAMETER UpdatesFolder
The folder where Microsoft Office looks for the updates. This is typically <Office installer folder>\Updates.
.EXAMPLE
All parameters pre-populated
powershell.exe -executionpolicy bypass -file OfficeUpdater.ps1
All parameters pre-populated without email send (-command must be used when populating boolean values at the command line)
powershell.exe -executionpolicy bypass -command OfficeUpdater.ps1 -EmailLogs $false
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.122
Created on: 6/15/2016 10:29 AM
Created by: Mick Pletcher
Filename: OfficeUpdater.ps1
===========================================================================
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()][string]$Country = 'en-us',
[ValidateNotNullOrEmpty()][boolean]$EmailLogs = $true,
[string]$EmailRecipients,
[string]$EmailSender,
[ValidateNotNullOrEmpty()][string]$ExclusionFileName = 'ExclusionList.txt',
[ValidateNotNullOrEmpty()][string]$LogFileName = 'MicrosoftOfficeUpdates.log',
[ValidateNotNullOrEmpty()][string]$ProcessedUpdatesFile = 'ProcessedUpdatesList.txt',
[string]$SMTPServer,
[ValidateNotNullOrEmpty()][string]$SourceFolder,
[ValidateNotNullOrEmpty()][string]$UpdatesFolder
)
function Copy-Updates {
<#
.SYNOPSIS
Copy the Office updates to the updates folder
.DESCRIPTION
This will extract the .msp file from the .cab and then copy it to the Microsoft Office installation updates folder.
.PARAMETER UnprocessedUpdates
Updates that SCCM has downloaded but have not been copied to the updates folder.
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()][object]$UnprocessedUpdates
)
$RelativePath = Get-RelativePath
$LogFile = $RelativePath + $LogFileName
$ExclusionList = Get-ExclusionList
foreach ($Update in $UnprocessedUpdates) {
$ExtractedFolder = $SourceFolder + '\' + $Update.Name + '\extracted'
If ((Test-Path $ExtractedFolder) -eq $true) {
$Files = Get-ChildItem -Path $ExtractedFolder
foreach ($File in $Files) {
if ($File.Extension -eq '.msp') {
[string]$KBUpdate = Get-MSPFileInfo -Path $File.Fullname -Property 'KBArticle Number'
$KBUpdate = 'KB' + $KBUpdate.Trim()
$KBUpdateShortName = $KBUpdate + '.msp'
$KBUpdateFullName = $File.DirectoryName + '\' + $KBUpdateShortName
$DestinationFile = $UpdatesFolder + '\' + $KBUpdateShortName
If ($KBUpdate -notin $ExclusionList) {
Write-Host "Renaming"$File.Name"to"$KBUpdateShortName"....." -NoNewline
$NoOutput = Copy-Item -Path $File.Fullname -Destination $KBUpdateFullName -Force
If ((Test-Path $KBUpdateFullName) -eq $true) {
Write-Host "Success" -ForegroundColor Yellow
} else {
Write-Host "Failed" -ForegroundColor Red
}
Write-Host "Copying"$KBUpdateShortName" to Office updates folder....." -NoNewline
$NoOutput = Copy-Item -Path $KBUpdateFullName -Destination $UpdatesFolder -Force
If ((Test-Path $DestinationFile) -eq $true) {
Write-Host "Success" -ForegroundColor Yellow
Add-Content -Path $LogFile -Value $KBUpdate -Force
} else {
Write-Host "Failed" -ForegroundColor Red
}
} else {
Write-Host $KBUpdate"....." -NoNewline
Write-Host "Excluded" -ForegroundColor Green
}
}
}
}
Start-Sleep -Seconds 1
}
}
function Expand-CABFiles {
<#
.SYNOPSIS
Extract the contents of the .CAB file
.DESCRIPTION
This will extract the contents of the CAB file to a temporary subfolder for all unprocessed updates. It filters the updates to expand by the country code specified in the Country parameter. It also expands all cab with none in the name.
.PARAMETER UnprocessedUpdates
Updates that SCCM has downloaded but have not been copied to the updates folder.
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()][object]$UnprocessedUpdates
)
$Executable = $env:windir + "\System32\expand.exe"
$Country = '*' + $Country + '*'
foreach ($Update in $UnprocessedUpdates) {
$Folder = $SourceFolder + '\' + $Update.Name
$Files = Get-ChildItem -Path $Folder
foreach ($File in $Files) {
If (($File.Name -like $Country) -or ($File.Name -like "*none*")) {
$ExtractedDirectory = $File.DirectoryName + '\extracted'
If ((Test-Path $ExtractedDirectory) -eq $true) {
$NoOutput = Remove-Item $ExtractedDirectory -Recurse -Force
}
$NoOutput = New-Item $ExtractedDirectory -ItemType Directory
Write-Host "Extracting"$File.Name"....." -NoNewline
$Parameters = [char]34 + $File.FullName + [char]34 + [char]32 + [char]34 + $ExtractedDirectory + [char]34 + [char]32 + "-f:*"
$ErrCode = (Start-Process -FilePath $Executable -ArgumentList $Parameters -Wait -WindowStyle Minimized -Passthru).ExitCode
If ($ErrCode -eq 0) {
Write-Host "Success" -ForegroundColor Yellow
} else {
Write-Host "Failed" -ForegroundColor Red
}
}
}
}
}
function Get-ExclusionList {
<#
.SYNOPSIS
Office Updates Exclusion List
.DESCRIPTION
Contains a list of Microsoft Office updates to exclude from installation
.NOTES
Additional information about the function.
#>
[CmdletBinding()][OutputType([object])]
param ()
$RelativePath = Get-RelativePath
$ExclusionFile = $RelativePath + $ExclusionFileName
If ((Test-Path $ExclusionFile) -eq $true) {
$ExclusionList = Get-Content $ExclusionFile -Force
} else {
$NoOutput = New-Item -Path $ExclusionFile -Force
$ExclusionList = Get-Content $ExclusionFile -Force
}
Return $ExclusionList
}
function Get-ExtractedUpdatesList {
<#
.SYNOPSIS
List of updates already extracted
.DESCRIPTION
This function retrieves the list of updates that have already been extracted to the updates folder.
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param ()
$RelativePath = Get-RelativePath
$ExtractedUpdatesFile = $RelativePath + $ProcessedUpdatesFile
If ((Test-Path $ExtractedUpdatesFile) -eq $true) {
$File = Get-Content -Path $ExtractedUpdatesFile -Force
Return $File
} else {
$NoOutput = New-Item -Path $ExtractedUpdatesFile -ItemType File -Force
Return $null
}
}
function Get-MSPFileInfo {
<#
.SYNOPSIS
Extract MSP information
.DESCRIPTION
This function will extract MSP file information from the metadata table. It has been written to be able to read data from a lot of different MSP files, including Microsoft Office updates and most application patches. There are some MSP files that were not populated with the metadata table, therefor no data is obtainable.
.PARAMETER Path
Location of the MSP file
.PARAMETER Property
A pre-defined set of properties in the msi metadata table
.NOTES
Additional information about the function.
#>
param
(
[Parameter(Mandatory = $true)][IO.FileInfo]$Path,
[Parameter(Mandatory = $true)][ValidateSet('Classification', 'Description', 'DisplayName', 'KBArticle Number', 'ManufacturerName', 'ReleaseVersion', 'TargetProductName')][string]$Property
)
try {
$WindowsInstaller = New-Object -ComObject WindowsInstaller.Installer
$MSIDatabase = $WindowsInstaller.GetType().InvokeMember("OpenDatabase", "InvokeMethod", $Null, $WindowsInstaller, @($Path.FullName, 32))
$Query = "SELECT Value FROM MsiPatchMetadata WHERE Property = '$($Property)'"
$View = $MSIDatabase.GetType().InvokeMember("OpenView", "InvokeMethod", $null, $MSIDatabase, ($Query))
$View.GetType().InvokeMember("Execute", "InvokeMethod", $null, $View, $null)
$Record = $View.GetType().InvokeMember("Fetch", "InvokeMethod", $null, $View, $null)
$Value = $Record.GetType().InvokeMember("StringData", "GetProperty", $null, $Record, 1)
return $Value
} catch {
Write-Output $_.Exception.Message
}
}
function Get-NewUpdatesList {
<#
.SYNOPSIS
List new updates to install
.DESCRIPTION
Get the list of updates to process
.NOTES
Additional information about the function.
#>
[CmdletBinding()][OutputType([object])]
param ()
$UnprocessedFolders = @()
$ExtractedUpdatesList = Get-ExtractedUpdatesList
$List = Get-ChildItem $SourceFolder
foreach ($Update in $List) {
If ($Update.Name -notin $ExtractedUpdatesList ) {
$UnprocessedFolders = $UnprocessedFolders + $Update
}
}
Return $UnprocessedFolders
}
function Get-RelativePath {
<#
.SYNOPSIS
Get the relative path
.DESCRIPTION
Returns the location of the currently running PowerShell script
.NOTES
Additional information about the function.
#>
[CmdletBinding()][OutputType([string])]
param ()
$Path = (split-path $SCRIPT:MyInvocation.MyCommand.Path -parent) + "\"
Return $Path
}
function New-LogFile {
<#
.SYNOPSIS
Generate a log file
.DESCRIPTION
Generate a log file containing a list of the KB updates copied into the updates folder
.NOTES
Additional information about the function.
#>
[CmdletBinding()][OutputType([boolean])]
param ()
$RelativePath = Get-RelativePath
$LogFile = $RelativePath + $LogFileName
If ((Test-path $LogFile) -eq $true) {
Write-Host 'Deleting old log file.....' -NoNewline
$NoOutput = Remove-Item $LogFile -Force
If ((Test-path $LogFile) -eq $false) {
Write-Host "Success" -ForegroundColor Yellow
} else {
Write-Host "Failed" -ForegroundColor Red
$Success = $false
}
}
If ((Test-path $LogFile) -eq $false) {
Write-Host "Creating new log file....." -NoNewline
$NoOutput = New-Item $LogFile -Force
If ((Test-path $LogFile) -eq $true) {
Write-Host "Success" -ForegroundColor Yellow
$Success = $true
} else {
Write-Host "Failed" -ForegroundColor Red
$Success = $false
}
}
Return $Success
}
function Remove-ExtractionFolders {
<#
.SYNOPSIS
Delete the extraction folders
.DESCRIPTION
Delete the extracted folders and contents of the unprocessed updates.
.PARAMETER UnprocessedUpdates
Updates that SCCM has downloaded but have not been copied to the updates folder.
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()][object]$UnprocessedUpdates
)
foreach ($Update in $UnprocessedUpdates) {
$ExtractedFolder = $SourceFolder + '\' + $Update.Name + '\extracted'
$Deleted = $false
$Counter = 1
If ((Test-Path $ExtractedFolder) -eq $true) {
Do {
Try {
Write-Host "Removing"$ExtractedFolder"....." -NoNewline
$NoOutput = Remove-Item $ExtractedFolder -Recurse -Force -ErrorAction Stop
If ((Test-Path $ExtractedFolder) -eq $false) {
Write-Host "Success" -ForegroundColor Yellow
$Deleted = $true
} else {
Write-Host "Failed" -ForegroundColor Red
$Deleted = $false
}
} Catch {
$Counter++
Write-Host 'Failed. Retrying in 5 seconds' -ForegroundColor Red
Start-Sleep -Seconds 5
If ((Test-Path $ExtractedFolder) -eq $true) {
$Deleted = $false
Write-Host "Removing"$ExtractedFolder"....." -NoNewline
$NoOutput = Remove-Item $ExtractedFolder -Recurse -Force -ErrorAction SilentlyContinue
If ((Test-Path $ExtractedFolder) -eq $false) {
Write-Host "Success" -ForegroundColor Yellow
$Deleted = $true
} else {
Write-Host "Failed" -ForegroundColor Red
$Deleted = $false
}
}
If ($Counter = 5) {
$Deleted = $true
}
}
} while ($Deleted = $false)
Start-Sleep -Seconds 1
}
}
}
function Send-UpdateReport {
<#
.SYNOPSIS
Send a report of new updates applied
.DESCRIPTION
Send the log file with a list of Office updates that were copied over to the Office updates folder.
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param ()
$RelativePath = Get-RelativePath
$LogFile = $RelativePath + $LogFileName
$Date = Get-Date -Format "dd-MMMM-yyyy"
$Subject = 'Microsoft Office Updates Report as of ' + $Date
$Body = 'List of Microsoft Office Updates added to the Office installation updates folder.'
$Count = 1
Do {
Try {
Write-Host "Emailing report....." -NoNewline
Send-MailMessage -To $EmailRecipients -From $EmailSender -Subject $Subject -Body $Body -Attachments $LogFile -SmtpServer $SMTPServer
Write-Host "Success" -ForegroundColor Yellow
$Exit = $true
} Catch {
$Count++
If ($Count -lt 4) {
Write-Host 'Failed to send message. Retrying.....' -ForegroundColor Red
} else {
Write-Host 'Failed to send message' -ForegroundColor Red
$Exit = $true
}
}
} Until ($Exit = $true)
}
function Update-ProcessedUpdatesFile {
<#
.SYNOPSIS
Update the Processed Updates File with new updates
.DESCRIPTION
Add updates that were copied over to the processed updates file so they do not get processed again.
.PARAMETER UnprocessedUpdates
Updates that SCCM has downloaded but have not been copied to the updates folder.
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
param
(
[ValidateNotNullOrEmpty()]$UnprocessedUpdates
)
$RelativePath = Get-RelativePath
$LogFile = $RelativePath + $ProcessedUpdatesFile
foreach ($Update in $UnprocessedUpdates) {
$Success = $false
Do {
Try {
Write-Host 'Adding'$Update.Name'to Processed updates.....' -NoNewline
Add-Content -Path $LogFile -Value $Update.Name -Force -ErrorAction Stop
$Success = $true
Write-Host "Success" -ForegroundColor Yellow
} Catch {
Write-Host "Failed" -ForegroundColor Red
$Success = $false
}
} while ($Success -eq $false)
}
}
Clear-Host
$UnprocessedFolders = Get-NewUpdatesList
If ($UnprocessedFolders -ne $null) {
$NewLog = New-LogFile
If ($NewLog -eq $true) {
Expand-CABFiles -UnprocessedUpdates $UnprocessedFolders
Copy-Updates -UnprocessedUpdates $UnprocessedFolders
Remove-ExtractionFolders -UnprocessedUpdates $UnprocessedFolders
Update-ProcessedUpdatesFile -UnprocessedUpdates $UnprocessedFolders
If ($EmailLogs -eq $true) {
Send-UpdateReport
}
} else {
Write-Host "Failed to create log file."
}
} else {
Write-Host "No updates to process"
}