Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell and wmi, how to map logical disk/volumes to a hard disk or vice versa?

Get-WmiObject -ComputerName $ip -Credential $credential -Class Win32_logicaldisk

This gets me disks as I see them in "My computer", eg. C:, D:, E: Now how I get corresponding underlying physical disks ?

If I run following command

Get-WmiObject -ComputerName $ip -Credential $credential -Class win32_diskdrive

I get disk 0, disk 1, disk 2

So how to find out which logical disk is on which physical disk ?

Another question is how to find out volume number ? If I run diskpart and executes "list volume" I get the following output

  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
  Volume 2     C                NTFS   Partition     59 GB  Healthy    Boot
  ...

How can I find out that logical disk C: is Volume 2 ?

best regards, Primoz.

like image 290
Primoz Avatar asked Dec 16 '22 18:12

Primoz


2 Answers

Try this

Get-WMIObject Win32_LogicalDisk | Foreach-Object {
    Get-WmiObject -Query "Associators of {Win32_LogicalDisk.DeviceID='$($_.DeviceID)'} WHERE ResultRole=Antecedent"
} | Format-Table

This gives you the related instances of WIn32_logicalDisk where Win32_LogicalDisk is the dependent entity in the relationship. So, you get the Win32_DiskDrive instances.

like image 132
ravikanth Avatar answered Apr 28 '23 07:04

ravikanth


Here is Chad Miller's answer modified to work with PowerShell Core and with proper quotes and apostrophes so that it actually works on Linux/Windows and regardless of term settings (i.e., UTF-8 or not):

param ($ComputerName)

$partitions = Get-CimInstance -ComputerName $ComputerName Win32_DiskPartition

$partitions |
foreach `
{
  Get-CimInstance -ComputerName $ComputerName `
                  -Query "ASSOCIATORS OF `
                          {Win32_DiskPartition.DeviceID='$($_.DeviceID)'} `
                          WHERE AssocClass=Win32_LogicalDiskToPartition" |
  Add-Member -MemberType NoteProperty PartitionName $_.Name -PassThru |
  Add-Member -MemberType NoteProperty Block $_.BlockSize -PassThru |
  Add-Member -MemberType NoteProperty StartingOffset $_.StartingOffset -PassThru |
  Add-Member -MemberType NoteProperty StartSector ($_.StartingOffset/$_.BlockSize) `
             -PassThru
} |
Select SystemName, Name, PartitionName, Block, StartingOffset, StartSector |
Sort-Object -Property Name # Sort by Drive letter to improve quality of life
# Select code above starting to the left of the hash in this comment
like image 35
Michael Goldshteyn Avatar answered Apr 28 '23 08:04

Michael Goldshteyn