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

function Update-DellDriverPackCatalog {
    [CmdletBinding()]
    param (
        #Updates the OSD Module Offline Catalog
        [System.Management.Automation.SwitchParameter]
        $UpdateModule
    )
    #=================================================
    # Custom Defaaults
    #=================================================
    $OnlineCatalogName = 'DriverPackCatalog.xml'
    $OnlineBaseUri = 'http://downloads.dell.com/'
    $OnlineCatalogUri = 'https://downloads.dell.com/catalog/DriverPackCatalog.cab'

    $OfflineCatalogName = 'DellDriverPackCatalog.xml'

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

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

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

    #$CatalogVersion = (Get-Date $XmlCatalogContent.DriverPackManifest.version).ToString('yy.MM.dd')
    $RawCatalogVersion = $XmlCatalogContent.DriverPackManifest.version -replace '.00','.01'
    $CatalogVersion = (Get-Date $RawCatalogVersion).ToString('yy.MM.dd')

    $DellDriverPackXml = $XmlCatalogContent.DriverPackManifest.DriverPackage
    $DellDriverPackXml = $DellDriverPackXml | Where-Object {($_.SupportedOperatingSystems.OperatingSystem.osCode.Trim() | Select-Object -Unique) -notmatch 'winpe'}

    #=================================================
    # Create DriverPack Object
    #=================================================
    $Results = foreach ($Item in $DellDriverPackXml) {
        $osCode = $Item.SupportedOperatingSystems.OperatingSystem.osCode.Trim() | Select-Object -Unique
        if ($osCode -match 'Windows11') {
            $osShortName = 'Win11'
        }
        elseif ($osCode -match 'Windows10') {
            $osShortName = 'Win10'
        }
        elseif ($osCode -match 'Windows7') {
            $osShortName = 'Win7'
            Continue
        }
        elseif ($ModelID -eq '0630') {
            Continue
        }
        else {
            Continue
        }

        $Name = "Dell $($Item.SupportedSystems.Brand.Model.name | Select-Object -Unique) $osShortName $($Item.dellVersion)"
        $Name = $Name -replace ' ',' '
        $Model = ($Item.SupportedSystems.Brand.Model.name | Select-Object -Unique)
        if ($Model -match '3650 Tower') {
            $Model = 'Precision 3650 Tower'
        }

        $ModelID = ($Item.SupportedSystems.Brand.Model.Display.'#cdata-section'.Trim() | Select-Object -Unique)
        if ($Model -eq 'Precision 3650 Tower') {
            $ModelID = 'Precision 3650 Tower'
        }

        $Generation = $Item.SupportedSystems.Brand.Model.generation | Select-Object -Unique
        if ($Generation -notmatch 'X') {
            $Generation = 'XX'
        }

        $ObjectProperties = [Ordered]@{
            CatalogVersion         = $CatalogVersion
            Status                = $null
            Component            = "DriverPack"
            ReleaseDate            = Get-Date $Item.dateTime -Format "yy.MM.dd"
            Manufacturer        = 'Dell'
            Name                = $Name
            #Description = ($Item.Description.Display.'#cdata-section'.Trim())
            DellVersion            = $Item.dellVersion
            Url                    = -join ($OnlineBaseUri, $Item.path)
            VendorVersion        = $Item.vendorVersion
            FileName            = (split-path -leaf $Item.path)
            SizeMB                = '{0:f2}' -f ($Item.size/1MB)
            ReleaseID            = $Item.ReleaseID
            Brand                = ($Item.SupportedSystems.Brand.Display.'#cdata-section'.Trim() | Select-Object -Unique)
            Key                    = ($Item.SupportedSystems.Brand.key | Select-Object -Unique)
            Prefix                = ($Item.SupportedSystems.Brand.prefix | Select-Object -Unique)
            Model                = $Model
            ModelID                = $ModelID
            SystemID            = ($Item.SupportedSystems.Brand.Model.systemID | Select-Object -Unique)
            RtsDate                = ($Item.SupportedSystems.Brand.Model.rtsDate | Select-Object -Unique)
            Generation            = $Generation
            SupportedOS            = ($Item.SupportedOperatingSystems.OperatingSystem.Display.'#cdata-section'.Trim() | Select-Object -Unique)
            osCode                = $osCode
            osVendor            = ($Item.SupportedOperatingSystems.OperatingSystem.osVendor.Trim() | Select-Object -Unique)
            osArch                = ($Item.SupportedOperatingSystems.OperatingSystem.osArch.Trim() | Select-Object -Unique)
            osType                = ($Item.SupportedOperatingSystems.OperatingSystem.osType.Trim() | Select-Object -Unique)
            majorVersion        = ($Item.SupportedOperatingSystems.OperatingSystem.majorVersion.Trim() | Select-Object -Unique)
            minorVersion        = ($Item.SupportedOperatingSystems.OperatingSystem.minorVersion.Trim() | Select-Object -Unique)
            spMajorVersion        = ($Item.SupportedOperatingSystems.OperatingSystem.spMajorVersion.Trim() | Select-Object -Unique)
            spMinorVersion        = ($Item.SupportedOperatingSystems.OperatingSystem.spMinorVersion.Trim() | Select-Object -Unique)
            ImportantInfoUrl    = ($Item.ImportantInfo.URL.Trim() | Select-Object -Unique)
            #Format = $Item.format
            #Delta = $Item.delta
            #Type = $Item.type
            OSVersion           = $null
            OSReleaseId         = $null
            OSBuild             = $null
            HashMD5                = $Item.HashMD5
        }
        New-Object -TypeName PSObject -Property $ObjectProperties
    }
    #=================================================
    # Normalize Results
    #=================================================
    foreach ($Item in $Results) {
        if ($Item.Name -match 'Win10') {
            $Item.OSVersion = 'Windows 10 x64'
        }
        if ($Item.Name -match 'Win11') {
            $Item.OSVersion = 'Windows 11 x64'
        }
    }

    #Need to remove duplicates
    $Results = $Results | Sort-Object ReleaseDate -Descending | Group-Object Name | ForEach-Object {$_.Group | Select-Object -First 1}
    #=================================================
    # 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:DellDriverPackCatalog'
    $Global:DellDriverPackCatalog = $Results | Sort-Object -Property Name
    #=================================================
}