Public/New-OSDUpdatePackage.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
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
<#
.SYNOPSIS
Creates Package Bundles of Microsoft Updates
 
.DESCRIPTION
Creates Package Bundles of Microsoft Updates
Requires BITS for downloading the updates
Requires Internet access for downloading the updates
 
.LINK
https://osdupdate.osdeploy.com/module/functions/new-osdupdatepackage
 
.PARAMETER PackagePath
Package Content will be downloaded into the PackagePath
 
.PARAMETER PackageName
The name of the OSDUpdate Package. These values are predefined
 
.PARAMETER AppendPackageName
Downloads Updates in the PackagePath in a PackageName subdirectory
 
.PARAMETER GridView
Displays the results in GridView with -PassThru. Updates selected in GridView can be selected
 
.PARAMETER HideDownloaded
Hides downloaded updates from the results
 
.PARAMETER OfficeProfile
Downloads Office Updates with the selected Profile
 
.PARAMETER OfficeSetupUpdatesPath
Updates Directory for Office Setup
 
.PARAMETER RemoveSuperseded
Remove Superseded Updates
 
.PARAMETER SkipInstallScript
Skips adding $PackagePath\Install-OSDUpdatePackage.ps1
 
.PARAMETER SkipUpdateScript
Skips adding $PackagePath\Update-OSDUpdatePackage.ps1
 
#>

