@@ -144,7 +144,42 @@ exit
144
144
catch {
145
145
throw $_
146
146
}
147
-
147
+ }
148
+ function Get-USBDeviceId {
149
+ try {
150
+ $USBDrives = $drives | ? { $_.BusType -eq " USB" }
151
+ if (@ ($USBDrives ).count -eq 1 ) {
152
+ $USBDrive = $USBDrives [0 ].DeviceId
153
+ return $USBDrive
154
+ }
155
+ else {
156
+ throw " Error while getting DeviceId of USB Stick. No additional USB storage devices must be attached"
157
+ }
158
+ }
159
+ catch {
160
+ throw $_
161
+ }
162
+ }
163
+ function Get-SystemDeviceId {
164
+ try {
165
+ $dataDrives = $drives | ? { $_.BusType -ne " USB" }
166
+ if (@ ($DataDrives ).count -eq 1 ) {
167
+ $targetDrive = $DataDrives [0 ].DeviceId
168
+ return $targetDrive
169
+ }
170
+ elseif (@ ($DataDrives ).count -gt 1 ) {
171
+ Write-Host " More than one disk has been detected. Select disk where Windows should be installed" - ForegroundColor Yellow
172
+ $DataDrives | ft DeviceId, FriendlyName, Size| Out-String | % {Write-Host $_ - ForegroundColor Cyan}
173
+ $targetDrive = Read-Host " Please make a selection..."
174
+ return $targetDrive
175
+ }
176
+ else {
177
+ throw " Error while getting DeviceId of potiential Windows target drives"
178
+ }
179
+ }
180
+ catch {
181
+ throw $_
182
+ }
148
183
}
149
184
function Set-DrivePartition {
150
185
[cmdletbinding ()]
@@ -429,7 +464,9 @@ try {
429
464
# endregion
430
465
# region Configure drive partitions
431
466
Write-Host " `n Configuring drive partitions.." - ForegroundColor Yellow
432
- Set-DrivePartition - winPEDrive $usb.winPEDrive - targetDrive 0
467
+ $drives = @ (Get-PhysicalDisk )
468
+ $targetDrive = Get-SystemDeviceId
469
+ Set-DrivePartition - winPEDrive $usb.winPEDrive - targetDrive $targetDrive
433
470
# endregion
434
471
# region Set paths
435
472
Write-Host " `n Setting up Scratch & Recovery paths.." - ForegroundColor Yellow
@@ -510,19 +547,20 @@ catch {
510
547
}
511
548
finally {
512
549
$sw.stop ()
550
+ $USBDrive = Get-USBDeviceId
513
551
if ($exitEarly ) {
514
552
$errorMsg = $null
515
553
}
516
554
if ($exitEarlyUsbWipe ) {
517
- Format-USBDisk - targetDrive 1
555
+ Format-USBDisk - targetDrive $USBDrive
518
556
}
519
557
if ($errorMsg ) {
520
558
Write-Warning $errorMsg
521
559
}
522
560
else {
523
561
if ($completed ) {
524
562
if ($usbWipe ) {
525
- Format-USBDisk - targetDrive 1
563
+ Format-USBDisk - targetDrive $USBDrive
526
564
}
527
565
Write-Host " `n Provisioning process completed..`n Total time taken: $ ( $sw.elapsed ) " - ForegroundColor Green
528
566
}
0 commit comments