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

function Update-HPDriverPackCatalog {
    [CmdletBinding()]
    param (
        #Updates the OSD Module Offline Catalog
        [System.Management.Automation.SwitchParameter]
        $UpdateModule
    )
    #=================================================
    # Custom Defaaults
    #=================================================
    $OnlineCatalogName = 'HPClientDriverPackCatalog.xml'
    $OnlineCatalogUri = 'http://ftp.hp.com/pub/caps-softpaq/cmit/HPClientDriverPackCatalog.cab'

    $OfflineCatalogName = 'HPDriverPackCatalog.xml'

    $ModuleCatalogXml = "$($MyInvocation.MyCommand.Module.ModuleBase)\Catalogs\HPDriverPackCatalog.xml"
    $ModuleCatalogJson = "$($MyInvocation.MyCommand.Module.ModuleBase)\Catalogs\HPDriverPackCatalog.json"
    #=================================================
    # Additional Defaults
    #=================================================
    $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 Cloud
    #=================================================
    Write-Verbose "Source: $OnlineCatalogUri"
    Write-Verbose "Destination: $RawCatalogCabPath"
    (New-Object System.Net.WebClient).DownloadFile($OnlineCatalogUri, $RawCatalogCabPath)

    if (Test-Path $RawCatalogCabPath) {
        Write-Verbose "Expand: $RawCatalogCabPath"
        $null = Expand "$RawCatalogCabPath" "$RawCatalogFile"

        if (Test-Path $RawCatalogFile) {
            Write-Verbose "Using Raw Catalog at $RawCatalogFile"
        }
        else {
            Write-Verbose "Could not expand $RawCatalogCabPath"
            Write-Warning 'Unable to complete'
            Break
        }
    }
    else {
        Write-Warning 'Unable to complete'
        Break
    }
    #=================================================
    # Read Catalog
    #=================================================
    Write-Verbose "Reading the Raw Catalog at $RawCatalogFile"
    Write-Warning "Building Catalog content, please wait ..."
    [xml]$XmlCatalogContent = Get-Content $RawCatalogFile -ErrorAction Stop

    $HpSoftPaqList = $XmlCatalogContent.NewDataSet.HPClientDriverPackCatalog.SoftPaqList.SoftPaq

    $HpModelList = $XmlCatalogContent.NewDataSet.HPClientDriverPackCatalog.ProductOSDriverPackList.ProductOSDriverPack
    $HpModelList = $HpModelList | Where-Object {$_.OSId -ge '4243'}
    $HpModelList = $HpModelList | Sort-Object OSId -Descending | Group-Object ProductId, SoftPaqId | ForEach-Object {$_.Group | Select-Object -First 1}
    $HpModelList = $HpModelList | Sort-Object OSId -Descending | Group-Object ProductId | ForEach-Object {$_.Group | Select-Object -First 1}
    #=================================================
    # Create DriverPack Object
    #=================================================
    $Results = foreach ($Item in $HpModelList) {
        $HpSoftPaq = $null
        $HpSoftPaq = $HpSoftPaqList | Where-Object {$_.Id -eq $Item.SoftPaqId}

        if ($null -eq $HpSoftPaq) {
            Continue
        }

        $Name = $($HpSoftPaq.Name)
        $Name = ($Name).Replace(' x64','')
        $Name = ($Name).Replace('Win 10','Win10')
        $Name = ($Name).Replace('Win 11','Win11')
        $Name = ($Name).Replace('Windows 10','Win10')
        $Name = ($Name).Replace('Windows 11','Win11')
        $Name = ($Name).Replace(' Driver Pack','')
        $Name = ($Name).Replace('/',' ')
        $Name = ($Name).Replace('-',' ')
        $Name = "$Name $($Item.SoftPaqId)"
        #$Name = ($Name).Replace(' A 1','')

        $UniqueName = "$($Item.SystemName) $($Item.OSName) $($($Item.SoftPaqId))"
        $UniqueName = $UniqueName.Replace('Windows 10 64-bit,', 'Win10')
        $UniqueName = $UniqueName.Replace('Windows 11 64-bit,', 'Win11')

        $template = "M/d/yyyy hh:mm:ss tt"
        $timeinfo = $HpSoftPaq.DateReleased
        $dtReleaseDate = [datetime]::ParseExact($timeinfo, $template, $null)


        $ObjectProperties = [Ordered]@{
            CatalogVersion     = Get-Date -Format yy.MM.dd
            Status          = $null
            Component       = 'DriverPack'
            ReleaseDate     = $dtReleaseDate.ToString("yy.MM.dd")
            Manufacturer    = 'HP'
            Name            = $UniqueName
            Model           = $Item.SystemName
            SystemId        = [array]$Item.SystemId.split(',').Trim()
            SoftPaqId       = $Item.SoftPaqId
            OSId            = $Item.OSId
            OSName          = $Item.OSName
            OSVersion       = ''
            OSReleaseId     = ''
            OSBuild         = ''
            Architecture    = $Item.Architecture
            ProductType     = $Item.ProductType
            SoftPaqName     = $HpSoftPaq.Name
            Version         = $HpSoftPaq.Version
            Category        = $HpSoftPaq.Category
            Url             = $HpSoftPaq.Url
            FileName        = $HpSoftPaq.Url | Split-Path -Leaf
            Size            = $HpSoftPaq.Size
            MD5             = $HpSoftPaq.MD5
            SHA256          = $HpSoftPaq.SHA256
            CvaFileUrl      = $HpSoftPaq.CvaFileUrl
            ReleaseNotesUrl = $HpSoftPaq.ReleaseNotesUrl
            CvaTitle        = $HpSoftPaq.CvaTitle
        }
        New-Object -TypeName PSObject -Property $ObjectProperties
    }
    #=================================================
    # Normalize Results
    #=================================================
    foreach ($Item in $Results) {
        $Item.Model = $Item.Model -replace 'HP ', ''
        if ($Item.OSName -match 'Windows 10') {
            $Item.OSVersion = 'Windows 10 x64'
        }
        if ($Item.OSName -match 'Windows 11') {
            $Item.OSVersion = 'Windows 11 x64'
        }
        if ($Item.OSId -match '4261') {
            $Item.OSVersion = 'Windows 10 x64'
        }
    }

    foreach ($Item in $Results) {
        if ($Item.OSName -match '22H2') {
            if ($Item.OSName -match 'Windows 10') {
                $Item.OSReleaseId = '22H2'
                $Item.OSBuild = '19045'
            }
            if ($Item.OSName -match 'Windows 11') {
                $Item.OSReleaseId = '22H2'
                $Item.OSBuild = '22621'
            }
        }
        elseif ($Item.OSName -match '21H2') {
            if ($Item.OSName -match 'Windows 10') {
                $Item.OSReleaseId = '21H2'
                $Item.OSBuild = '19044'
            }
            if ($Item.OSName -match 'Windows 11') {
                $Item.OSReleaseId = '21H2'
                $Item.OSBuild = '22000'
            }
        }
        elseif ($Item.OSName -match '21H1') {
            $Item.OSReleaseId = '21H1'
            $Item.OSBuild = '19043'
        }
        elseif ($Item.OSName -match '20H2') {
            $Item.OSReleaseId = '20H2'
            $Item.OSBuild = '19042'
        }
        elseif ($Item.OSName -match '2004') {
            $Item.OSReleaseId = '2004'
            $Item.OSBuild = '19041'
        }
        elseif ($Item.OSName -match '1909') {
            $Item.OSReleaseId = '1909'
            $Item.OSBuild = '18363'
        }
        elseif ($Item.OSName -match '1903') {
            $Item.OSReleaseId = '1903'
            $Item.OSBuild = '18362'
        }
        elseif ($Item.OSName -match '1809') {
            $Item.OSReleaseId = '1809'
            $Item.OSBuild = '17763'
        }
        elseif ($Item.OSName -match '1803') {
            $Item.OSReleaseId = '1803'
            $Item.OSBuild = '17134'
        }
        elseif ($Item.OSName -match '1709') {
            $Item.OSReleaseId = '1709'
            $Item.OSBuild = '16299'
        }
        elseif ($Item.OSName -match '1703') {
            $Item.OSReleaseId = '1703'
            $Item.OSBuild = '15063'
        }
        elseif ($Item.OSName -match '1607') {
            $Item.OSReleaseId = '1607'
            $Item.OSBuild = '14393'
        }
        elseif ($Item.OSName -match '1511') {
            $Item.OSReleaseId = '1511'
            $Item.OSBuild = '10586'
        }
        elseif ($Item.OSName -match '1507') {
            $Item.OSReleaseId = '1507'
            $Item.OSBuild = '10240'
        }
    }
    #=================================================
    # Validate Results
    #=================================================
    $Results = $Results | Sort-Object Url
    $PreviousUrl = $null
    foreach ($Item in $Results) {
        $CurrentUrl = $Item.Url
        if ($CurrentUrl -ne $PreviousUrl) {
            Write-Verbose -Verbose "Testing Download File at $CurrentUrl"
            try {
                $DownloadHeaders = (Invoke-WebRequest -Method Head -Uri $CurrentUrl -UseBasicParsing).Headers
            }
            catch {
                Write-Warning "Failed: $CurrentUrl"
                $Item.Status = 'Failed'
            }
        }
        $PreviousUrl = $CurrentUrl
    }
    #=================================================
    # Sort Results
    #=================================================
    $Results = $Results | Sort-Object -Property 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:HPDriverPackCatalog'
    $Global:HPDriverPackCatalog = $Results | Sort-Object -Property Name
    #=================================================
}