-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstall Windows.ps1
641 lines (546 loc) · 23.2 KB
/
Install Windows.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
function Write-Menu {
[CmdletBinding()]
param(
# Array or hashtable containing the menu entries
[Parameter(Mandatory=$true, ValueFromPipeline = $true)]
[ValidateNotNullOrEmpty()]
[Alias('InputObject')]
$Entries,
# Title shown at the top of the menu.
[Parameter(ValueFromPipelineByPropertyName = $true)]
[Alias('Name')]
[string]
$Title,
# Sort entries before they are displayed.
[Parameter()]
[switch]
$Sort,
# Select multiple menu entries using space, each selected entry will then get invoked (this will disable nested menu's).
[Parameter()]
[switch]
$MultiSelect
)
<#
Configuration
#>
# Entry prefix, suffix and padding
$script:cfgPrefix = ' '
$script:cfgPadding = 2
$script:cfgSuffix = ' '
$script:cfgNested = ' >'
# Minimum page width
$script:cfgWidth = 30
# Hide cursor
[System.Console]::CursorVisible = $false
# Save initial colours
$script:colorForeground = [System.Console]::ForegroundColor
$script:colorBackground = [System.Console]::BackgroundColor
<#
Checks
#>
# Check if entries has been passed
if ($Entries -like $null) {
Write-Error "Missing -Entries parameter!"
return
}
# Check if host is console
if ($host.Name -ne 'ConsoleHost') {
Write-Error "[$($host.Name)] Cannot run inside current host, please use a console window instead!"
return
}
<#
Set-Color
#>
function Set-Color ([switch]$Inverted) {
switch ($Inverted) {
$true {
[System.Console]::ForegroundColor = $colorBackground
[System.Console]::BackgroundColor = $colorForeground
}
Default {
[System.Console]::ForegroundColor = $colorForeground
[System.Console]::BackgroundColor = $colorBackground
}
}
}
<#
Get-Menu
#>
function Get-Menu ($script:inputEntries) {
# Clear console
Clear-Host
# Check if -Title has been provided, if so set window title, otherwise set default.
if ($Title -notlike $null) {
# $host.UI.RawUI.WindowTitle = $Title
$script:menuTitle = "$Title"
} else {
$script:menuTitle = 'Menu'
}
# Set menu height
$script:pageSize = ($host.UI.RawUI.WindowSize.Height - 5)
# Convert entries to object
$script:menuEntries = @()
switch ($inputEntries.GetType().Name) {
'String' {
# Set total entries
$script:menuEntryTotal = 1
# Create object
$script:menuEntries = New-Object PSObject -Property @{
Command = ''
Name = $inputEntries
Selected = $false
onConfirm = 'Name'
}; break
}
'Object[]' {
# Get total entries
$script:menuEntryTotal = $inputEntries.Length
# Loop through array
foreach ($i in 0..$($menuEntryTotal - 1)) {
# Create object
$script:menuEntries += New-Object PSObject -Property @{
Command = ''
Name = $($inputEntries)[$i]
Selected = $false
onConfirm = 'Name'
}; $i++
}; break
}
'Hashtable' {
# Get total entries
$script:menuEntryTotal = $inputEntries.Count
# Loop through hashtable
foreach ($i in 0..($menuEntryTotal - 1)) {
# Check if hashtable contains a single entry, copy values directly if true
if ($menuEntryTotal -eq 1) {
$tempName = $($inputEntries.Keys)
$tempCommand = $($inputEntries.Values)
} else {
$tempName = $($inputEntries.Keys)[$i]
$tempCommand = $($inputEntries.Values)[$i]
}
# Check if command contains nested menu
if ($tempCommand.GetType().Name -eq 'Hashtable') {
$tempAction = 'Hashtable'
} elseif ($tempCommand.Substring(0,1) -eq '@') {
$tempAction = 'Invoke'
} else {
$tempAction = 'Command'
}
# Create object
$script:menuEntries += New-Object PSObject -Property @{
Name = $tempName
Command = $tempCommand
Selected = $false
onConfirm = $tempAction
}; $i++
}; break
}
Default {
Write-Error "Type `"$($inputEntries.GetType().Name)`" not supported, please use an array or hashtable."
exit
}
}
# Sort entries
if ($Sort -eq $true) {
$script:menuEntries = $menuEntries | Sort-Object -Property Name
}
# Get longest entry
$script:entryWidth = ($menuEntries.Name | Measure-Object -Maximum -Property Length).Maximum
# Widen if -MultiSelect is enabled
if ($MultiSelect) { $script:entryWidth += 4 }
# Set minimum entry width
if ($entryWidth -lt $cfgWidth) { $script:entryWidth = $cfgWidth }
# Set page width
$script:pageWidth = $cfgPrefix.Length + $cfgPadding + $entryWidth + $cfgPadding + $cfgSuffix.Length
# Set current + total pages
$script:pageCurrent = 0
$script:pageTotal = [math]::Ceiling((($menuEntryTotal - $pageSize) / $pageSize))
# Insert new line
[System.Console]::WriteLine("")
# Save title line location + write title
$script:lineTitle = [System.Console]::CursorTop
[System.Console]::WriteLine(" $menuTitle" + "`n")
# Save first entry line location
$script:lineTop = [System.Console]::CursorTop
}
<#
Get-Page
#>
function Get-Page {
# Update header if multiple pages
if ($pageTotal -ne 0) { Update-Header }
# Clear entries
for ($i = 0; $i -le $pageSize; $i++) {
# Overwrite each entry with whitespace
[System.Console]::WriteLine("".PadRight($pageWidth) + ' ')
}
# Move cursor to first entry
[System.Console]::CursorTop = $lineTop
# Get index of first entry
$script:pageEntryFirst = ($pageSize * $pageCurrent)
# Get amount of entries for last page + fully populated page
if ($pageCurrent -eq $pageTotal) {
$script:pageEntryTotal = ($menuEntryTotal - ($pageSize * $pageTotal))
} else {
$script:pageEntryTotal = $pageSize
}
# Set position within console
$script:lineSelected = 0
# Write all page entries
for ($i = 0; $i -le ($pageEntryTotal - 1); $i++) {
Write-Entry $i
}
}
<#
Write-Entry
#>
function Write-Entry ([int16]$Index, [switch]$Update) {
# Check if entry should be highlighted
switch ($Update) {
$true { $lineHighlight = $false; break }
Default { $lineHighlight = ($Index -eq $lineSelected) }
}
# Page entry name
$pageEntry = $menuEntries[($pageEntryFirst + $Index)].Name
# Prefix checkbox if -MultiSelect is enabled
if ($MultiSelect) {
switch ($menuEntries[($pageEntryFirst + $Index)].Selected) {
$true { $pageEntry = "[X] $pageEntry"; break }
Default { $pageEntry = "[ ] $pageEntry" }
}
}
# Full width highlight + Nested menu indicator
switch ($menuEntries[($pageEntryFirst + $Index)].onConfirm -in 'Hashtable', 'Invoke') {
$true { $pageEntry = "$pageEntry".PadRight($entryWidth) + "$cfgNested"; break }
Default { $pageEntry = "$pageEntry".PadRight($entryWidth + $cfgNested.Length) }
}
# Write new line and add whitespace without inverted colours
[System.Console]::Write("`r" + $cfgPrefix)
# Invert colours if selected
if ($lineHighlight) { Set-Color -Inverted }
# Write page entry
[System.Console]::Write("".PadLeft($cfgPadding) + $pageEntry + "".PadRight($cfgPadding))
# Restore colours if selected
if ($lineHighlight) { Set-Color }
# Entry suffix
[System.Console]::Write($cfgSuffix + "`n")
}
<#
Update-Entry
#>
function Update-Entry ([int16]$Index) {
# Reset current entry
[System.Console]::CursorTop = ($lineTop + $lineSelected)
Write-Entry $lineSelected -Update
# Write updated entry
$script:lineSelected = $Index
[System.Console]::CursorTop = ($lineTop + $Index)
Write-Entry $lineSelected
# Move cursor to first entry on page
[System.Console]::CursorTop = $lineTop
}
<#
Update-Header
#>
function Update-Header {
# Set corrected page numbers
$pCurrent = ($pageCurrent + 1)
$pTotal = ($pageTotal + 1)
# Calculate offset
$pOffset = ($pTotal.ToString()).Length
# Build string, use offset and padding to right align current page number
$script:pageNumber = "{0,-$pOffset}{1,0}" -f "$("$pCurrent".PadLeft($pOffset))","/$pTotal"
# Move cursor to title
[System.Console]::CursorTop = $lineTitle
# Move cursor to the right
[System.Console]::CursorLeft = ($pageWidth - ($pOffset * 2) - 1)
# Write page indicator
[System.Console]::WriteLine("$pageNumber")
}
<#
Initialisation
#>
# Get menu
Get-Menu $Entries
# Get page
Get-Page
# Declare hashtable for nested entries
$menuNested = [ordered]@{}
<#
User Input
#>
# Loop through user input until valid key has been pressed
do { $inputLoop = $true
# Move cursor to first entry and beginning of line
[System.Console]::CursorTop = $lineTop
[System.Console]::Write("`r")
# Get pressed key
$menuInput = [System.Console]::ReadKey($false)
# Define selected entry
$entrySelected = $menuEntries[($pageEntryFirst + $lineSelected)]
# Check if key has function attached to it
switch ($menuInput.Key) {
# Exit / Return
{ $_ -in 'Escape', 'Backspace' } {
# Return to parent if current menu is nested
if ($menuNested.Count -ne 0) {
$pageCurrent = 0
$Title = $($menuNested.GetEnumerator())[$menuNested.Count - 1].Name
Get-Menu $($menuNested.GetEnumerator())[$menuNested.Count - 1].Value
Get-Page
$menuNested.RemoveAt($menuNested.Count - 1) | Out-Null
# Otherwise exit and return $null
} else {
Clear-Host
$inputLoop = $false
[System.Console]::CursorVisible = $true
return $null
}; break
}
# Next entry
'DownArrow' {
if ($lineSelected -lt ($pageEntryTotal - 1)) { # Check if entry isn't last on page
Update-Entry ($lineSelected + 1)
} elseif ($pageCurrent -ne $pageTotal) { # Switch if not on last page
$pageCurrent++
Get-Page
}; break
}
# Previous entry
'UpArrow' {
if ($lineSelected -gt 0) { # Check if entry isn't first on page
Update-Entry ($lineSelected - 1)
} elseif ($pageCurrent -ne 0) { # Switch if not on first page
$pageCurrent--
Get-Page
Update-Entry ($pageEntryTotal - 1)
}; break
}
# Select top entry
'Home' {
if ($lineSelected -ne 0) { # Check if top entry isn't already selected
Update-Entry 0
} elseif ($pageCurrent -ne 0) { # Switch if not on first page
$pageCurrent--
Get-Page
Update-Entry ($pageEntryTotal - 1)
}; break
}
# Select bottom entry
'End' {
if ($lineSelected -ne ($pageEntryTotal - 1)) { # Check if bottom entry isn't already selected
Update-Entry ($pageEntryTotal - 1)
} elseif ($pageCurrent -ne $pageTotal) { # Switch if not on last page
$pageCurrent++
Get-Page
}; break
}
# Next page
{ $_ -in 'RightArrow','PageDown' } {
if ($pageCurrent -lt $pageTotal) { # Check if already on last page
$pageCurrent++
Get-Page
}; break
}
# Previous page
{ $_ -in 'LeftArrow','PageUp' } { # Check if already on first page
if ($pageCurrent -gt 0) {
$pageCurrent--
Get-Page
}; break
}
# Select/check entry if -MultiSelect is enabled
'Spacebar' {
if ($MultiSelect) {
switch ($entrySelected.Selected) {
$true { $entrySelected.Selected = $false }
$false { $entrySelected.Selected = $true }
}
Update-Entry ($lineSelected)
}; break
}
# Select all if -MultiSelect has been enabled
'Insert' {
if ($MultiSelect) {
$menuEntries | ForEach-Object {
$_.Selected = $true
}
Get-Page
}; break
}
# Select none if -MultiSelect has been enabled
'Delete' {
if ($MultiSelect) {
$menuEntries | ForEach-Object {
$_.Selected = $false
}
Get-Page
}; break
}
# Confirm selection
'Enter' {
# Check if -MultiSelect has been enabled
if ($MultiSelect) {
Clear-Host
# Process checked/selected entries
$menuEntries | ForEach-Object {
# Entry contains command, invoke it
if (($_.Selected) -and ($_.Command -notlike $null) -and ($entrySelected.Command.GetType().Name -ne 'Hashtable')) {
Invoke-Expression -Command $_.Command
# Return name, entry does not contain command
} elseif ($_.Selected) {
return $_.Name
}
}
# Exit and re-enable cursor
$inputLoop = $false
[System.Console]::CursorVisible = $true
break
}
# Use onConfirm to process entry
switch ($entrySelected.onConfirm) {
# Return hashtable as nested menu
'Hashtable' {
$menuNested.$Title = $inputEntries
$Title = $entrySelected.Name
Get-Menu $entrySelected.Command
Get-Page
break
}
# Invoke attached command and return as nested menu
'Invoke' {
$menuNested.$Title = $inputEntries
$Title = $entrySelected.Name
Get-Menu $(Invoke-Expression -Command $entrySelected.Command.Substring(1))
Get-Page
break
}
# Invoke attached command and exit
'Command' {
Clear-Host
Invoke-Expression -Command $entrySelected.Command
$inputLoop = $false
[System.Console]::CursorVisible = $true
break
}
# Return name and exit
'Name' {
Clear-Host
return $entrySelected.Name
$inputLoop = $false
[System.Console]::CursorVisible = $true
}
}
}
}
} while ($inputLoop)
}
# Self-elevate the script if required
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
$CommandLine = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb Runas -ArgumentList $CommandLine
Exit
}
}
$Host.UI.RawUI.WindowTitle = "Windows No USB"
function PauseNul ($message = "Press any key to continue... ") {
Write-Host $message -NoNewLine
$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') | Out-Null
}
if ([System.Environment]::OSVersion.Version -le (New-Object System.Version "6.1")) {
Write-Host "This script is not supported on Windows 7, due to lack of proper image deployment support.
You should upgrade to Windows 10 or 11, anything below is no longer supported.`n" -ForegroundColor Red
PauseNul "Press any key to exit... "
exit 1
}
if ($PSVersionTable.PSVersion.Major -lt 5 -or ($PSVersionTable.PSVersion.Major -eq 5 -and $PSVersionTable.PSVersion.Minor -lt 1)) {
Write-Host "This script is not compatible with PowerShell versions below 5.1.
It is advised to update Windows with the WMF 5.1 update or upgrading to 10 or 11.`n" -ForegroundColor Yellow
PauseNul "Press any key to exit... "
exit 1
}
Write-Host "Choosing an ISO, ESD or WIM file to install with file picker..." -ForegroundColor Green
Add-Type -AssemblyName System.Windows.Forms
$filePicker = New-Object Windows.Forms.OpenFileDialog
$filePicker.Title = "Pick a Windows image"
$filePicker.InitialDirectory = "$env:userprofile\Downloads"
$filePicker.Filter = "Image Files (*.iso; *.wim; *.esd)|*.iso;*.wim;*.esd|ISO Files (*.iso)|*.iso|Windows Imaging Format (*.wim)|*.wim|Electronic Software Delivery (*.esd)|*.esd"
$filePicker.Multiselect = $false
$filePickerResult = $filePicker.ShowDialog()
try {
if ($filePickerResult -eq [System.Windows.Forms.DialogResult]::OK) { $selectedFilePath = $filePicker.FileName } else { exit }
do {
Clear-Host
Write-Host "Enter a drive letter for a NTFS partition that currently exists."
Write-Host "You can't select a partition/drive letter with a Windows installation on it!`n"
Write-Host "If your drive is invalid or not NTFS, then this prompt will be repeated until it's valid.`n" -ForegroundColor Yellow
$driveLetter = Read-Host "Drive letter (single character, A-Z)"
$driveLetter = $driveLetter.Substring(0, 1)
} until ($null -ne (Get-Volume -DriveLetter $driveLetter -ErrorAction SilentlyContinue | Where-Object {$_.FileSystem -eq "NTFS"}))
$drivePath = "$driveLetter`:"
Clear-Host
# Mount ISO or set ESD/WIM path
if ([System.IO.Path]::GetExtension($selectedFilePath) -eq ".iso") {
try { Mount-DiskImage -ImagePath "$selectedFilePath" | Out-Null } catch {
Clear-Host
Write-Host "An error occured while trying to mount the ISO.`nError: $($Error[0].Exception.Message)`n" -ForegroundColor Red
PauseNul "Press any key to exit... "
exit 1
}
$mountedDriveLetter = (Get-DiskImage -ImagePath "$selectedFilePath" | Get-Volume).DriveLetter
if (Test-Path "$mountedDriveLetter`:\sources\install.esd") {
$esdWimPath = "$mountedDriveLetter`:\sources\install.esd"
} elseif (Test-Path "$mountedDriveLetter`:\sources\install.wim") {
$esdWimPath = "$mountedDriveLetter`:\sources\install.wim"
} else {
Clear-Host
Write-Host "The selected ISO does not seem to be a Windows ISO." -ForegroundColor Red
Write-Host "The ISO must contain either install.esd or install.wim in the 'sources' folder." -ForegroundColor Yellow
Write-Host "Currently, this script does not support swm (split images).`n" -ForegroundColor Yellow
PauseNul "Press any key to exit..."
exit 1
}
} else { $esdWimPath = $filePicker.FileName }
try {
$editions = Get-WindowsImage -ImagePath $esdWimPath
} catch {
Write-Host "An error occured while trying to get the editions of the selected image!`n$($Error[0].Exception.Message)`n" -ForegroundColor Red
PauseNul "Press any key to exit... "
exit 1
}
$result = Write-Menu -title "Select an edition of Windows" -Sort -Entries ($editions).ImageName
$index = $editions | Where-Object { $_.ImageName -eq $result } | Select-Object -ExpandProperty ImageIndex
Clear-Host
try {
Write-Host "Applying image (installing/extracing Windows)...`n" -ForegroundColor Yellow
$applyImageCommand = Expand-WindowsImage -ImagePath "$esdWimPath" -ApplyPath "$drivePath\" -Index $index -LogLevel 2
} catch {
Write-Host "`nSomething went wrong while trying to apply the Windows image!`nError: $($Error[0].Exception.Message)`nLog: $($applyImageCommand.LogPath)`n" -ForegroundColor Red
PauseNul "Press any key to exit..."
}
if (Test-Path "$drivePath\autounattend.xml") {
Write-Host "Applying unattend answer files...`n" -ForegroundColor Yellow
try {
Use-WindowsUnattend -Path "$drivePath\" -UnattendPath "$drivePath\autounattend.xml"
if (Test-Path $mountedDriveLetter`:\sources\`$OEM`$\`$`$\Setup\Scripts) {
Copy-Item "$mountedDriveLetter`:\sources\`$OEM`$\`$`$\Setup\Scripts\*" -Destination "\Windows\Setup\Scripts" -Recurse
}
Copy-Item "$mountedDriveLetter`:\autounattend.xml*" -Destination "$drivePath\Windows\Setup\Scripts" -Recurse
} catch {
Write-Host "Something went wrong while trying to apply the unattend answer files/scripts!`n$($Error[0].Exception.Message)`n" -ForegroundColor Red
}
}
Write-Host "Adding new Windows installation to boot loader..." -ForegroundColor Yellow
try {
bcdboot $drivePath\Windows | Out-Null
} catch {
Write-Host "Something went wrong trying to add the Windows installation to the boot loader!`nError: $($Error[0].Exception.Message)`n" -ForegroundColor Red
}
Write-Host "Completed!`n" -ForegroundColor Green
PauseNul "Press any key to exit... "
exit
} finally {
Dismount-DiskImage -ImagePath "$selectedFilePath" -ErrorAction SilentlyContinue | Out-Null
}