function New-OSDUpdatePackage {
    [CmdletBinding()]
    PARAM (
        [Parameter(Mandatory = $True)]
        [string]$PackagePath,

        [Parameter(Mandatory = $True)]
        [ValidateSet(
            #================================
            # Office
            #================================
            'Office 2010 32-Bit',
            'Office 2010 64-Bit',
            'Office 2013 32-Bit',
            'Office 2013 64-Bit',
            'Office 2016 32-Bit',
            'Office 2016 64-Bit',
            #================================
            # Windows
            #================================
            'Windows 7 x64',
            'Windows 7 x86',
            'Windows 10 x64 1803',
            'Windows 10 x64 1809',
            'Windows 10 x64 1903',
            'Windows 10 x64 1909',
            'Windows 10 x64 2004',
            'Windows 10 x64 2009',
            'Windows 10 x64 20H2',
            'Windows 10 x86 1803',
            'Windows 10 x86 1809',
            'Windows 10 x86 1903',
            'Windows 10 x86 1909',
            'Windows 10 x86 2004',
            'Windows 10 x86 2009',
            'Windows 10 x86 20H2',
            'Windows Server 2016 1607',
            'Windows Server 2016 1709',
            'Windows Server 2016 1803',
            'Windows Server 2019 1809',
            'Windows Server SAC 1809',
            'Windows Server SAC 1903',
            'Windows Server SAC 1909',
            'Windows Server SAC 2004',
            'Windows Server SAC 20H2',
            #================================
            # Other
            #================================
            'Servicing Stacks')]
        [string]$PackageName,

        [switch]$AppendPackageName,
        [switch]$GridView,
        [switch]$HideDownloaded,

        [ValidateSet('Default','Proofing','Language','All')]
        [string]$OfficeProfile = 'Default',

        [string]$OfficeSetupUpdatesPath,

        [switch]$RemoveSuperseded,
        [switch]$SkipInstallScript,
        [switch]$SkipUpdateScript

    )
    #===================================================================================================
    # AppendPackageName
    #===================================================================================================
    if ($AppendPackageName) {
        $PackagePath = "$PackagePath\$PackageName"
    }
    #===================================================================================================
    # PackagePath
    #===================================================================================================
    if (!(Test-Path "$PackagePath")) {New-Item -Path "$PackagePath" -ItemType Directory -Force | Out-Null}
    #===================================================================================================
    # Get-OSDUpdate
    #===================================================================================================
    $OSDUpdate = @()
    $OSDUpdate = Get-OSDUpdate
    Write-Host $PackageName -ForegroundColor Green
    #===================================================================================================
    # Filter Catalog
    #===================================================================================================
    if ($PackageName -match 'Office') {
        $OSDUpdate = $OSDUpdate | Where-Object {$_.Catalog -eq $PackageName}
    }

    if ($PackageName -like "Windows*") {
        if ($PackageName -like "Windows 7*") {
            $OSDUpdate = $OSDUpdate | Where-Object {$_.Catalog -eq 'Windows 7'}
        } elseif ($PackageName -like "Windows 10*") {
            $OSDUpdate = $OSDUpdate | Where-Object {$_.Catalog -eq 'Windows 10'}
        } elseif ($PackageName -like "Windows Server 2016*") {
            $OSDUpdate = $OSDUpdate | Where-Object {$_.Catalog -eq 'Windows Server 2016'}
        } elseif ($PackageName -like "Windows Server 2019*") {
            $OSDUpdate = $OSDUpdate | Where-Object {$_.Catalog -eq 'Windows Server 2019'}
        } elseif ($PackageName -match "Windows Server SAC 1809") {
            $OSDUpdate = $OSDUpdate | Where-Object {$_.Catalog -eq 'Windows Server 2019'}
        } elseif ($PackageName -match "Windows Server SAC") {
            $OSDUpdate = $OSDUpdate | Where-Object {$_.Catalog -eq 'Windows Server 1903 and Later'}
        }
    }
    #===================================================================================================
    # AllOSDUpdates
    #===================================================================================================
    $AllOSDUpdates = $OSDUpdate
    #===================================================================================================
    # Superseded Updates
    #===================================================================================================
    $CurrentUpdates = @()
    if ($PackageName -match 'Office') {
        $OSDUpdate = $OSDUpdate | Sort-Object OriginUri -Unique
        $OSDUpdate = $OSDUpdate | Sort-Object CreationDate -Descending

        $SupersededUpdates = @()

        foreach ($OfficeUpdate in $OSDUpdate) {
            $SkipUpdate = $false

            foreach ($CurrentUpdate in $CurrentUpdates) {
                if ($($OfficeUpdate.FileName) -eq $($CurrentUpdate.FileName)) {$SkipUpdate = $true}
            }

            if ($SkipUpdate) {
                $SupersededUpdates += $OfficeUpdate
            } else {
                $CurrentUpdates += $OfficeUpdate
            }
        }
        $OSDUpdate = $CurrentUpdates
    } else {
        $CurrentUpdates = $OSDUpdate
    }
    #===================================================================================================
    # Find Existing Updates
    #===================================================================================================
    $LocalUpdates = @()
    $LocalSuperseded = @()

    $LocalUpdates = Get-ChildItem -Path "$PackagePath\*" -Directory -Recurse | Select-Object -Property *
    $LocalUpdatesMsp = Get-ChildItem -Path "$PackagePath\*" *.msp -File -Recurse | Select-Object -Property * | Sort-Object CreationDate -Descending
    $LocalUpdatesXml = Get-ChildItem -Path "$PackagePath\*" *.xml -File -Recurse | Select-Object -Property * | Sort-Object CreationDate -Descending

    foreach ($Update in $LocalUpdates) {
        if ($CurrentUpdates.Title -NotContains $Update.Name) {$LocalSuperseded += $Update.FullName}
    }
    #===================================================================================================
    # Remove Superseded Update Directories
    #===================================================================================================
    foreach ($Update in $LocalSuperseded) {
        if ($RemoveSuperseded.IsPresent) {
            Write-Warning "Removing Superseded: $Update"
            Remove-Item $Update -Recurse -Force | Out-Null
        } else {
            Write-Warning "Superseded: $Update"
        }
    }
    #===================================================================================================
    # Remove Superseded Update Files
    #===================================================================================================
    if ($PackageName -match 'Office') {
        foreach ($Update in $SupersededUpdates) {
            $SupersededMsp = "$PackagePath\$($Update.Title)\$([IO.Path]::GetFileNameWithoutExtension($Update.FileName)).msp"
            $SupersededXml = "$PackagePath\$($Update.Title)\$([IO.Path]::GetFileNameWithoutExtension($Update.FileName)).xml"
            if (Test-Path "$SupersededMsp") {
                if ($RemoveSuperseded.IsPresent) {
                    Write-Warning "Removing Superseded: $SupersededMsp"
                    Remove-Item $SupersededMsp -Force | Out-Null
                } else {
                    Write-Warning "Superseded: $SupersededMsp"
                }
            }
            if (Test-Path "$SupersededXml") {
                if ($RemoveSuperseded.IsPresent) {
                    Write-Warning "Removing Superseded: $SupersededXml"
                    Remove-Item $SupersededXml -Force | Out-Null
                } else {
                    Write-Warning "Superseded: $SupersededXml"
                }
            }
        }
    }
    #===================================================================================================
    # Get Downloaded Updates
    #===================================================================================================
    foreach ($Update in $OSDUpdate) {
        if ($PackageName -like "Windows*" -or $PackageName -eq 'Servicing Stacks') {
            $FullUpdatePath = "$PackagePath\$($Update.Title)\$($Update.FileName)"
            if (Test-Path $FullUpdatePath) {
                $Update.OSDStatus = 'Downloaded'
            }
        }
        if ($PackageName -match 'Office') {
            $FullUpdatePath = "$PackagePath\$($Update.Title)\$([IO.Path]::GetFileNameWithoutExtension($Update.FileName)).msp"
            if (Test-Path $FullUpdatePath) {
                $Update.OSDStatus = 'Downloaded'
            }
        }
    }
    #===================================================================================================
    # Filter Other
    #===================================================================================================
    if ($PackageName -eq 'Servicing Stacks') {
        $OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateGroup -eq 'SSU'}
    }

    if ($PackageName -like "Windows*") {
        if ($PackageName -like "*x64*") {$OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateArch -eq 'x64'}}
        if ($PackageName -like "*x86*") {$OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateArch -eq 'x86'}}
        if ($PackageName -like "*1507*") {$OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateBuild -eq '1507'}}
        if ($PackageName -like "*1511*") {$OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateBuild -eq '1511'}}
        if ($PackageName -like "*1607*") {$OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateBuild -eq '1607'}}
        if ($PackageName -like "*1703*") {$OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateBuild -eq '1703'}}
        if ($PackageName -like "*1709*") {$OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateBuild -eq '1709'}}
        if ($PackageName -like "*1803*") {$OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateBuild -eq '1803'}}
        if ($PackageName -like "*1809*") {$OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateBuild -eq '1809'}}
        if ($PackageName -like "*1903*") {$OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateBuild -eq '1903'}}
        if ($PackageName -like "*1909*") {$OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateBuild -eq '1909'}}
        if ($PackageName -like "*2004*") {$OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateBuild -eq '2004'}}
        if ($PackageName -like "*2009*") {$OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateBuild -eq '2009'}}
        if ($PackageName -like "*20H2*") {$OSDUpdate = $OSDUpdate | Where-Object {$_.UpdateBuild -eq '2009'}}
    }
    $OSDUpdate | Export-Clixml -Path "$PackagePath\OSDUpdatePackage.xml" -Force | Out-Null

    if ($PackageName -like "Office*") {
        if ($OfficeProfile -eq 'Default') {
            $OSDUpdate = $OSDUpdate | Where-Object {$_.FileName -like "*none*" -or $_.FileName -like "*en-us*"}
            $OSDUpdate = $OSDUpdate | Where-Object {$_.Title -notlike "*Language Pack*"}
        }
        if ($OfficeProfile -eq 'Language') {
            $OSDUpdate = $OSDUpdate | Where-Object {$_.FileName -notlike "*none*" -and $_.FileName -notlike "*en-us*"}
        }
        if ($OfficeProfile -eq 'Proofing') {
            $OSDUpdate = $OSDUpdate | Where-Object {$_.FileName -like "*Proof*"}
        }
    }
    #===================================================================================================
    # HideDownloaded
    #===================================================================================================
    if ($HideDownloaded.IsPresent) {
        $OSDUpdate = $OSDUpdate | Where-Object {$_.OSDStatus -ne 'Downloaded'}
    }
    #===================================================================================================
    # GridView
    #===================================================================================================
    if ($PackageName -like "Office*") {
        $OSDUpdate = $OSDUpdate | Select-Object -Property OSDStatus,Catalog,CreationDate,KBNumber,Title,FileName,Size,FileUri,OriginUri,OSDGuid
    } else {
        $OSDUpdate = $OSDUpdate | Select-Object -Property OSDStatus,Catalog,UpdateOS,UpdateArch,UpdateBuild,CreationDate,KBNumber,Title,FileName,Size,FileUri,OriginUri,OSDGuid
    }
    if ($GridView.IsPresent) {$OSDUpdate = $OSDUpdate | Out-GridView -PassThru -Title "Select OSDUpdate Downloads to include in the Package"}
    #===================================================================================================
    # Sort
    #===================================================================================================
    $OSDUpdate = $OSDUpdate | Sort-Object DateCreated
    #===================================================================================================
    # Office Download
    #===================================================================================================
    if ($PackageName -like "Office*") {
        foreach ($Update in $OSDUpdate) {
            $UpdateFile = $($Update.FileName)
            $MspFile = $UpdateFile -replace '.cab', '.msp'
            $DownloadDirectory = "$PackagePath\$($Update.Title)"

            if (!(Test-Path "$DownloadDirectory")) {New-Item -Path "$DownloadDirectory" -ItemType Directory -Force | Out-Null}
        
            if (Test-Path "$DownloadDirectory\$MspFile") {
                Write-Host "$DownloadDirectory\$MspFile" -ForegroundColor Cyan
            } else {
                Write-Host "$DownloadDirectory\$MspFile" -ForegroundColor Cyan
                Write-Host "Download: $($Update.OriginUri)" -ForegroundColor Gray
                Start-BitsTransfer -Source $($Update.OriginUri) -Destination "$DownloadDirectory\$UpdateFile"
            }

            if ((Test-Path "$DownloadDirectory\$UpdateFile") -and (!(Test-Path "$DownloadDirectory\$MspFile"))) {
                Write-Host "Expand: $DownloadDirectory\$MspFile" -ForegroundColor Gray
                expand "$DownloadDirectory\$UpdateFile" -F:* "$DownloadDirectory" | Out-Null
            }

            if ((Test-Path "$DownloadDirectory\$UpdateFile") -and (Test-Path "$DownloadDirectory\$MspFile")) {
                Write-Host "Remove: $DownloadDirectory\$UpdateFile" -ForegroundColor Gray
                Remove-Item "$DownloadDirectory\$UpdateFile" -Force | Out-Null
            }
            #===================================================================================================
            # Office Setup Updates
            #===================================================================================================
            if ($OfficeSetupUpdatesPath) {
                if (!(Test-Path "$OfficeSetupUpdatesPath")) {New-Item -Path "$OfficeSetupUpdatesPath" -ItemType Directory -Force | Out-Null}
                Write-Host "Date Created: $($Update.CreationDate)" -ForegroundColor Gray
                Write-Host "Source: $DownloadDirectory\$MspFile" -ForegroundColor Gray
                Write-Host "Destination: $OfficeSetupUpdatesPath\$MspFile" -ForegroundColor Gray
                Copy-Item -Path "$DownloadDirectory\$MspFile" "$OfficeSetupUpdatesPath\$MspFile" -Force
            }
        }
    } else {
        foreach ($Update in $OSDUpdate) {
            $UpdateFile = $($Update.FileName)
            $DownloadDirectory = "$PackagePath\$($Update.Title)"

            if (!(Test-Path "$DownloadDirectory")) {New-Item -Path "$DownloadDirectory" -ItemType Directory -Force | Out-Null}
        
            if (Test-Path "$DownloadDirectory\$UpdateFile") {
                Write-Host "$($Update.Title)" -ForegroundColor Cyan
                Write-Host "$DownloadDirectory\$UpdateFile" -ForegroundColor Gray
                #Write-Host "Update already downloaded" -ForegroundColor Gray
            } else {
                Write-Host "$($Update.Title)" -ForegroundColor Cyan
                Write-Host "$($Update.OriginUri)" -ForegroundColor Gray
                Write-Host "$DownloadDirectory\$UpdateFile" -ForegroundColor Gray
                Start-BitsTransfer -Source $($Update.OriginUri) -Destination "$DownloadDirectory\$UpdateFile"
            }
        }
    }
    #===================================================================================================
    # Install Script
    #===================================================================================================
    if (!($SkipInstallScript)) {
        Write-Host "Install Script: $PackagePath\Install-OSDUpdatePackage.ps1" -ForegroundColor Green
        Copy-Item "$($MyInvocation.MyCommand.Module.ModuleBase)\Scripts\Install-OSDUpdatePackage.ps1" "$PackagePath" -Force | Out-Null
    }
    #===================================================================================================
    # Update Script
    #===================================================================================================
    if (!($SkipUpdateScript)) {
        Write-Host "Update Script: $PackagePath\Update-OSDUpdatePackage.ps1" -ForegroundColor Green
        $ExportLine = "New-OSDUpdatePackage -PackageName '$PackageName' -PackagePath ""`$PSScriptRoot"" -RemoveSuperseded"
        $ExportLine | Out-File -FilePath "$PackagePath\Update-OSDUpdatePackage.ps1"
    }
}