Hi,
I’ve searched since yesterday how to resolve the problem, but I’ve found anything.
I’ve simplified my scripts for the post, in reality I have two scripts more difficult, a script for mount an image on a USB drive, and a scripts for eject the drive.
My script “Prepare-Disk” for mount image :
Param (
# Specifies the disk number
[Parameter(Mandatory = $false)]
[string]$Hash,
[Parameter(Mandatory = $false, ParameterSetName="DiskNumber")]
[UInt32]$DiskNumber,
[Parameter(Mandatory = $true)]
[string]$ImageFile,
[switch]$Save
)
function Compare-Hash
{
if (!$Hash)
{
return
}
switch ($Hash.Length)
{
32 {$algorithm = "MD5" }
40 {$algorithm = "SHA1" }
64 {$algorithm = "SHA256" }
96 {$algorithm = "SHA384" }
128 {$algorithm = "SHA512" }
}
$hashImage = (Get-FileHash $ImageFile -Algorithm $algorithm).Hash
$hashResult = $false
if ($Hash -eq $hashImage)
{
$hashResult = $true
}
if (-not $hashResult)
{
Write-Host "Hash error : Hash file and hash value don't match, verify image source." -ForegroundColor red
exit
}
}
#Detect and select disk to use
function Select-Disk
{
$listDisk = Get-Disk | Where-Object BusType -eq USB
$listCountDisk = $listDisk.Number.Count
# If there is no USB drive
if ($listCountDisk = 0)
{
Write-Host "There is no USB drive detected" -ForegroundColor red
return
}
# If there is only one USB drive
if ($listCountDisk = 1)
{
$global:number = $listDisk.Number
}
# If there is two or more USB drive
if ($listCountDisk -ge 2 -And -not $DiskNumber)
{
$listDisk
[Environment]::NewLine
[UInt32]$global:number = Read-Host "Enter the disk number to format"
}
}
#Save files and folders in disk
function Save-Disk-Item
{
if (!$Save)
{
return
}
$listDrive = (Get-Partition -number $number).DriveLetter
foreach ($drive in $listDrive)
{
$directoryInfo = Get-ChildItem "$($drive):\" | Measure-Object
if ($directoryInfo.count -eq 0)
{
break
}
$listItem = (Get-ChildItem "$($drive):\").FullName
$savePath = "$($env:UserProfile)\Documents\Save drive ($($drive))"
New-Item -Path $savePath -ItemType Directory
Move-Item $listItem $savePath
}
}
function Format-Disk
{
$diskName = (Get-Disk -Number $number).Manufacturer
$mountImg = Mount-DiskImage -ImagePath $ImageFile
$mountLetter = ($mountImg | Get-Volume).DriveLetter
Clear-Disk -Number $number -RemoveData -RemoveOEM -Confirm:$true -PassThru | Out-Null
New-Partition -DiskNumber $number -UseMaximumSize -IsActive -AssignDriveLetter | Out-Null
$global:diskLetter = (Get-Partition -DiskNumber $number).DriveLetter
Format-Volume -DriveLetter $diskLetter -FileSystem FAT32 -NewFileSystemLabel "$diskName" | Out-Null
Copy-Item "$($mountLetter):\*" "$($diskLetter):\"
DisMount-DiskImage -ImagePath $ImageFile | Out-Null
}
Compare-Hash
Select-Disk
Save-Disk-Item
Format-Disk
My script for “Eject-Disk” :
Param (
# Specifies the disk number
[Parameter(Mandatory = $false, ValueFromPipeline = $true)]
[string]$Number
)
function Eject
{
$eject = New-Object -comObject Shell.Application
$eject.NameSpace(17).ParseName(“$($Number):”).InvokeVerb(“Eject”)
}
When I run the command :
Prepare-Disk -ImageFile “C:\Users\UserName\Downloads\debian-11.3.0-amd64-netinst.iso” | Eject-Disk
The first script works perfectly ! But the second script doesn’t eject the USB drive on which I work. I’ve search several solution, but I don’t know how to recover the disk number for use it in “Eject-Disk” and eject the USB drive after having mounting.
Thank you again.