Public/Functions/Catalogs/Update/Update-LenovoDriverPackCatalog.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
<#
.SYNOPSIS
Builds the Lenovo DriverPack Catalog
 
.DESCRIPTION
Builds the Lenovo DriverPack Catalog
 
.LINK
https://github.com/OSDeploy/OSD/tree/master/Docs
 
.NOTES
#>

function Update-LenovoDriverPackCatalog {
    [CmdletBinding()]
    param (
        #Updates the OSD Module Offline Catalog
        [System.Management.Automation.SwitchParameter]
        $UpdateModule
    )
    #=================================================
    # Defaults
    #=================================================
    $OnlineCatalogName = 'catalogv2.xml'
    $OnlineCatalogUri = 'https://download.lenovo.com/cdrt/td/catalogv2.xml'

    $OfflineCatalogName = 'LenovoDriverPackCatalog.xml'

    $ModuleCatalogXml      = "$($MyInvocation.MyCommand.Module.ModuleBase)\Catalogs\LenovoDriverPackCatalog.xml"
    $ModuleCatalogJson      = "$($MyInvocation.MyCommand.Module.ModuleBase)\Catalogs\LenovoDriverPackCatalog.json"

    $UTF8ByteOrderMark      = [System.Text.Encoding]::UTF8.GetString(@(195, 175, 194, 187, 194, 191))
    #=================================================
    # Additional Paths
    #=================================================
    $CatalogBuildFolder = Join-Path $env:TEMP 'OSD'
    if (-not(Test-Path $CatalogBuildFolder)) {
        $null = New-Item -Path $CatalogBuildFolder -ItemType Directory -Force
    }
    $RawCatalogFile            = Join-Path $env:TEMP (Join-Path 'OSD' $OnlineCatalogName)
    $RawCatalogCabName      = [string]($OnlineCatalogUri | Split-Path -Leaf)
    $RawCatalogCabPath         = Join-Path $env:TEMP (Join-Path 'OSD' $RawCatalogCabName)
    #=================================================
    # Get Online Catalog
    #=================================================
    try {
        #[xml]$XmlCatalog = $RawDriverPackCatalog -replace "^$UTF8ByteOrderMark"
        $CatalogCloudRaw = Invoke-RestMethod -Uri $OnlineCatalogUri -UseBasicParsing
        Write-Verbose -Verbose "Cloud Catalog $OnlineCatalogUri"
        Write-Verbose -Verbose "Saving Cloud Catalog to $RawCatalogFile"        
        $CatalogCloudContent = $CatalogCloudRaw.Substring(3)
        $CatalogCloudContent | Out-File -FilePath $RawCatalogFile -Encoding utf8 -Force

        if (Test-Path $RawCatalogFile) {
            Write-Verbose -Verbose "Catalog saved to $RawCatalogFile"
            $UseCatalog = 'Raw'
        }
        else {
            Write-Verbose -Verbose "Catalog was NOT downloaded to $RawCatalogFile"
            Write-Warning 'Unable to complete'
            Break
        }
    }
    catch {
        Write-Warning 'Unable to complete'
        Break
    }
    #=================================================
    # Build Catalog
    #=================================================
    Write-Verbose -Verbose "Reading the Raw Catalog at $RawCatalogFile"
    [xml]$XmlCatalogContent = Get-Content -Path $RawCatalogFile -Raw

    $ModelList = $XmlCatalogContent.ModelList.Model
    #=================================================
    # Create Object
    #=================================================
    $Results = foreach ($Model in $ModelList) {
        foreach ($Item in $Model.SCCM) {
            $DownloadUrl = $Item.'#text'
            $ReleaseDate = $null
            
            $OSReleaseId = $Item.version
            if ($OSReleaseId -eq '*') {
                $OSReleaseId = $null
            }

            $OSBuild = $null
            if ($OSReleaseId -eq '22H2') {
                if ($Item.os -eq 'win10') {
                    $OSBuild = '19045'
                }
                if ($Item.os -eq 'win11') {
                    $OSBuild = '22621'
                }
            }
            elseif ($OSReleaseId -eq '21H2') {
                if ($Item.os -eq 'win10') {
                    $OSBuild = '19044'
                }
                if ($Item.os -eq 'win11') {
                    $OSBuild = '22000'
                }
            }
            elseif ($OSReleaseId -eq '21H1') {
                $OSBuild = '19043'
            }
            elseif ($OSReleaseId -eq '20H2') {
                $OSBuild = '19042'
            }
            elseif ($OSReleaseId -eq '2004') {
                $OSBuild = '19041'
            }
            elseif ($OSReleaseId -eq '1909') {
                $OSBuild = '18363'
            }
            elseif ($OSReleaseId -eq '1903') {
                $OSBuild = '18362'
            }
            elseif ($OSReleaseId -eq '1809') {
                $OSBuild = '17763'
            }
            elseif ($OSReleaseId -eq '1803') {
                $OSBuild = '17134'
            }
            elseif ($OSReleaseId -eq '1709') {
                $OSBuild = '16299'
            }
            elseif ($OSReleaseId -eq '1703') {
                $OSBuild = '15063'
            }
            elseif ($OSReleaseId -eq '1607') {
                $OSBuild = '14393'
            }
            elseif ($OSReleaseId -eq '1511') {
                $OSBuild = '10586'
            }
            elseif ($OSReleaseId -eq '1507') {
                $OSBuild = '10240'
            }
            $HashMD5 = $Item.crc

            if ($Item.os -eq 'win10') {
                if ($Item.version -eq '*') {
                    $NewName = "Lenovo $($Model.name) Win10"
                }
                else {
                    $NewName = "Lenovo $($Model.name) Win10 $($Item.version)"
                }
                $ObjectProperties = [Ordered]@{
                    CatalogVersion     = Get-Date -Format yy.MM.dd
                    Status          = $null
                    Component       = 'DriverPack'
                    ReleaseDate     = $ReleaseDate
                    Manufacturer    = 'Lenovo'
                    Model           = $Model.name
                    Product            = [array]$Model.Types.Type.split(',').Trim()
                    Name            = $NewName
                    PackageID       = $null
                    FileName        = $DownloadUrl | Split-Path -Leaf
                    Url             = $DownloadUrl
                    OSVersion       = 'Windows 10 x64'
                    OSReleaseId     = $OSReleaseId
                    OSBuild         = $OSBuild
                    HashMD5         = $HashMD5
                }
                New-Object -TypeName PSObject -Property $ObjectProperties
            }

            if ($Item.os -eq 'win11') {
                if ($Item.version -eq '*') {
                    $NewName = "Lenovo $($Model.name) Win11"
                }
                else {
                    $NewName = "Lenovo $($Model.name) Win11 $($Item.version)"
                }
                $ObjectProperties = [Ordered]@{
                    CatalogVersion     = Get-Date -Format yy.MM.dd
                    Status          = $null
                    Component       = 'DriverPack'
                    ReleaseDate     = $ReleaseDate
                    Manufacturer    = 'Lenovo'
                    Model           = $Model.name
                    Product            = [array]$Model.Types.Type.split(',').Trim()
                    Name            = $NewName
                    PackageID       = $null
                    FileName        = $DownloadUrl | Split-Path -Leaf
                    Url             = $DownloadUrl
                    OSVersion       = 'Windows 11 x64'
                    OSReleaseId     = $OSReleaseId
                    OSBuild         = $OSBuild
                    HashMD5         = $HashMD5
                }
                New-Object -TypeName PSObject -Property $ObjectProperties
            }
        }
    }
    $Results = $Results | Sort-Object Name, OSVersion -Descending | Group-Object Name | ForEach-Object {$_.Group | Select-Object -First 1}
    $Results = $Results | Sort-Object Name, OSVersion -Descending
    #=================================================
    # Validate Results
    #=================================================
    Write-Warning "Testing each download link, please wait ..."
    $Results = $Results | Sort-Object Url
    $LastItem = $null

    foreach ($Item in $Results) {
        if ($Item.Url -eq $LastItem.Url) {
            $Item.Status = $LastItem.Status
            $Item.ReleaseDate = $LastItem.ReleaseDate
        }
        else {
            $Global:DownloadHeaders = $null
            try {
                $Global:DownloadHeaders = (Invoke-WebRequest -Method Head -Uri $Item.Url -UseBasicParsing).Headers
            }
            catch {
                Write-Warning "Failed: $($Item.Url)"
            }

            if ($Global:DownloadHeaders) {
                $Item.ReleaseDate = Get-Date ($Global:DownloadHeaders.'Last-Modified') -Format "yy.MM.dd"
                Write-Verbose -Verbose "Success: $($Item.Url)"
                Write-Verbose -Verbose "ReleaseDate: $($Item.ReleaseDate)"
            }
            else {
                $Item.Status = 'Failed'
            }
        }
        $LastItem = $Item
    }
    #=================================================
    # Sort Results
    #=================================================
    $Results = $Results | Sort-Object Name
    #=================================================
    # UpdateModule
    #=================================================
    if ($UpdateModule) {
        Write-Verbose -Verbose "UpdateModule: Exporting to OSD Module Catalogs at $ModuleCatalogXml"
        $Results | Export-Clixml -Path $ModuleCatalogXml -Force
        Write-Verbose -Verbose "UpdateModule: Exporting to OSD Module Catalogs at $ModuleCatalogJson"
        $Results | ConvertTo-Json | Out-File $ModuleCatalogJson -Encoding ascii -Width 2000 -Force
    }
    #=================================================
    # Complete
    #=================================================
    Write-Verbose -Verbose 'Complete: Results have been stored $Global:LenovoDriverPackCatalog'
    $Global:LenovoDriverPackCatalog = $Results | Sort-Object -Property Name
    #=================================================
}