December 17th, 2012

Use PowerShell to Map Disk Drives and Partitions

Doctor Scripto
Scripter

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and the CIM cmdlets to use WMI to map disk drives to partitions. Microsoft Scripting Guy, Ed Wilson, is here. It is definitely a cliché that the more things change, the more they stay the same. This is certainly true in the world of computers. I have been an IT Pro for nearly a quarter of a century (dude, that sounds like a long time when I express it that way), and I have seen many changes in this fascinating field. But the fact of the matter is that I am still doing the same sorts of things I did nearly forty years ago when I had an Osborne computer—I do databases, word processing, and spread sheets on a day-in and day-out basis. I also interact with other computers. Truth be told, even using the Scripting Wife’s brand spanking new Surface, I am still doing word processing, spreadsheet databases, and interacting with other computers. And so it goes with scripting and with management tasks. Over the weekend, I was messing around with some of the computers on my network, and I realized that I did not have a mapping of partitions and disk drives … of course, the realization of that fact sprung from a troubleshooting need, but I digress. I thought there was an association WMI class that could do that, and so I used the CIM cmdlets to search the association classes, until I found what I needed.

Note   For more information on CIM classes, refer back to the Use PowerShell CIM Cmdlets to Discover WMI Associations blog post. There is a VBScript version of what I am going to do in the Hey Scripting Guy blog post, How Can I Correlate Logical Disk Drives and Physical Disks. After you read that post, you will realize that my lament, that things stay the same, is for the types of tasks we confront and NOT for the tools we have to use. I mean, dude, that is some heavy duty code, and I have all the respect in the world for my Scripting Guys predecessors. See the way cool History of the Scripting Guys blog post for more information about the FAB FOUR (five)!

Mapping Disk Drives and Disk Partitions

The first thing to do when working with WMI association classes is to find out what the association is comprised of. To do this, use the Get-CimClass cmdlet, and pipe the results to the Select-Object cmdlet. Make sure you expand the CimClassProperties property. This command and its output associated are shown here.

16:49 C:> Get-CimClass win32_diskdriveToDiskPartition | select -exp cimclassproperties

Name               : Antecedent

Value              :

CimType            : Reference

Flags              : Property, Key, ReadOnly, NullValue

Qualifiers         : {read, key, MappingStrings, Override}

ReferenceClassName : Win32_DiskDrive

 

Name               : Dependent

Value              :

CimType            : Reference

Flags              : Property, Key, ReadOnly, NullValue

Qualifiers         : {read, key, MappingStrings, Override}

ReferenceClassName : Win32_DiskPartition The information I need is in the ReferencedClassName property. In fact, as I will show in just a minute, this information is also valuable from a troubleshooting perspective. I mean, I need to know what I am using in order to use it properly. Initially, I thought I could use Get-Disk and pipe it to the Get-CimAssociatedInstance cmdlet, but as is shown here, it does not work.

17:10 C:> get-disk | Get-CimAssociatedInstance -Association win32_DiskDriveToDiskpartition No error returns, but neither does any data. I then decided to look at Get-Disk to see what object it returns. I decide to look at the type object being returned. First, I use the GetType method as shown here.

17:13 C:> (get-disk).GetType()

IsPublic IsSerial Name                                     BaseType

——– ——– —-                                     ——–

True     True     CimInstance                              System.Object Well, that did no good. I now use Get-Member, because I know I have seen the underlying WMI classes before (and it is the actual returned WMI class that I suspect is causing problems). Here is the command I used and its associated output. 17:13 C:> (get-disk | get-member).typename[0] Microsoft.Management.Infrastructure.CimInstance#ROOT/Microsoft/Windows/Storage/MSFT_Disk I see that the Get-Disk function returns a MSFT_Disk class and not an instance of the Win32_DiskDrive WMI class. So, I am back to basics. I use the Get-CimInstance cmdlet to return an instance of the Win32_DiskDrive WMI class. I pipe the results to the Get-CimAssociatedInstance cmdlet and specify the association as the win32_diskdriveToDiskPartition association class. The command and its associated output are shown here.

17:18 C:> Get-CimInstance win32_diskdrive | Get-CimAssociatedInstance -Association w

in32_diskdriveToDiskPartition

Name             NumberOfBlock BootPartition PrimaryPartit Size         Index

                 s                           ion

—-             ————- ————- ————- —-         —–

Disk #0, Part… 716800        True          True          367001600    0

Disk #0, Part… 311859200     False         True          159671910400 1 That is all there is to using Windows PowerShell, WMI, and the CIM classes to map disk drives and disk partitions.  Join me tomorrow when I will talk about more cool Windows PowerShell stuff. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.