PowerShell for Windows Admins

Jan 19 2010   2:26PM GMT

Mapping Physical Drives to Logical Drives Part 3



Posted by: Richard Siddaway
Disks

 

Following on from last time we will extend our script to pull the information for logical drives.

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
Get-WmiObject -Class Win32_DiskDrive | foreach {
    "`n {0} {1}" -f $($_.Name), $($_.Model)

    $query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=’" `
     + $_.DeviceID + "’} WHERE ResultClass=Win32_DiskPartition"
    
    Get-WmiObject -Query $query | foreach {
        ""
        "Name : {0}" -f $_.Name
        "Description : {0}" -f $_.Description
        "PrimaryPartition : {0}" -f $_.PrimaryPartition
   
        $query2 = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=’" `
        + $_.DeviceID + "’} WHERE ResultClass=Win32_LogicalDisk"
           
        Get-WmiObject -Query $query2 | Format-List Name,
        @{Name="Disk Size (GB)"; Expression={"{0:F3}" -f $($_.Size/1GB)}},
        @{Name="Free Space (GB)"; Expression={"{0:F3}" -f $($_.FreeSpace/1GB)}}
   
    }
}

When we get the data for the partitions (line 7) we now put that into a foreach loop. The name, description and if its a primary partition are dumped. I’m using a string to write it because we will get a formatting error if we try to use format-list with two different objects in the same script.

A query to get the associators of the partition (as long as its a logical drive) is created and run. The results from that are displayed using the calculated fields we have seen earlier.

That concludes our look at getting information about disks. Next we’ll look at the methods these WMI classes make available – in other words what can we do with them

Technorati Tags: ,,

Comment on this Post

Leave a comment: