Using WMI to link a Disk Volume to a Physical Disk with PowerShell


Recently someone asked how to map a disk volume to a physical disk. At first I figured this would be real easy and just involve two WMI calls. Come to find out, it’s not that easy, but not impossible.


$diskdrive = gwmi win32_diskdrive

foreach($drive in $diskdrive)

{

out-host -InputObject "`nDrive: deviceid-$($drive.deviceid.substring(4)) Model - $($drive.model)"

##partition

$partitions = gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($drive.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"

foreach($part in $partitions)

{

Out-Host -InputObject "`tPartition: $($part.name)"

$vols = gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($part.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"

foreach($vol in $vols)

{

out-host -InputObject "`t`t$($vol.name)"

}

}

}

Not too bad, but could be better. There is a GetRelated method that PowerShell tack on to WMI objects which should allow you to do the Associate stuff with PowerShell rather than writing WMI queries but I havent figured that out.

Advertisements

About jrich

I am the Solutions Architect for Apex Learning in Seattle WA. I've been working with computers since I was 13. Started programming when I was 14. Had my first IT job as tech support at an ISP at the age of 15 and became a network admin at the age of 17. Since then I've worked at a variety of small to mid size companies supporting, maintaining and developing all aspects of IT. Mostly working with Windows based networks but have recently been working with Solaris system as well. I created this blog mostly as a place for me to take my own notes, but also share things that I had a hard time finding the info for.

Posted on December 12, 2011, in WMF (Powershell/WinRM) and tagged , , . Bookmark the permalink. 8 Comments.

  1. Thanks, interesting script. BUT… How to link unmounted volumes with physical disk? it would allow to mount selected volumes on a given disk…

  2. How would you link a mount point to a physical disk?

  3. I was also looking for the same answer as junnie. I finally figured out an answer.
    In the interest of helping others that stumble upon this page, you can make the hop between unmounted volumes to physical disk by way of the registry.
    The GUID in Win32_Volume.DeviceID can found in a value name within HKLM\SYSTEM\MountedDevices. The value’s data will be 12 bytes long for volumes that map to a physical partition: the first four bytes match the physical disk’s signature (Win32_DiskDrive.Signature) and the final eight bytes are the offset to the partition, matching Win32_DiskPartition.StartingOffset.
    This lets you link everything together at Diskpart does.

  1. Pingback: Geoff @ UVM - Which Disk is that volume on?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: