-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCreateWinPE.ps1
More file actions
334 lines (287 loc) · 14 KB
/
CreateWinPE.ps1
File metadata and controls
334 lines (287 loc) · 14 KB
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
<#
.SYNOPSIS
Creating a WinPE ISO image
.DESCRIPTION
Creates an ISO image to be used for deploying ASDK
.PARAMETER TargetDirectory
$TargetDirectory where the image must be created
Note
About 500Mb free space required (is not checked)
.EXAMPLE
CreateASDKDeploymentIso.ps1 -TargetDirectory d:\winpe_asdk
.FUNCTIONALITY
PowerShell Language
#>
[cmdletbinding()]
param (
[string[]]$TargetDirectory
)
#$TargetDirectory='d:\winpe_amd81'
$version="201806105"
$TargetBatchFile=($env:TEMP + '\PreparewinPE.bat')
$ClosingISOBatchFile=($env:TEMP + '\PrepareISO.bat')
function Write-LogMessage {
[cmdletbinding()]
param
(
[string]$SystemName = "PRE-ASDK",
[parameter(Mandatory = $false)]
[string]$Message = ''
)
BEGIN {}
PROCESS {
Write-Verbose "Writing log message"
# Function for displaying formatted log messages. Also displays time in minutes since the script was started
write-host (Get-Date).ToShortTimeString() -ForegroundColor Cyan -NoNewline;
write-host ' - [' -ForegroundColor White -NoNewline;
write-host $systemName -ForegroundColor Yellow -NoNewline;
write-Host "]::$($message)" -ForegroundColor White;
}
END {}
}
Function Get-FileContents {
Param(
[string]$file
)
Process
{
$read = New-Object System.IO.StreamReader($file)
$serverarray = @()
while (($line = $read.ReadLine()) -ne $null)
{
$serverarray += $line
}
$read.Dispose()
return $serverarray
}
}
function DownloadWithRetry{
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]
$url,
[Parameter(Mandatory=$false)]
[string]
$downloadLocation,
[Parameter(Mandatory=$false)]
[int]
$retries
)
while($true)
{
try
{
Invoke-WebRequest $url -OutFile $downloadLocation
break
}
catch
{
$exceptionMessage = $_.Exception.Message
Write-Host "Error downloading '$url': $exceptionMessage"
if ($retries -gt 0) {
$retries--
Write-Host ("Waiting 10 seconds before retrying. Retries left: " + $retries)
Start-Sleep -Seconds 10
}
else
{
$exception = $_.Exception
Write-Host "Failed to download '$url': $exceptionMessage"
break
}
}
}
}
cls
Write-Host " *******************************" -foregroundColor Yellow
write-host " Welcome to the ASDK BUILDER " -foregroundColor Yellow
Write-Host " *******************************" -foregroundColor Yellow
write-host ""
Write-LogMessage -Message "Validating if a newer version is available..."
$localversion=$version
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$Connection = test-connection -computername raw.githubusercontent.com -count 1 -quiet
If (!$Connection) {
Write-LogMessage -Message "No Internet connection available. Using local script"
return $false
}elseIf ($Connection) {
$Uri = 'https://raw.githubusercontent.com/RZomerman/ASDK/master/CreateASDKDeploymentIso.ps1'
$OutFile = ($env:TEMP + '\' + 'CreateASDKDeploymentIso.ps1')
DownloadWithRetry -url $uri -downloadLocation $outfile -retries 3
$DownloadedFile=Get-FileContents $outfile
Foreach ($line in $DownloadedFile) {
If ($line -like '$version=*') {
$version=$line.replace('$version=','')
$version=$version.replace('"',"")
break
}
}
Write-LogMessage -Message "Downloaded file" $version
Write-LogMessage -Message "Local file" $Localversion
If ($version -gt $Localversion) {
Copy-item ($env:TEMP + '\' + 'CreateASDKDeploymentIso.ps1') 'CreateASDKDeploymentIso.ps1' -force
Write-LogMessage -Message "A newer version of this script was downloaded -"
Write-LogMessage -Message " ***Please restart this script***"
exit
}
Elseif ($version -eq $Localversion){
}
Else {
}
}
Write-LogMessage -Message "Validating if running under Admin Privileges"
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
If (!($currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))){
Write-LogMessage -Message "User is not administrator - forced quit"
exit
}
#Creating a copy of the Deployment Toolkit cmd start script so we can add the copype.cmd to it when starting
If (test-path "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\DandISetEnv.bat") {
copy-item "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\DandISetEnv.bat" $TargetBatchFile
$batchfile = Get-Content $TargetBatchFile
$batchfile += "`r`nREM Creating WinPE repository"
$batchfile += "`r`ncopype.cmd amd64 $TargetDirectory"
Set-Content -Value $batchfile -Path $TargetBatchFile -Force
#Starting the Deployment Toolkit CMD from the created batchfile - this will automatically start the copype.cmd script
If (!(test-path ($TargetDirectory + '\media'))) {
Write-LogMessage -Message "Creating base WinPE image"
Start-Process 'C:\WINDOWS\system32\cmd.exe' -argumentlist "/k $TargetBatchFile" -Verb runAs -WindowStyle Minimized
}
}
Else {
Write-LogMessage -Message "Windows Deployment tools not found!" -ForegroundColor red
exit
}
#Need to wait for the copype.cmd to be completed - last file seems to be Media\zh-tw\bootmgt.efi.mui
$MonitorredFile=($TargetDirectory + '\Media\zh-tw\bootmgr.efi.mui')
Write-LogMessage -Message "waiting for copy to complete..."
While (1 -eq 1) {
IF (Test-Path $MonitorredFile) {
#file exists. break loop
break
}
#sleep for 2 seconds, then check again
Start-Sleep -s 2
}
#Mounting the image and adding the required repositories to it
$1=('/mount-image /imagefile:' + $TargetDirectory + '\media\sources\boot.wim /index:1 /mountdir:' + $TargetDirectory + '\mount')
$2=('/Image:' + $TargetDirectory + '\mount /Add-Package /PackagePath:"C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64\WinPE_OCs\WinPE-WMI.cab"')
$3=('/Image:' + $TargetDirectory + '\mount /Add-Package /PackagePath:"C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64\WinPE_OCs\WinPE-NetFX.cab"')
$4=('/Image:' + $TargetDirectory + '\mount /Add-Package /PackagePath:"C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64\WinPE_OCs\WinPE-Scripting.cab"')
$5=('/Image:' + $TargetDirectory + '\mount /Add-Package /PackagePath:"C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64\WinPE_OCs\WinPE-PowerShell.cab"')
$6=('/Image:' + $TargetDirectory + '\mount /Add-Package /PackagePath:"C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64\WinPE_OCs\WinPE-DismCmdlets.cab"')
$7=('/Image:' + $TargetDirectory + '\mount /Add-Package /PackagePath:"C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Windows Preinstallation Environment\amd64\WinPE_OCs\WinPE-StorageWMI.cab"')
Write-LogMessage -Message "Mounting the WinPE image"
Start-Process 'DISM' -ArgumentList $1 -WindowStyle Minimized
$MonitorredFile=($TargetDirectory + '\mount\Windows\System32\winpe.jpg')
While (1 -eq 1) {
IF (Test-Path $MonitorredFile) {
#file exists. break loop
break
}
#sleep for 2 seconds, then check again
Start-Sleep -s 2
}
Write-LogMessage -Message "Adding WMI repository"
Start-Process 'DISM' -wait -ArgumentList $2 -WindowStyle Minimized
Write-LogMessage -Message "Adding Network repository"
Start-Process 'DISM' -wait -ArgumentList $3 -WindowStyle Minimized
Write-LogMessage -Message "Adding Scriptig repository"
Start-Process 'DISM' -wait -ArgumentList $4 -WindowStyle Minimized
Write-LogMessage -Message "Adding Powershell repository"
Start-Process 'DISM' -wait -ArgumentList $5 -WindowStyle Minimized
Write-LogMessage -Message "Adding DISM repository"
Start-Process 'DISM' -wait -ArgumentList $6 -WindowStyle Minimized
Write-LogMessage -Message "Adding Storage WMI repository"
Start-Process 'DISM' -wait -ArgumentList $7 -WindowStyle Minimized
#Need to copy all the required files from github to the new destination
Write-LogMessage -Message "Downloading scripts from GitHub"
$Uri = 'https://raw.githubusercontent.com/RZomerman/ASDK/master/PrepareAzureStackPOC.psm1'
$OutFile = ($env:TEMP + '\' + 'PrepareAzureStackPOC.psm1')
DownloadWithRetry -url $uri -downloadLocation $outfile -retries 3
$DownloadedFile=Get-FileContents $outfile
$Uri = 'https://raw.githubusercontent.com/RZomerman/ASDK/master/PrepareAzureStackPOC.ps1'
$OutFile = ($env:TEMP + '\' + 'PrepareAzureStackPOC.ps1')
DownloadWithRetry -url $uri -downloadLocation $outfile -retries 3
$DownloadedFile=Get-FileContents $outfile
$Uri = 'https://raw.githubusercontent.com/RZomerman/ASDK/master/winpe.jpg'
$OutFile = ($env:TEMP + '\' + 'winpe.jpg')
DownloadWithRetry -url $uri -downloadLocation $outfile -retries 3
$DownloadedFile=Get-FileContents $outfile
#Copy the files to the mounted image
If (test-path ($TargetDirectory + "\mount\Windows")) {
Write-LogMessage -Message "Copying files to the mounted image"
If (test-path ($env:TEMP + '\' + 'PrepareAzureStackPOC.ps1')) {
Copy-Item ($env:TEMP + '\' + 'PrepareAzureStackPOC.ps1') ($TargetDirectory + '\mount\PrepareAzureStackPOC.ps1') -Force
}
If (test-path ($env:TEMP + '\' + 'PrepareAzureStackPOC.psm1')) {
Copy-Item ($env:TEMP + '\' + 'PrepareAzureStackPOC.psm1') ($TargetDirectory + '\mount\PrepareAzureStackPOC.psm1') -Force
}
#need to take ownership of WinPE and delete it (to change background later on)
Write-LogMessage -Message "Taking ownership of background image"
$BackgroundImage=($TargetDirectory + '\mount\Windows\System32\winpe.jpg')
$acl=Get-Acl $BackgroundImage
$Group = New-Object System.Security.Principal.NTAccount("Builtin", "Administrators")
$ACL.SetOwner($Group)
Set-Acl -Path $BackgroundImage -AclObject $acl
#Delete the old WinPE.jpg and copy the new one
Write-LogMessage -Message "Taking full control of background image for replacement"
$permission = ".\Administrators","FullControl","Allow"
$accessRule = new-object System.Security.AccessControl.FileSystemAccessRule $permission
$acl.SetAccessRule($accessRule)
Set-Acl -Path $BackgroundImage -AclObject $acl
remove-item $BackgroundImage
If (test-path ($env:TEMP + '\' + 'winpe.jpg')) {
Write-LogMessage -Message "Replacing background image"
Copy-Item ($env:TEMP + '\' + 'winpe.jpg') ($BackgroundImage) -Force
}
#Setting the autostart to run powershell start.ps1 script
Write-LogMessage -Message "Setting the autostart scripts"
$startnet = Get-Content ($TargetDirectory + '\mount\windows\system32\startnet.cmd')
$startnet += "`r`npowershell -c Set-ExecutionPolicy Unrestricted -Force"
$startnet += "`r`ncd\"
$startnet += "`r`npowershell -NoExit -c X:\Start.ps1"
#$startnet
Set-Content -Value $startnet -Path ($TargetDirectory + "\mount\windows\system32\startnet.cmd") -Force
}
#Closing the mount and making an ISO out of it.....
$DISM=('/unmount-image /mountdir:' + $TargetDirectory + '\mount /commit')
Write-LogMessage -Message "Unmounting the image"
Start-process 'Dism' -ArgumentList $DISM -Wait -WindowStyle Minimized
$ISO=('MakeWinPEMedia /ISO ' + $TargetDirectory + ' ' + $TargetDirectory + '\WinPE_ASDK_Stack.iso')
If (test-path "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\DandISetEnv.bat") {
copy-item "C:\Program Files (x86)\Windows Kits\10\Assessment and Deployment Kit\Deployment Tools\DandISetEnv.bat" $ClosingISOBatchFile
$ISOfile = Get-Content $ClosingISOBatchFile
$ISOfile += "`r`nREM Creating ISO"
$ISOfile += "`r`n$ISO"
Set-Content -Value $ISOfile -Path $ClosingISOBatchFile -Force
Write-LogMessage -Message "Creating the ISO image"
$MonitorredFile=($TargetDirectory + '\WinPE_ASDK_Stack.iso')
If (test-path $MonitorredFile) {
Write-LogMessage "ISO is already present.."
}Else{
Start-Process 'C:\WINDOWS\system32\cmd.exe' -argumentlist "/k $ClosingISOBatchFile" -Verb runAs -WindowStyle Minimized
}
}
$MonitorredFile=($TargetDirectory + '\WinPE_ASDK_Stack.iso')
Write-LogMessage -Message "waiting for ISO to be created"
While (1 -eq 1) {
IF (Test-Path $MonitorredFile) {
#file exists. break loop
break
}
#sleep for 2 seconds, then check again
Start-Sleep -s 4
}
If (test-path ($TargetDirectory + '\WinPE_ASDK_Stack.iso')) {
Write-LogMessage -Message "Iso successfully created"
$ISOOutput=($TargetDirectory + '\WinPE_ASDK_Stack.iso')
Write-Host " ***********************************************" -foregroundColor Yellow
write-host " please check $ISOOutput " -foregroundColor Yellow
Write-Host " ***********************************************" -foregroundColor Yellow
write-host ""
}
#Dism /unmount-image /mountdir:D:\winpe_amd71\mount /commit
#MakeWinPEMedia /ISO D:\winpe_amd71 D:\winpe_amd71\WinPE_ASDK_Stack.iso
#MakeWinPEMedia /UFD D:\winpe_amd71 P: