Closed HedgeComp closed 1 month ago
My guess is that it's an external hard disk and not Removable Media
Run gwmi win32_diskdrive | select * and Media Type will likely be an external hard disk.
My fear with other media types is formatting a drive unintentionally. Removable media is low-hanging fruit, but an external fixed disk could be a NAS or something else with important data on it. When making the USB drive, it doesn't prompt you to select a drive now. So I would probably prompt for that for external fixed disks that are detected. I'd have some things to think about here.
For you to work around this, you could use a removable USB stick, or you could modify the code.
Find this line (line 2902 in v2407.2)
$USBDrives = (Get-WmiObject -Class Win32_DiskDrive -Filter "MediaType='Removable Media'")
Change it to
$USBDrives = (Get-WmiObject -Class Win32_DiskDrive -Filter "MediaType='Removable Media' OR MediaType='External hard disk media'")
The Kingston USB is a removeable stick. https://www.kingston.com/en/usb-flash-drives/datatraveler-max
You are correct that it is seen as an external disk, Here's the output
It appears on line 2959 it loops through ($USBDrive in $USBDrives) and formats each drive.
When it gets to the External Drive it errors:
FYI, I believe I have discovered the Issue. I am working on some logic and then a code change. I'll post it when I'm done. Perhaps you can apply it.
I'm guessing that the drive is probably fresh out of the box? If so, it's probably set to a RAW partition style, which doesn't work well with Clear-Disk.
Clear-Disk also seems to behave different with external hard disk media vs removable. When running clear-disk against an external disk, it sets the partitionstyle as RAW, but when doing the same vs removable, it sets the PartitionStyle to MBR. Because of this, the code needs to be adjusted when partitioning/formating the drive. I have an external drive I'm testing with, and will have a build in the 2408.1 branch I'll link you to when I've got it working.
I did the same sort of logic and place it here:
$ScriptBlock = {
param($DiskNumber)
Clear-Disk -Number $DiskNumber -RemoveData -RemoveOEM -Confirm:$false
Get-Disk $DiskNumber | Get-Partition | Remove-Partition
$Disk = Get-Disk -Number $DiskNumber
if($Disk.PartitionStyle -ne 'RAW')
{
Write-Host "Iam red USB STICK"
$Disk | Set-Disk -PartitionStyle MBR
}
else {
Write-Host "I am RAW"
pause
$Disk | Initialize-Disk -PartitionStyle MBR
}
$BootPartition = $Disk | New-Partition -Size 2GB -IsActive -AssignDriveLetter
$DeployPartition = $Disk | New-Partition -UseMaximumSize -AssignDriveLetter
Format-Volume -Partition $BootPartition -FileSystem FAT32 -NewFileSystemLabel "TempBoot" -Confirm:$false
Format-Volume -Partition $DeployPartition -FileSystem NTFS -NewFileSystemLabel "TempDeploy" -Confirm:$false
}
@rbalsleyMSFT
Tried this with the -AllowExternalHarddriveMedia $true and was wondering if it would make more sense to people to use a paramater to give the drive letter ?
My thought is that if you specify the drive from the Windows perspective and then you only need to grab the Drive.Disknumber from that.
Or throw a prompt as you do for the Driver selection if more than one "External Drive" is found.
just some thoughts, as I did have a USB external travel drive enclosure connected to the back of my PC, so when I was testing, I almost forgot to remove it first before I borked it.. :)
Try this
https://github.com/rbalsleyMSFT/FFU/archive/refs/heads/2408.1.zip
There's a new variable called $PromptExternalHardDiskMedia which is set by default to $true. If that's set, it'll prompt for any drives that are external. Set it to $false and it won't prompt. You'll still need to set $AllowExternalHardDiskMedia to $true.
Doing it this way shouldn't change the experience for those not using external hard disks while those that would like to use them can.
Try this
https://github.com/rbalsleyMSFT/FFU/archive/refs/heads/2408.1.zip
There's a new variable called $PromptExternalHardDiskMedia which is set by default to $true. If that's set, it'll prompt for any drives that are external. Set it to $false and it won't prompt. You'll still need to set $AllowExternalHardDiskMedia to $true.
Doing it this way shouldn't change the experience for those not using external hard disks while those that would like to use them can.
Richard, is there any sort of output change in your code that doesn't allow an Array to print out via Format-Table
command?
I am tweaking some of this new code and I can't display the output in a clean table but If I use a powershell prompt or create a new PS1 with just the your copied logic and run it with my added code, the table appears!?
Am I missing something? I asked Chat .. and it says
I'm cooked.
not sure what that means. lol
Can you share what you're trying to do? There shouldn't be any restrictions here.
right now it doesn't seem to matter what I am working on it won't display a table.
So I create a sample I put write after the opening of Get-USB Function, line 2904, just to see if I can output the objects.
Write-Host "Can I Format an Array?"
# $USBDrives = (Get-WmiObject -Class Win32_DiskDrive -Filter "MediaType='Removable Media'")
WriteLog 'Checking for USB drives'
$colorArray = @(
[PSCustomObject]@{
Index = 1
Color = 'Red'
Hex = '#FF0000'
},
[PSCustomObject]@{
Index = 2
Color = 'Green'
Hex = '#00FF00'
},
[PSCustomObject]@{
Index = 3
Color = 'Blue'
Hex = '#0000FF'
}
)
# Display the debug array using Format-Table
$colorArray | Format-Table -Property Index, Color, Hex -AutoSize
Nothing happens while in your script. If I use this debug anywhere else it works fine.
That's because the call to the Get-USBDrives function is being redirected to two variables.
I see, I put the debug outside the function and it works. Is there a way to output in a format-table while inside the Get-USBDrive Function?
I see, I put the debug outside the function and it works. Is there a way to output in a format-table while inside the Get-USBDrive Function?
I see the issue now, It doesn't dispaly until the variables are returned from the function.
Is there a limitation on the size of the USB drive?
I have some new USB-C/A 3.2 Gen2 drives that are extremely fast and I am using them for images going forward.
My issue is these new drives don't detect via your script? I have used a previous corporate image ISO and rufus to the same drives.
These are Kingston DataTraveler Max
its detected by Windows and is right out the box.