Get-DriversPackFromDell.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
<#PSScriptInfo

.VERSION 1.1

.GUID 7f1f9ff8-2536-4673-a33a-29180b321668

.AUTHOR Brice SARRAZIN

.COMPANYNAME

.COPYRIGHT

.TAGS Dell CAB drivers PSEdition_Desktop PSEdition_Core Windows Linux Mac

.LICENSEURI https://github.com/b-sarrazin/Get-DriversPackFromDell/blob/master/LICENSE

.PROJECTURI https://github.com/b-sarrazin/Get-DriversPackFromDell

.ICONURI

.EXTERNALMODULEDEPENDENCIES

.REQUIREDSCRIPTS

.EXTERNALSCRIPTDEPENDENCIES

.RELEASENOTES


.PRIVATEDATA

#>


<#

.DESCRIPTION
# Get-DriversPackFromDell

A script to download CAB files from Dell

## Getting Started

The following variables can be modified:
* driverCatalog: URL to download CAB files
* downloadFolder: destination folder for CAB files
* monthsBack: download drivers pack newer than X month. 0 equal no time limit
* models: filter CAB files according to the requested models
* operatingSystems: filter CAB files according to the operating systems requested
* architectures: filter CAB files according to the requested architectures

The use of parameters is not mandatory.
However, for more convenience, it is advisable to modify the following variables directly in the code: $downloadFolder, $monthsBack

### Prerequisites

* Windows 7+ / Windows Server 2003+
* PowerShell v5+

## Description

The script performs the following actions:
* download CAB files from Dell
* skip files already downloaded
* removes old packages when a new one is downloaded

It is possible to filter according to:
* computer models
* operating systems
* processor architecture
* age of CAB files

## Exemple

Download CAB files less than 12 months old
> .\Get-DriversPackFromDell.ps1 -monthsBack 12

Download CAB files less than 6 months old corresponding to x86 or x64 architectures and Windows 7 or 10 operating systems :
> .\Get-DriversPackFromDell.ps1 -architectures x86,x64 -operatingSystems Windows10,Windows7 -monthsBack 6

Download CAB files corresponding to models Latitude 7370 or Latitude 7490
> .\Get-DriversPackFromDell.ps1 -models 'Latitude 7370','Latitude 7490'

*Remember to take advantage of auto-completion, especially for computer models*

#>


[CmdletBinding()]
param
(
    [Parameter(HelpMessage = 'Driver Pack Catalog download address')]
    [string]$driverCatalog = 'http://downloads.dell.com/catalog/DriverPackCatalog.cab',
    [Parameter(HelpMessage = 'path to the CAB file download folder')]
    [string]$downloadFolder = (Join-Path $env:USERPROFILE 'Downloads'),
    [Parameter(HelpMessage = 'download drivers pack newer than X month. 0 equal no time limit')]
    [int]$monthsBack = 0
)

DynamicParam
{
    $parametersName = 'models', 'operatingSystems', 'architectures'
    $runtimeParameterDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
    
    foreach ($parameterName in $parametersName)
    {
        $attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
        $parameterAttribute = New-Object System.Management.Automation.ParameterAttribute
        $attributeCollection.Add($parameterAttribute)
        if (Test-Path ".\$parameterName.txt")
        {
            $arrSet = Get-Content -Path ".\$parameterName.txt"
        }
        else
        {
            $arrSet = ""
        }
        $validateSetAttribute = New-Object System.Management.Automation.ValidateSetAttribute($arrSet)
        $attributeCollection.Add($validateSetAttribute)
        $runtimeParameter = New-Object System.Management.Automation.RuntimeDefinedParameter($parameterName, [array], $attributeCollection)
        $PSBoundParameters[$parameterName] = "*"
        
        $runtimeParameterDictionary.Add($parameterName, $runtimeParameter)
    }
    return $runtimeParameterDictionary
}

