Give this a try, it will:
- Get all unmounted partitions for a given drive index
$targetDisk
using WMI
- Mount the discovered partitions on the target disk to the next available drive letter using a diskpart script.
Using the GetRelated
method is all about knowing what you need to relate. It helps to know what WMI class represents what you are looking for Win32_DiskPartition
. In your case you want to find the partitions which are not associated with a logical disk (unmounted) so we look for instances of Win32_DiskPartition
which don't have an associated Win32_LogicalDisk
.
Since you only want unmounted volumes on a particular physical disk we need to further associate classes. To do this we need to get Win32_DiskPartition
's associated Win32_DiskDrive
instance.
$targetDisk = 3
$unmounted = gwmi -class win32_DiskPartition | ? {
($_.GetRelated('Win32_LogicalDisk')).Count -eq 0
}
if ($unmounted) {
$commands = @()
$unmounted | ? { $_.GetRelated('Win32_DiskDrive') | ? { $_.Index -eq $targetDisk} } | % {
$commands += "select disk {0}" -f $_.DiskIndex
$commands += "select partition {0}" -f ($_.Index + 1)
$commands += "assign"
}
$tempFile = [io.path]::GetTempFileName()
$commands | out-file $tempFile -Encoding ASCII
$output = & diskpart.exe /s $tempFile 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Error $output
}
}