-
Notifications
You must be signed in to change notification settings - Fork 3
/
Get-ADKWinPEAddons.ps1
143 lines (129 loc) · 5.73 KB
/
Get-ADKWinPEAddons.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
#Requires -Version 4
#Requires -RunAsAdministrator
Function Get-ADKWinPEAddonsFiles {
[CmdletBinding()]
Param(
[parameter(Mandatory)]
[system.string]$TargetFolder
)
Begin {
$HT = @{ ErrorAction = 'Stop'}
# Validate target folder
try {
$null = Get-Item $TargetFolder @HT
} catch {
Write-Warning -Message 'The target folder specified as parameter does not exist'
break
}
}
Process {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$adkGenericURL = (Invoke-WebRequest -Uri 'https://go.microsoft.com/fwlink/?linkid=2175097' -MaximumRedirection 0 -ErrorAction SilentlyContinue)
# 302 = redirect as moved temporarily
# 301 = Moved Permanently
if ($adkGenericURL.StatusCode -eq 302) {
# Currently set to https://download.microsoft.com/download/5/2/5/525dcde0-c7b8-487a-894d-0952775a78c7/adkwinpeaddons
# Resolving download root for: https://go.microsoft.com/fwlink/?linkid=2175097
$MainURL = $adkGenericURL.Headers.Location
Write-Verbose "Root URI set to $($MainURL)"
if(-not ($MainURL.EndsWith('/'))) {
$MainURL = "$($MainURL)/"
}
$InstallerURLs = DATA {
ConvertFrom-StringData @'
0=0b63b7c537782729483bff2d64a620fa.cab
1=13334584cdbf659a8dd105fc34851ea3.cab
2=24ab0f36f991590c112d8f31215bfe53.cab
3=3b8aa90b3015bd30b59330184cbbb275.cab
4=3ffff92a2c7e8b2f1a6e85afeaa027ca.cab
5=755a8f59ce4582d52bc4c91761db7935.cab
6=856a378483a6c7e8068c4f52cb49227c.cab
7=8686aa39453398c3a04c1edaac29ca7a.cab
8=9722214af0ab8aa9dffb6cfdafd937b7.cab
9=a6296c5a6eec9ca02b431ed0461b952d.cab
10=bfdbb113d9d5201e5897ed28d0a47f42.cab
11=e4ef343897232361feeab2810591d8ae.cab
12=e7a23bd4ab45aa717a3e0b2fa877dce9.cab
13=f493bb83bbe844f5cd3610b0847bc113.cab
14=f53c2cdf2f5aeeaef4ca19d4b3344930.cab
15=Windows PE Boot Files (DesktopEditions)-x86_en-us.msi
16=Windows PE Boot Files (OnecoreUAP)-x86_en-us.msi
17=Windows PE Optional Packages (DesktopEditions)-x86_en-us.msi
18=Windows PE Scripts-x86_en-us.msi
19=Windows PE wims (DesktopEditions)-x86_en-us.msi
20=Kits Configuration Installer-x86_en-us.msi
'@
}
'Installers' | ForEach-Object -Process {
# Create target folders if required as BIT doesn't accept missing folders
If (-not(Test-Path (Join-Path -Path $TargetFolder -ChildPath $_))) {
try {
$null = New-Item -Path (Join-Path -Path $TargetFolder -ChildPath $_) -ItemType Directory -Force @HT
} catch {
Write-Warning -Message "Failed to create folder $($TargetFolder)/$_"
break
}
}
}
# Get adkwinpesetup.exe
try {
Write-Verbose -Message "Attempt to download adkwinpesetup.exe from $($MainURL)adkwinpesetup.exe"
Invoke-WebRequest -Uri "$($MainURL)adkwinpesetup.exe" -OutFile "$($TargetFolder)\adkwinpesetup.exe" @HT
Write-Verbose -Message "Successfully downloaded adkwinpesetup.exe from $($MainURL)adkwinpesetup.exe"
} catch {
Write-Warning -Message "Failed to download adkwinpesetup.exe because $($_.Exception.Message)"
break
}
# Create a job that will downlad our first file
$ffsource = "$($MainURL)Installers/$($InstallerURLs['0'])"
$ffdest = (Join-Path -Path $TargetFolder -ChildPath ("Installers/$($InstallerURLs['0'])"))
$fjobHT = @{
Suspended = $true ;
Asynchronous = $true
Source = $ffsource ;
Destination = $ffdest ;
ErrorAction = 'Stop' ;
}
try {
Write-Verbose -Message "Attempt to add to download list $($ffsource) saved to $($ffdest)"
$job = Start-BitsTransfer @fjobHT
} catch {
Write-Warning -Message "Failed to create first BITS job because $($_.Exception.Message)"
break
}
# Downlod installers
For ($i = 1 ; $i -lt $InstallerURLs.Count ; $i++) {
$URL = $Destination = $null
$URL = "$($MainURL)Installers/$($InstallerURLs[$i.ToString()])"
$Destination = Join-Path -Path (Join-Path -Path $TargetFolder -ChildPath Installers) -ChildPath (([URI]$URL).Segments[-1] -replace '%20'," ")
Write-Verbose -Message "Attempt to add to download list $($URL) saved to $($Destination)"
# Add-BitsFile http://technet.microsoft.com/en-us/library/dd819411.aspx
$newjob = Add-BitsFile -BitsJob $job -Source $URL -Destination $Destination
Write-Progress -Activity "Adding file $($newjob.FilesTotal)" -Status "Percent completed: " -PercentComplete (($newjob.FilesTotal)*100/($InstallerURLs.Count))
}
# Begin the download and show us the job
$null = Resume-BitsTransfer -BitsJob $job -Asynchronous
# http://msdn.microsoft.com/en-us/library/windows/desktop/ee663885%28v=vs.85%29.aspx
while ($job.JobState -in @('Connecting','Transferring','Queued')) {
Write-Progress -activity 'Downloading ADK files' -Status 'Percent completed: ' -PercentComplete ($job.BytesTransferred*100/$job.BytesTotal)
}
Switch($job.JobState) {
'Transferred' {
Complete-BitsTransfer -BitsJob $job
Write-Verbose -Message "Successfully downloaded ADK to $($TargetFolder)"
break
}
'Error' {
# List the errors.
$job | Format-List
}
default {
# Perform corrective action.
}
}
} else {
Write-Warning -Message "Guessing the ADK location returned the status code $($adkGenericURL.StatusCode)"
}
}
End {}
}