Drive info

The CIM CmdLets in PowerShell I think is a collection of great new features and a long needed replacements to WMI.
One very useful CmdLet is Get-CimAssociatedInstance that makes the hierarchy of classes nice and easy.
This I had use for lately for checking the drives on some new database servers. That was large servers with several drives on various storage technologies.

By travelling through the associated CIM objects I was able to get from the drive with a drive letter to the physical diskdrive.
The chain could be described like:

Drive or Volume - Logical Disk - Disk Partition - Disk Drive

The Volume (Win32_Volume) has no direct association to the Logical Disk (Win32_LogicalDisk), but the Logical Disk has the drive letter as key in the attribute DeviceID. The Volume object still holds some interesting attributes like BlockSize.

With a Logical Disk object I got the Disk Partition (Win32_DiskPartition). And from there I got the Disk Drive (Win32_DiskDrive).
The complete chain on the C-drive can look like this in PowerShell:

[String]$DriveLetter = 'C'

$WqlVolume = "SELECT * FROM Win32_Volume WHERE DriveLetter='$($DriveLetter):'"
$CimVolume = Get-CimInstance -Query $WqlVolume
$CimVolume | fl *

$WqlLogicalDisk = "SELECT * FROM Win32_LogicalDisk WHERE DeviceID='$($DriveLetter):'"
$CimDisk = Get-CimInstance -Query $WqlLogicalDisk
$CimDisk | fl *

$CimDiskPartition = Get-CimAssociatedInstance -InputObject $CimDisk -ResultClassName 'Win32_DiskPartition'
$CimDiskPartition | fl *

$CimDiskDrive = Get-CimAssociatedInstance -InputObject $CimDiskPartition -ResultClassName 'Win32_DiskDrive'
$CimDiskDrive | fl *

Now all the info is stored in variables, and can be accessed and processed directly.
If you like a selection of data can be combined in a custom PowerShell object (PSObject).

It is obvious that the classes for the more physical elements were defined for spinning plate disks, e.g. a SSD on SATA has tracks and cylinders according to Win32_PhysicalDrive.

No comments: