Public/WinOS/Save-WinPECloudDriver.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
function Save-WinPECloudDriver {
    <#
    .SYNOPSIS
    Download and expand WinPE Drivers
     
    .DESCRIPTION
    Download and expand WinPE Drivers
    This function must be run in Windows
     
    .LINK
    https://github.com/OSDeploy/OSD/tree/master/Docs
    #>

    [CmdletBinding(PositionalBinding = $false)]
    [OutputType([System.IO.FileInfo])]
    param (
        [System.String[]]
        #WinPE Driver: Download and install in WinPE drivers from Dell,HP,IntelNet,LenovoDock,Nutanix,Surface,USB,VMware,WiFi
        [ValidateSet('*','Dell','HP','IntelNet','LenovoDock','Surface','Nutanix','USB','VMware','WiFi')]
        $CloudDriver,

        [System.String[]]
        #WinPE Driver: HardwareID of the Driver download from Microsoft Catalog
        [Alias('HardwareID')]
        $DriverHWID,

        [System.String]
        #WinPE Driver: Destination path to save the drivers
        #If not specified, a random directory in $env:TEMP is selected
        $Path
    )
    #=================================================
    # Block
    #=================================================
    Block-WinPE
    Block-StandardUser
    Block-WindowsVersionNe10
    Block-PowerShellVersionLt5
    #=================================================
    # CloudDriver
    #=================================================
    if ($CloudDriver -contains '*') {
        $CloudDriver = @('Dell','HP','IntelNet','LenovoDock','Nutanix','Surface','USB','VMware','WiFi')
    }

    $DellCloudDriverText                = 'Dell WinPE Driver Pack'
    $DellCloudDriverPack                = Get-DellWinPEDriverPack
    
    $HpCloudDriverText                  = 'HP WinPE Driver Pack'
    $HpCloudDriverPack                  = Get-HpWinPEDriverPack

    $OSDCatalogIntelEthernetDriver      = Get-IntelEthernetDriverPack | `
                                        Where-Object {($_.OSVersion -match '10.0')} | `
                                        Where-Object {($_.OSArch -match 'x64')} | `
                                        Select-Object -First 1
    $IntelEthernetCloudDriverUrl        = $OSDCatalogIntelEthernetDriver.DriverUrl
    $IntelEthernetCloudDriverVersion    = $OSDCatalogIntelEthernetDriver.DriverVersion
    $IntelEthernetCloudDriverText       = "Intel Ethernet Driver Pack [$IntelEthernetCloudDriverVersion] $IntelEthernetCloudDriverUrl"

    $OSDCatalogIntelWirelessDriver      = Get-IntelWirelessDriverPack | `
                                        Where-Object {($_.OSVersion -match '10.0')} | `
                                        Where-Object {($_.OSArch -match 'x64')} | `
                                        Select-Object -First 1
    $IntelWiFiCloudDriverUrl            = $OSDCatalogIntelWirelessDriver.DriverUrl
    $IntelWiFiCloudDriverVersion        = $OSDCatalogIntelWirelessDriver.DriverVersion
    $IntelWiFiCloudDriverText           = "Intel Wireless Driver Pack [$IntelWiFiCloudDriverVersion] $IntelWiFiCloudDriverUrl"

    $LenovoCloudDriverText              = 'Lenovo WinPE Driver Pack'
    $LenovoCloudDriverPacks             = @(
                                        'https://pcsupport.lenovo.com/downloads/DS105415'
                                        'https://pcsupport.lenovo.com/downloads/DS542093'
                                        'https://pcsupport.lenovo.com/downloads/DS542998'
                                        'https://support.lenovo.com/downloads/DS104737'
                                        'https://support.lenovo.com/downloads/DS105119'
                                        'https://support.lenovo.com/downloads/DS105977'
                                        'https://support.lenovo.com/downloads/DS106048'
                                        'https://support.lenovo.com/downloads/DS106096'
                                        'https://support.lenovo.com/downloads/DS112079'
                                        'https://support.lenovo.com/downloads/DS112425'
                                        'https://support.lenovo.com/downloads/DS113152'
                                        'https://support.lenovo.com/downloads/DS119040'
                                        'https://support.lenovo.com/downloads/DS119264'
                                        'https://support.lenovo.com/downloads/DS119270'
                                        'https://support.lenovo.com/downloads/DS119281'
                                        'https://support.lenovo.com/downloads/DS120413'
                                        'https://support.lenovo.com/downloads/DS120934'
                                        'https://support.lenovo.com/downloads/DS500698'
                                        'https://support.lenovo.com/downloads/DS500699'
                                        'https://support.lenovo.com/downloads/DS500715'
                                        'https://support.lenovo.com/downloads/DS500728'
                                        'https://support.lenovo.com/downloads/DS500738'
                                        'https://support.lenovo.com/downloads/DS501356'
                                        'https://support.lenovo.com/downloads/DS501531'
                                        'https://support.lenovo.com/downloads/DS502154'
                                        'https://support.lenovo.com/downloads/DS502454'
                                        'https://support.lenovo.com/downloads/DS503363'
                                        'https://support.lenovo.com/downloads/DS503944'
                                        'https://support.lenovo.com/downloads/DS504611'
                                        'https://support.lenovo.com/downloads/DS504613'
                                        'https://support.lenovo.com/downloads/DS505256'
                                        'https://support.lenovo.com/downloads/DS505931'
                                        'https://support.lenovo.com/downloads/DS541513'
                                        'https://support.lenovo.com/downloads/DS542109'
                                        'https://support.lenovo.com/downloads/DS543834'
                                        'https://support.lenovo.com/downloads/DS544286'
                                        'https://support.lenovo.com/downloads/DS545020'
                                        'https://support.lenovo.com/downloads/DS545687'
                                        'https://support.lenovo.com/downloads/DS546529'
                                        'https://support.lenovo.com/downloads/DS547277'
                                        'https://support.lenovo.com/downloads/DS547601'
                                        'https://support.lenovo.com/downloads/DS547827'
                                        'https://support.lenovo.com/downloads/DS548453'
                                        'https://support.lenovo.com/downloads/DS549669'
                                        'https://support.lenovo.com/downloads/DS549738'
                                        'https://support.lenovo.com/downloads/DS551195'
                                        'https://support.lenovo.com/downloads/DS551368'
                                        'https://support.lenovo.com/downloads/DS554384'
                                        'https://support.lenovo.com/downloads/ds504277'
                                    )

    $LenovoDockCloudDriverText          = 'Lenovo Dock WinPE Driver Pack [22.1.31]'
    $LenovoDockCloudDriverUrl           = @(
                                        'https://download.lenovo.com/pccbbs/mobiles/rtk-winpe-w10.zip'
                                        'https://download.lenovo.com/km/media/attachment/USBCG2.zip'
                                        )

    $NutanixCloudDriverText             = 'Nutanix WinPE Driver Pack [Microsoft Catalog]'
    $NutanixCloudDriverHwids            = @(
                                        'VEN_1AF4&DEV_1000 and VEN_1AF4&DEV_1041' #Red Hat Nutanix VirtIO Ethernet Adapter
                                        'VEN_1AF4&DEV_1002' #Red Hat Nutanix VirtIO Balloon
                                        'VEN_1AF4&DEV_1004 and VEN_1AF4&DEV_1048' #Red Hat Nutanix VirtIO SCSI pass-through controller
                                        )

                                        #https://docs.microsoft.com/en-us/surface/enable-surface-keyboard-for-windows-pe-deployment
    $SurfaceCloudDriverText             = 'Surface WinPE Driver Pack [Microsoft Catalog]'
    $SurfaceCloudDriverHwids            = @(
                                        'MSHW0028' #Button and MSHW0040
                                        'MSHW0084' #Serial Hub
                                        'MSHW0091' #ACPI Notify
                                        'MSHW0094' #Null
                                        'MSHW0096' #Keyboard
                                        'MSHW0146' #Battery
                                        'MSHW0153' #HotPlug
                                        'MSHW0184' #Light Sensor
                                        )
                                        #'VEN_8086&DEV_A0D0 VEN_8086&DEV_43D0 VEN_8086&DEV_A0D1 VEN_8086&DEV_43D1' #Touch
    
    $UsbDongleHwidsText                 = 'USB Dongle Driver Pack [Microsoft Catalog]'
    $UsbDongleHwids                     = @(
                                        'VID_045E&PID_0927 VID_045E&PID_0927 VID_045E&PID_09A0 Surface Ethernet'
                                        'VID_0B95&PID_7720 VID_0B95&PID_7E2B Asix AX88772 USB2.0 to Fast Ethernet'
                                        'VID_0B95&PID_1790 ASIX AX88179 USB 3.0 to Gigabit Ethernet'
                                        'VID_0BDA&PID_8153 Realtek USB GbE and Dell DA 300'
                                        'VID_13B1&PID_0041'
                                        'VID_17EF&PID_720C Lenovo USB-C Ethernet'
                                        )
    
    $VmwareCloudDriverText              = 'VMware WinPE Driver Pack [Microsoft Catalog]'
    $VmwareCloudDriverHwids             = @(
                                        'VEN_15AD&DEV_0740' #VMware Virtual Machine Communication Interface
                                        'VEN_15AD&DEV_07B0' #VMware VMXNET3 Ethernet Controller
                                        'VEN_15AD&DEV_07C0' #VMware PVSCSI Controller
                                        )
    #=================================================
    # Block
    #=================================================
    Block-WinPE
    Block-StandardUser
    Block-WindowsVersionNe10
    Block-PowerShellVersionLt5
    Block-NoCurl
    Block-NoInternet
    #=================================================
    # Path
    #=================================================
    if (-not $Path) {
        $Path = Join-Path $env:TEMP (Get-Random)
        Write-Warning "Path was not specified, defaulting to $Path"
    }
    If (-not (Test-Path $Path)) {
        try {
            $null = New-Item -Path $Path -ItemType Directory -Force -ErrorAction Stop
        }
        catch {
            Write-Error $_
            Break
        }
    }
    #=================================================
    # DriverHWID
    #=================================================
    foreach ($Item in $DriverHWID) {
        Save-MsUpCatDriver -HardwareID $Item -DestinationDirectory (Join-Path $Path 'HardwareID')
    }
    #=================================================
    # CloudDriver
    #=================================================
    foreach ($DriverPack in $CloudDriver) {
        #=================================================
        # Dell
        #=================================================
        if ($DriverPack -eq 'Dell'){
            Write-Host -ForegroundColor Yellow "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) $DellCloudDriverText"

            if (Test-WebConnection -Uri $DellCloudDriverPack) {
               $DriverPackDownload = Save-WebFile -SourceUrl $DellCloudDriverPack

             if (Test-Path $DriverPackDownload.FullName) {
                    $DriverPackItem = Get-Item -Path $DriverPackDownload.FullName
                    $DriverPackExpand = Join-Path $Path (Join-Path $DriverPack $DriverPackItem.BaseName)
            
                    if (-NOT (Test-Path $DriverPackExpand)) {
                        New-Item -Path $DriverPackExpand -ItemType Directory -Force | Out-Null
                    }

                    Expand -R "$($DriverPackItem.FullName)" -F:* "$DriverPackExpand" | Out-Null
                    if (Test-Path "$DriverPackExpand\winpe\x86") {
                        $null = Remove-Item -Path "$DriverPackExpand\winpe\x86" -Recurse -Force -ErrorAction Ignore
                    }
                }
            }
            else {
                Write-Warning "Unable to connect to $DellCloudDriverPack"
         }
        }
        #=================================================
        # HP
        #=================================================
        if ($DriverPack -eq 'HP') {
            Write-Host -ForegroundColor Yellow "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) $HpCloudDriverText"

            if (Test-WebConnection -Uri $HpCloudDriverPack)   {
              $DriverPackDownload = Save-WebFile -SourceUrl $HpCloudDriverPack

                if (Test-Path $DriverPackDownload.FullName) {
                    $DriverPackItem = Get-Item -Path $DriverPackDownload.FullName
                    $DriverPackExpand = Join-Path $Path (Join-Path $DriverPack $DriverPackItem.BaseName)

                    Start-Process -FilePath $DriverPackItem -ArgumentList "/s /e /f `"$DriverPackExpand`"" -Wait
                }
            }
            else {  
                Write-Warning "Unable to connect to $HpCloudDriverPack"  
            }
        }
        #=================================================
        # Intel Ethernet
        #=================================================
        if ($DriverPack -eq 'IntelNet') {
            Write-Host -ForegroundColor Yellow "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) $IntelEthernetCloudDriverText"

            if (Test-WebConnection -Uri $IntelEthernetCloudDriverUrl)   {
                $DriverPackDownload = Save-WebFile -SourceUrl $IntelEthernetCloudDriverUrl
                   if (Test-Path $DriverPackDownload.FullName) {
                    $DriverPackItem = Get-Item -Path $DriverPackDownload.FullName
                    $DriverPackExpand = Join-Path $Path (Join-Path $DriverPack $DriverPackItem.BaseName)
                    Expand-Archive -Path $DriverPackItem -DestinationPath $DriverPackExpand -Force

                    #$DriverPackExpand = Join-Path $DriverPackItem.Directory $DriverPackItem.BaseName
                    #Expand-Archive -Path $DriverPackItem -DestinationPath $DriverPackExpand -Force
                    #$IntelExe = Get-ChildItem -Path $DriverPackExpand 'Wired_driver_26.8_x64.exe'
                    #$IntelExe | Rename-Item -newname { $_.name -replace '.exe','.zip' } -Force -ErrorAction Ignore
                    #$DriverPackItem = Get-ChildItem -Path $DriverPackExpand 'Wired_driver_26.8_x64.zip' -Recurse
                    #$DriverPackExpand = Join-Path $DriverPackItem.Directory $DriverPackItem.BaseName
                    #$DriverPackExpand = Join-Path $Path (Join-Path $DriverPack $DriverPackItem.BaseName)
                    #Expand-Archive -Path $DriverPackItem.FullName -DestinationPath $DriverPackExpand -Force
                    $RemoveItems = Get-ChildItem -Path $DriverPackExpand -Directory -Recurse | Where-Object {$_.Name -in @('APPS','DDP_Profiles','DOCS','NDIS63','NDIS64','NVMUpdatePackage','RDMA')}
                    foreach ($Item in $RemoveItems) {
                        Remove-Item $Item.FullName -Recurse -Force -ErrorAction Ignore
                    }
                }
            }
            else {
                Write-Warning "Unable to connect to $IntelEthernetCloudDriverUrl"
            }
        }
        #=================================================
        # Intel WiFi
        #=================================================
        if ($DriverPack -eq 'WiFi') {
            Write-Host -ForegroundColor Yellow $IntelWiFiCloudDriverText
            if (Test-WebConnection -Uri $IntelWiFiCloudDriverUrl) {
                $DriverPackDownload = Save-WebFile -SourceUrl $IntelWiFiCloudDriverUrl
                if (Test-Path $DriverPackDownload.FullName) {
                    $DriverPackItem = Get-Item -Path $DriverPackDownload.FullName
                    $DriverPackExpand = Join-Path $Path (Join-Path $DriverPack $DriverPackItem.BaseName)
                    Expand-Archive -Path $DriverPackItem -DestinationPath $DriverPackExpand -Force
                }
            }
            else {
                Write-Warning "Unable to connect to $IntelWiFiCloudDriverUrl"
            }
        }
        #=================================================
        # LenovoDock
        #=================================================
        if ($DriverPack -eq 'LenovoDock') {
            Write-Host -ForegroundColor Yellow "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) $LenovoDockCloudDriverText"

            foreach ($OnlineDriver in $LenovoDockCloudDriverUrl) {
                if (Test-WebConnection -Uri $OnlineDriver) {
                    $DriverPackDownload = Save-WebFile -SourceUrl $OnlineDriver

                    if (Test-Path $DriverPackDownload.FullName) {
                        $DriverPackItem = Get-Item -Path $DriverPackDownload.FullName
                        $DriverPackExpand = Join-Path $Path (Join-Path $DriverPack $DriverPackItem.BaseName)

                        Expand-Archive -Path $DriverPackItem -DestinationPath $DriverPackExpand -Force
                        Get-ChildItem -Path "$DriverPackExpand\WIN10\32" | Remove-Item -Recurse -Force
                    }
                }
                else {
                    Write-Warning "Unable to connect to $LenovoDockCloudDriverUrl"
                }
            }
        }
        #=================================================
        # Nutanix
        #=================================================
        if ($DriverPack -eq 'Nutanix') {
            Write-Host -ForegroundColor Yellow "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) $NutanixCloudDriverText"
            Save-MsUpCatDriver -HardwareID $NutanixCloudDriverHwids -DestinationDirectory (Join-Path $Path $DriverPack)
        }
        #=================================================
        # Surface
        #=================================================
        if ($DriverPack -eq 'Surface') {
            Write-Host -ForegroundColor Yellow "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) $SurfaceCloudDriverText"
            Save-MsUpCatDriver -HardwareID $SurfaceCloudDriverHwids -DestinationDirectory (Join-Path $Path $DriverPack)
        }
        #=================================================
        # USB Dongles
        #=================================================
        if ($DriverPack -eq 'USB') {
            Write-Host -ForegroundColor Yellow "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) $UsbDongleHwidsText"
            Save-MsUpCatDriver -HardwareID $UsbDongleHwids -DestinationDirectory (Join-Path $Path $DriverPack)
        }
        #=================================================
        # VMware
        #=================================================
        if ($DriverPack -eq 'VMware') {
            Write-Host -ForegroundColor Yellow "$((Get-Date).ToString('yyyy-MM-dd-HHmmss')) $VmwareCloudDriverText"
            Save-MsUpCatDriver -HardwareID $VmwareCloudDriverHwids -DestinationDirectory (Join-Path $Path $DriverPack)
        }
    }
    #=================================================
    # Complete
    #=================================================
    $DriverPath = Get-Item $Path
    if ($Clipboard) {
        Set-Clipboard -Value $DriverPath.FullName -Verbose
    }
    Return $DriverPath
    #=================================================
}