Public/Functions/Catalogs/Update/Update-MicrosoftDriverPackCatalog.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
<#
.SYNOPSIS
Builds the Microsoft Surface DriverPacks
 
.DESCRIPTION
Builds the Microsoft Surface DriverPacks
 
.LINK
https://github.com/OSDeploy/OSD/tree/master/Docs
 
.NOTES
#>

function Update-MicrosoftDriverPackCatalog {
    [CmdletBinding()]
    param (
        #Updates the OSD Module Offline Catalog
        [System.Management.Automation.SwitchParameter]
        $UpdateModule
    )
    #=================================================
    # Reference
    #=================================================
    # Device List
    # https://docs.microsoft.com/en-us/surface/surface-system-sku-reference
    #
    # Supported Operating Systems
    # https://support.microsoft.com/en-us/surface/surface-supported-operating-systems-9559cc3c-7a38-31b6-d9fb-571435e84cd1
    #
    # Download Links
    # https://support.microsoft.com/en-us/surface/download-drivers-and-firmware-for-surface-09bb2e09-2a4b-cb69-0951-078a7739e120#bkmk_update-manually
    #
    # https://docs.microsoft.com/en-us/surface/manage-surface-driver-and-firmware-updates
    # https://www.reddit.com/r/Surface/comments/mlhqw5/all_direct_download_links_for_surface/
    # https://dancharblog.wordpress.com/2021/04/06/all-direct-download-links-for-surface-firmware-drivers/
    #=================================================
    # Defaults
    #=================================================
    $OfflineCatalogName = 'MicrosoftDriverPackCatalog.json'

    $OnlineCatalogName = 'MicrosoftDriverPackCatalog.json'
    $OnlineBaseUri = 'https://www.microsoft.com/en-us/download/details.aspx?id='
    $OnlineDownloadUri = 'https://www.microsoft.com/en-us/download/confirmation.aspx?id='
    $OnlineCatalogUri = 'https://support.microsoft.com/en-us/surface/download-drivers-and-firmware-for-surface-09bb2e09-2a4b-cb69-0951-078a7739e120'

    $MicrosoftSurfaceModels = Get-Content -Path "$($MyInvocation.MyCommand.Module.ModuleBase)\Catalogs\MicrosoftSurfaceModels.json" -Raw
    #=================================================
    # 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)
    $TempCatalogFile        = Join-Path $env:TEMP (Join-Path 'OSD' $OfflineCatalogName)
    $ModuleCatalogXml       = "$($MyInvocation.MyCommand.Module.ModuleBase)\Catalogs\MicrosoftDriverPackCatalog.xml"
    $ModuleCatalogJson      = "$($MyInvocation.MyCommand.Module.ModuleBase)\Catalogs\MicrosoftDriverPackCatalog.json"
    #=================================================
    # UseCatalog Cloud
    #=================================================
    $SurfaceModels = $MicrosoftSurfaceModels | ConvertFrom-Json
    $MasterResults = @()

    $MasterResults = foreach ($Surface in $SurfaceModels) {
        Write-Verbose -Verbose "Processing $($Surface.Name)"
        $DriverPage = $OnlineDownloadUri + $Surface.PackageID
        $Downloads = (Invoke-WebRequest -Uri $DriverPage).Links
        $Downloads = $Downloads | Where-Object {$_.href -match 'download.microsoft.com'}
        $Downloads = $Downloads | Where-Object {($_.href -match 'Win11') -or ($_.href -match 'Win10')}
        $Downloads = $Downloads | Sort-Object href | Select-Object href -Unique
        #$Downloads = $Downloads | Select-Object -Last 1
        #$Surface.Url = ($Downloads).href
        #$Surface.FileName = Split-Path $Surface.Url -Leaf
        #=================================================
        # Create Object
        #=================================================
        foreach ($Download in $Downloads) {
            $DownloadUrl = $Download.href
            Write-Verbose -Verbose "Testing Download File at $DownloadUrl"

            $GetUrl = Invoke-WebRequest -Method Head -Uri $DownloadUrl
            $GetHeaders = $GetUrl.Headers
            $GetLastModified = $GetHeaders['Last-Modified']
            Write-Verbose -Verbose "Last Modified: $GetLastModified"

            $ReleaseDate = (Get-Date $GetLastModified).ToString('yy.MM.dd')
            $FileName = Split-Path $DownloadUrl -Leaf

            if ($FileName -match 'Win11') {
                $OSVersion = 'Windows 11 x64'
            }
            else {
                $OSVersion = 'Windows 10 x64'
            }
            
            $ByteArray = [System.Convert]::FromBase64String($GetHeaders['Content-MD5'])
            $HexObject = $ByteArray | Format-Hex
            $HashMD5 = ($HexObject.Bytes | ForEach-Object {"{0:X}" -f $_}) -join ''

            $UniqueFileName = $FileName
            $UniqueFileName = $UniqueFileName -replace '_Win', ' Win'
            $UniqueFileName = $UniqueFileName.Split(' ')[1]
            $UniqueFileName = $UniqueFileName -replace 'Win10_', 'Win10 '
            $UniqueFileName = $UniqueFileName -replace 'Win11_', 'Win11 '
            $UniqueFileName = $UniqueFileName.Split('_')[0]

            $UniqueName = "Microsoft $($Surface.Name) $UniqueFileName"

            $ObjectProperties = [ordered] @{
                CatalogVersion          = Get-Date -Format yy.MM.dd
                Status                  = $null
                Component               = 'DriverPack'
                ReleaseDate             = $ReleaseDate
                Manufacturer            = 'Microsoft'
                Model                   = $Surface.Model
                Product                 = $Surface.Product
                Name                    = $UniqueName
                PackageID               = $Surface.PackageID
                FileName                = $FileName
                Url                     = $DownloadUrl
                DownloadCenter          = $OnlineBaseUri + $Surface.PackageID
                OSVersion               = $OSVersion
                OSReleaseId             = ''
                OSBuild                 = ''    
                HashMD5                 = $HashMD5
            }
            New-Object -TypeName PSObject -Property $ObjectProperties
        }
    }

    foreach ($Item in $MasterResults) {
        $Item.OSBuild = $Item.Name.Split(' ')[-1]
    }

    foreach ($Item in $MasterResults) {
        if ($Item.Name -match 'Win10 10240') {
            $Item.OSReleaseId = '1507'
            $Item.Name = $Item.Name -replace 'Win10 10240', 'Win10 1507'
        }
        if ($Item.Name -match 'Win10 10586') {
            $Item.OSReleaseId = '1511'
            $Item.Name = $Item.Name -replace 'Win10 10586', 'Win10 1511'
        }
        if ($Item.Name -match 'Win10 14393') {
            $Item.OSReleaseId = '1607'
            $Item.Name = $Item.Name -replace 'Win10 14393', 'Win10 1607'
        }
        if ($Item.Name -match 'Win10 15063') {  
            $Item.OSReleaseId = '1703'
            $Item.Name = $Item.Name -replace 'Win10 15063', 'Win10 1703'
        }
        if ($Item.Name -match 'Win10 16299') {
            $Item.OSReleaseId = '1709'
            $Item.Name = $Item.Name -replace 'Win10 16299', 'Win10 1709'
        }
        if ($Item.Name -match 'Win10 17134') {
            $Item.OSReleaseId = '1803'
            $Item.Name = $Item.Name -replace 'Win10 17134', 'Win10 1803'
        }
        if ($Item.Name -match 'Win10 17763') {
            $Item.OSReleaseId = '1809'
            $Item.Name = $Item.Name -replace 'Win10 17763', 'Win10 1809'
        }
        if ($Item.Name -match 'Win10 18362') {
            $Item.OSReleaseId = '1903'
            $Item.Name = $Item.Name -replace 'Win10 18362', 'Win10 1903'
        }
        if ($Item.Name -match 'Win10 18363') {
            $Item.OSReleaseId = '1909'
            $Item.Name = $Item.Name -replace 'Win10 18363', 'Win10 1909'
        }
        if ($Item.Name -match 'Win10 19041') {
            $Item.OSReleaseId = '2004'
            $Item.Name = $Item.Name -replace 'Win10 19041', 'Win10 2004'
        }
        if ($Item.Name -match 'Win10 19042') {
            $Item.OSReleaseId = '20H2'
            $Item.Name = $Item.Name -replace 'Win10 19042', 'Win10 20H2'
        }
        if ($Item.Name -match 'Win10 19043') {
            $Item.OSReleaseId = '21H1'
            $Item.Name = $Item.Name -replace 'Win10 19043', 'Win10 21H1'
        }
        if ($Item.Name -match 'Win10 19044') {
            $Item.OSReleaseId = '21H2'
            $Item.Name = $Item.Name -replace 'Win10 19044', 'Win10 21H2'
        }
        if ($Item.Name -match 'Win10 19045') {
            $Item.OSReleaseId = '22H2'
            $Item.Name = $Item.Name -replace 'Win10 19045', 'Win10 22H2'
        }
        if ($Item.Name -match 'Win11 22000') {
            $Item.OSReleaseId = '21H2'
            $Item.Name = $Item.Name -replace 'Win11 22000', 'Win11 21H2'
        }
        if ($Item.Name -match 'Win11 22621') {
            $Item.OSReleaseId = '22H2'
            $Item.Name = $Item.Name -replace 'Win11 22621', 'Win11 22H2'
        }
    }
    #=================================================
    # UpdateModule
    #=================================================
    if ($UpdateModule) {
        Write-Verbose -Verbose "UpdateModule: Exporting to OSD Module Catalogs at $ModuleCatalogXml"
        $MasterResults | Export-Clixml -Path $ModuleCatalogXml -Force
        Write-Verbose -Verbose "UpdateModule: Exporting to OSD Module Catalogs at $ModuleCatalogJson"
        $MasterResults | ConvertTo-Json | Out-File $ModuleCatalogJson -Encoding ascii -Width 2000 -Force
    }
    #=================================================
    # Results
    #=================================================
    Write-Verbose -Verbose 'Complete: Results have been stored $Global:MicrosoftDriverPackCatalog'
    $Global:MicrosoftDriverPackCatalog = $MasterResults | Sort-Object -Property Name
    #=================================================
}