BEGIN
{
    Write-Output "Updating variables models, operatingSystems, architectures"
    $models = $PSBoundParameters['models']
    $operatingSystems = $PSBoundParameters['operatingSystems']
    $architectures = $PSBoundParameters['architectures']
}
PROCESS
{
    Write-Output "Downloading Driver Pack Catalog (CAB)"
    Write-Output "URL : $driverCatalog"
    try
    {
        $temp = "$env:TEMP\$([guid]::NewGuid())"
        New-Item -Path $temp -ItemType Directory -ErrorAction Stop | Out-Null
        Start-BitsTransfer -DisplayName "Driver Pack Catalog (CAB)" -Description "$driverCatalog" -Source $driverCatalog -Destination $temp -ErrorAction Stop
    }
    catch
    {
        Write-Warning "Failed to download Driver Pack Catalog (ERROR : $($Error[0]))"
    }
    
    Write-Output "Expanding Driver Pack Catalog (CAB to XML)"
    try
    {
        $cabCatalog = Join-Path $temp $driverCatalog.Split("/")[-1]
        $oShell = New-Object -ComObject Shell.Application
        $sourceFile = $oShell.Namespace("$cabCatalog").items()
        $destinationFolder = $oShell.Namespace("$temp")
        $destinationFolder.CopyHere($sourceFile)
    }
    catch
    {
        Write-Warning "Failed to expand Driver Pack Catalog"
    }
    
    Write-Debug "Moving Driver Pack Catalog"
    try
    {
        $xmlCatalog = Join-Path $temp $($sourceFile | Select-Object -ExpandProperty Name)
        Move-Item -Path $xmlCatalog -Destination $downloadFolder -Force -ErrorAction Stop
        $xmlCatalog = Join-Path $downloadFolder $($sourceFile | Select-Object -ExpandProperty Name)
    }
    catch
    {
        Write-Warning "Failed to move Driver Pack Catalog (ERROR : $($Error[0])"
    }
    
    Write-Output "Loading Driver Pack Catalog (XML)"
    $catalog = [xml](Get-Content $xmlCatalog)
    $urlRoot = "http://" + $($catalog.DriverPackManifest | Select-Object -ExpandProperty baseLocation)
    
    #region Create/update attibute set for variables models, operatingSystems, architectures
    [array]$supportedModels = @()
    [array]$supportedOS = @()
    [array]$supportedArch = @()
    $catalog.DriverPackManifest.DriverPackage | ForEach-Object {
        $supportedModels += $_.SupportedSystems.Brand.Model | Select-Object -ExpandProperty name
        $supportedOS += $_.SupportedOperatingSystems.OperatingSystem | Select-Object -ExpandProperty osCode
        $supportedArch += $_.SupportedOperatingSystems.OperatingSystem | Select-Object -ExpandProperty osArch
    }
    $supportedModels | Select-Object -Unique | Sort-Object | Set-Content .\models.txt
    $supportedOS | Select-Object -Unique | Sort-Object | Set-Content .\operatingSystems.txt
    $supportedArch | Select-Object -Unique | Sort-Object | Set-Content .\architectures.txt
    #endregion
    
    $catalog.DriverPackManifest.DriverPackage | ForEach-Object {
        
        $driversPack = $_
        
        if (($monthsBack -eq 0) `
            -or ([datetime]$($driversPack | Select-Object -ExpandProperty dateTime) -ge [datetime]::Today.AddMonths(- $monthsBack)))
        {
            foreach ($model in $models)
            {
                [array]$supportedModels = $driversPack.SupportedSystems.Brand.Model | Select-Object -ExpandProperty name
                
                if (($model -in $supportedModels) -or ($model -eq "*"))
                {
                    Write-Debug "Matching model : $supportedModels"
                    
                    foreach ($OS in $operatingSystems)
                    {
                        [array]$supportedOS = $driversPack.SupportedOperatingSystems.OperatingSystem | Select-Object -ExpandProperty osCode
                        
                        if (($OS -in $supportedOS) -or ($OS -eq "*"))
                        {
                            Write-Debug "Matching OS : $supportedOS"
                            
                            foreach ($arch in $architectures)
                            {
                                [array]$supportedArch = $driversPack.SupportedOperatingSystems.OperatingSystem | Select-Object -ExpandProperty osArch
                                
                                if (($arch -in $supportedArch) -or ($arch -eq "*"))
                                {
                                    Write-Debug "Matching architecture : $supportedArch"
                                    
                                    [array]$supportedOSandArch = @()
                                    for ($i = 0; $i -lt $supportedOS.Count; $i++)
                                    {
                                        if ($i -eq 0)
                                        {
                                            $supportedOSandArch = $supportedOS[$i].ToString().Trim() + " " + $supportedArch[$i].ToString().Trim()
                                        }
                                        else
                                        {
                                            $supportedOSandArch += " / " + $supportedOS[$i].ToString().Trim() + " " + $supportedArch[$i].ToString().Trim()
                                        }
                                    }
                                    $supportedOSandArch[-1] = $supportedOSandArch[-1].ToString().Trim()
                                    
                                    $alreadyDownloaded = $false
                                    
                                    $driversPackURL = "$urlRoot/$($driversPack | Select-Object -ExpandProperty path)"
                                    
                                    $filter = $driversPackURL.Split("/")[-1]
                                    $filter = $filter.Split('-')[0] + "-" + $filter.Split('-')[1] + "-*-*"
                                    
                                    Get-Item $(Join-Path $downloadFolder $filter) | Select-Object -ExpandProperty Name |
                                    ForEach-Object {
                                        if ($driversPackURL.Split("/")[-1] -eq $_) # already downloaded
                                        {
                                            Write-Information "Package $supportedModels on $supportedOSandArch already downloaded"
                                            break
                                        }
                                    }
                                    if (-not $alreadyDownloaded)
                                    {
                                        try
                                        {
                                            Write-Information "Downloading package for $supportedModels - $supportedOSandArch"
                                            Start-BitsTransfer -DisplayName $driversPackURL.Split("/")[-1] `
                                                               -Description "$supportedModels - $supportedOSandArch" `
                                                               -Source $driversPackURL`                                                                -Destination $downloadFolder
                                            Write-Information "Package $($driversPackURL.Split("/")[-1]) downloaded for $supportedModels - $supportedOSandArch"
                                        }
                                        catch
                                        {
                                            Write-Warning "Failed to download package for $supportedModels - $supportedOSandArch"
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
END
{

}