Skip to content

Commit 195100b

Browse files
authored
Added logic to detect proper system and USB disk
1 parent 46aaf8c commit 195100b

File tree

1 file changed

+42
-4
lines changed

1 file changed

+42
-4
lines changed

Invoke-Provision/Invoke-Provision.ps1

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,42 @@ exit
144144
catch {
145145
throw $_
146146
}
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+
}
148183
}
149184
function Set-DrivePartition {
150185
[cmdletbinding()]
@@ -429,7 +464,9 @@ try {
429464
#endregion
430465
#region Configure drive partitions
431466
Write-Host "`nConfiguring 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
433470
#endregion
434471
#region Set paths
435472
Write-Host "`nSetting up Scratch & Recovery paths.." -ForegroundColor Yellow
@@ -510,19 +547,20 @@ catch {
510547
}
511548
finally {
512549
$sw.stop()
550+
$USBDrive = Get-USBDeviceId
513551
if ($exitEarly) {
514552
$errorMsg = $null
515553
}
516554
if ($exitEarlyUsbWipe) {
517-
Format-USBDisk -targetDrive 1
555+
Format-USBDisk -targetDrive $USBDrive
518556
}
519557
if ($errorMsg) {
520558
Write-Warning $errorMsg
521559
}
522560
else {
523561
if ($completed) {
524562
if ($usbWipe) {
525-
Format-USBDisk -targetDrive 1
563+
Format-USBDisk -targetDrive $USBDrive
526564
}
527565
Write-Host "`nProvisioning process completed..`nTotal time taken: $($sw.elapsed)" -ForegroundColor Green
528566
}

0 commit comments

Comments
 (0)